下載Nginx wget http://nginx.org/download/nginx-1.12.2.tar.gz 一、安裝nginx時必須先安裝相應的編譯工具 [root@xuegod63 ~]#yum -y install gcc gcc-c++ autoconf automake gcc c ...
下載Nginx wget http://nginx.org/download/nginx-1.12.2.tar.gz 一、安裝nginx時必須先安裝相應的編譯工具 [root@xuegod63 ~]#yum -y install gcc gcc-c++ autoconf automake gcc c語言編譯器 gcc-c++ c++語言編譯器 autoconf automake 用於make編譯的工具 [root@xuegod63 ~]#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel zlib :nginx提供gzip模塊,需要zlib庫支持 openssl :nginx提供ssl功能 pcre :支持地址重寫rewrite功能 安裝nginx: 上傳nginx軟體包對 [root@xuegod63 ~]# yum install lrzsz -y [root@xuegod63 ~]# tar zxvf nginx-1.12.2.tar.gz [root@xuegod63 ~]# cd nginx-1.12.2 [root@xuegod63 nginx-1.12.2]# useradd -s /sbin/nologin -M nginx #創建一個nginx用戶 -s 指定登錄shell -M 不創建家目錄 #groupadd -r nginx -r 系統賬號 #useradd -g nginx -r nginx # [root@xuegod63 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/bin/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-pcre --with-http_stub_status_module --with-http_addition_module --with-http_gzip_static_module #檢查編譯環境 參數說明: --prefix=/usr/local/nginx 指定安裝路徑 --user=nginx --group=nginx 指定運行nginx進程的用戶和組 --with-http_ssl_module 支持ssl加密 --with-http_realip_module 此模塊支持顯示真實來源IP地址,主要用於NGINX做前端負載均衡伺服器使用 --with-http_gzip_static_module 這個模塊指定壓縮 --with-pcre 此模塊支持rewrite功能 --with-http_stub_status_module 此模塊用於查看Nginx的一些狀態信息. [root@xuegod63 nginx-1.12.2]# echo $? 0 編譯和安裝: [root@xuegod63 nginx-1.12.2]# make -j 4 #編譯,把源代碼編譯成可執行的二進位文件。 -j 4 #以4個進程同時編譯 編譯:mysql 內核 1個小時。 -j 4 20分鐘。 [root@xuegod63 nginx-1.12.2]# make install #安裝 nginx主要目錄結構: [root@xuegod63 nginx-1.12.2]# ls /usr/local/nginx/ conf html logs sbin conf #配置文件 html #網站根目錄 logs #日誌 sbin #nginx啟動腳本 啟動nginx : [root@xuegod63 ~]# nginx 查看版本 [root@xuegod63 ~]# nginx -v 查看安裝具體信息 [root@xuegod63 ~]# nginx -V 需要重新進行nginx安裝: [root@xuegod63 nginx-1.12.2]# /usr/local/nginx/sbin/nginx -s stop [root@xuegod63 nginx-1.12.2]# rm -rf /usr/local/nginx/ [root@xuegod63 nginx-1.12.2]# pwd /root/nginx-1.10.2 [root@xuegod63 nginx-1.12.2]# make clean rm -rf Makefile objs 編寫Nginx的systemd啟動腳本 systemd啟動腳本不需要編寫shell,只需在 /usr/lib/systemd/system/ 目錄下建一個 nginx.service 腳本,內容如下 [root@xuegod63 nginx-1.12.2]# pwd /usr/lib/systemd/system [root@purenlai system]# cat nginx.service [Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ #After參數設置項用來確認啟動順序 After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/bin/nginx -t ExecStart=/usr/bin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID [Install] WantedBy=multi-user.target 重新載入所有的[Unit]文件 [root@purenlai system]# systemctl daemon-reload [root@purenlai system]# systemctl start nginx [root@purenlai system]# systemctl status nginx ● nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2019-08-20 14:14:25 CST; 2h 50min ago Docs: http://nginx.org/en/docs/ Process: 25287 ExecStart=/usr/bin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS) Process: 25286 ExecStartPre=/usr/bin/nginx -t (code=exited, status=0/SUCCESS) Main PID: 25290 (nginx) CGroup: /system.slice/nginx.service ├─25290 nginx: master process /usr/bin/nginx -c /usr/local/nginx/conf/nginx.conf └─25291 nginx: worker process Aug 20 14:14:25 purenlai systemd[1]: Starting nginx - high performance web server... Aug 20 14:14:25 purenlai nginx[25286]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok Aug 20 14:14:25 purenlai nginx[25286]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful Aug 20 14:14:25 purenlai systemd[1]: Failed to read PID from file /usr/local/nginx/logs/nginx.pid: Invalid argument Aug 20 14:14:25 purenlai systemd[1]: Started nginx - high performance web server. |