1. 程式人生 > >centos 7安裝 nginx

centos 7安裝 nginx

1、更新yum

yum update

2、安裝依賴

yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

3、進入到 /usr/local/src目錄下載nginx

wget http://nginx.org/download/nginx-1.14.0.tar.gz

4、解壓

tar -zxvf nginx-1.14.0.tar.gz

5、編譯

./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/tem/nginx/client --http-proxy-temp-path=/var/tem/nginx/proxy --http-fastcgi-temp-path=/var/tem/nginx/fcgi --with-http_stub_status_module

6、安裝

make && make install

7、啟動

nginx -c /etc/nginx/nginx.conf

8、如果出現[emerg] getpwnam("nginx") failed 錯誤 執行

useradd -s /sbin/nologin -M nginx
id nginx

9、如果出現 [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or directory) 錯誤 執行

mkdir -p /var/tem/nginx/client

10、如果您正在執行防火牆,請執行以下命令以允許HTTP和HTTPS通訊:

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

11、輸入伺服器地址,檢視是否成功

12、配置nginx開機啟動

切換到/lib/systemd/system/目錄,建立nginx.service檔案vi nginx.service

cd /lib/systemd/system
vi nginx.service

輸入內容如下:

[Unit]
Description=nginx 
After=network.target

[Service] 
Type=forking 
ExecStart=/usr/local/src/nginx/sbin/nginx
ExecReload=/usr/local/src/nginx/sbin/nginx reload
ExecStop=/usr/local/src/nginx/sbin/nginx quit
PrivateTmp=true 
   
[Install] 
WantedBy=multi-user.target

其中/usr/local/src為你的nginx安裝目錄

退出儲存檔案,然後執行命令:

systemctl enable nginx.service

systemctl start nginx.service 啟動nginx

systemctl stop nginx.service 結束nginx

systemctl restart nginx.service 重啟nginx

最後請求伺服器,檢驗是否安裝成功!