一、安裝依賴包
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
依賴包說明:
1、編譯依賴 gcc 環(huán)境,所以需要:gcc gcc-c++;
2、PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發(fā)的一個二次開發(fā)庫,所以需要:pcre pcre-devel ;
3、zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內(nèi)容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫,所以需要:zlib zlib-devel ;
4、OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協(xié)議,并提供豐富的應(yīng)用程序供測試或其它目的使用。nginx 不僅支持 http 協(xié)議,還支持 https(即在ssl協(xié)議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫,所以需要:openssl openssl-devel ;
二、從官網(wǎng)下載安裝包
wget https://nginx.org/download/nginx-1.16.1.tar.gz
三、解壓并安裝
tar zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx
make && make install
四、測試安裝是否成功
whereis nginx
五、啟動nginx服務(wù)
cd /usr/local/nginx/sbin
./nginx
六、驗證服務(wù)是否啟動成功
[root@localhost sbin]# netstat -ntlp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 349/nginx:
master
七、添加nginx服務(wù)
vi /lib/systemd/system/nginx.service
將以下內(nèi)容插入:
[Unit]
Description=nginx
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 quit
PrivateTmp=true [Install]
WantedBy=multi-user.target
八、以服務(wù)的方式啟動nginx
pkill nginx
systemctl start nginx
九、查看服務(wù)是否啟動
[root@localhost sbin]# systemctl status nginx
● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2019-04-29 23:19:39 EDT; 18min ago
Process: 348 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 349 (nginx)
Tasks: 2 Memory: 976.0K
CGroup: /system.slice/nginx.service
├─349 nginx: master process /usr/local/nginx/sbin/nginx
└─350 nginx: worker process
Apr 29 23:19:39 localhost.localdomain systemd[1]: Starting nginx...
Apr 29 23:19:39 localhost.localdomain systemd[1]: Started nginx.</pre>
[root@localhost sbin]# netstat -ntlp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 349/nginx:
master p
十、配置nginx服務(wù)自動啟動
[root@localhost sbin]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.