系統基礎環境:
# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
# cat /proc/version Linux version 3.10.0-957.5.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Fri Feb 1 14:54:57 UTC 2019
安裝方法一:源碼編譯安裝
基礎環境 : CentOS Linux release 7.6.1810 (Core)
目錄:/usr/local/src
1.1、安裝前准備(根據各軟件最新版本信息更改url)
下載nginx、openssl、zlib、pcre
# cd /usr/local/src # wget http://nginx.org/download/nginx-1.16.0.tar.gz # wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz # wget http://zlib.net/zlib-1.2.11.tar.gz # wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
1.2、安裝(順序不要錯)
安裝make
# yum -y install gcc automake autoconf libtool make
安裝gcc編譯環境
# yum install gcc-c++
安裝pcre
# tar zxvf pcre-8.43.tar.gz # cd pcre-8.43 # ./configure && make && make install
安裝openssl:
# cd .. # tar zxvf openssl-fips-2.0.10.tar.gz # cd openssl-fips-2.0.10 # ./config && make && make install
安裝zlib
# cd .. # tar zxvf zlib-1.2.11.tar.gz # cd zlib-1.2.11 # ./configure && make && make install
安裝nginx
# cd .. # tar zxvf nginx-1.16.0.tar.gz # cd nginx-1.16.0 # ./configure && make && make install
這樣安裝完成后,nginx的路徑為/usr/local/nginx/sbin/nginx
# whereis nginx
nginx: /usr/local/nginx
安裝方法二:
查看CentOS的版本
# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
添加資源庫 , 在 CentOS 系統上安裝 Nginx ,得先去添加一個資源庫:
# vim /etc/yum.repos.d/nginx.repo
輸入如下代碼:
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1
安裝nginx
# yum -y install nginx
使用命令
# systemctl stop nginx.service #停止nginx服務
# systemctl start nginx.service #打開nginx服務
# systemctl restart nginx.service #重啟nginx服務
# systemctl status nginx.service #查看nginx服務狀態
開啟並驗證nginx安裝成果:
開啟nginx:# cd /usr/local/nginx/sbin # ./nginx
或
# systemctl start nginx.service
# curl http://localhost
應該能輸出Welcome to nginx的html文件,說明安裝啟動成功。
開啟外部訪問
centos7默認開啟了防火牆模塊firewalld,可以查看一下是否開啟
# ystemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: active (running) since Mon 2019-03-18 10:12:46 EDT; 34s ago Docs: man:firewalld(1) Main PID: 3618 (firewalld) CGroup: /system.slice/firewalld.service └─3618 /usr/bin/python -Es /usr/sbin/firewalld --nofork... Mar 18 10:12:46 localhost.localdomain systemd[1]: Starting firewal... Mar 18 10:12:46 localhost.localdomain systemd[1]: Started firewall... Hint: Some lines were ellipsized, use -l to show in full. 或者: # firewall-cmd --state running
關閉firewalld,然后在虛擬機外可以訪問
# systemctl stop firewalld #關閉
# systemctl start firewalld #打開
# systemctl restart firewalld #重啟
# systemctl disable firewalld #關閉開機啟動
# systemctl enable firewalld #開機啟動
外部訪問nginx主頁:http://***.***.***.***

添加 nginx 為系統服務,設置開機自啟
1、在 /lib/systemd/system 目錄添加 nginx.service 文件
# vim /lib/systemd/system/nginx.service
2、寫入以下內容,保存退出
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop ExecRestart=/usr/local/nginx/sbin/nginx -s restart PrivateTmp=true [Install] WantedBy=multi-user.target
3、設置開機自啟
# systemctl enable nginx
4、其它命令
# 啟動 nginx
systemctl start nginx
# 停止
systemctl stop nginx
# 加載配置文件
systemctl reload nginx
# 重啟
systemctl restart nginx
# 查看狀態
systemctl status nginx