CentOS 下 Nginx 安裝

1. 安裝依賴包nginx

yum update 
yum install -y gcc pcre pcre-devel openssl openssl-devel gd gd-devel

2.上傳 nginx-1.18.0.tar.gz 至 /usr/local/nginxvim

   需安裝 lrzsz 在/usr/local/nginx目錄下使用 rz 命令centos

yum install -y lrzsz

3.解壓 spa

tar -xzvf nginx-1.18.0.tar.gz

4.  進入解壓後的目錄 運行 ./configurerest

[root@VM-0-7-centos nginx-1.12.2]# ./configure 
checking for OS
......
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

5. make & make installcode

7. 爲nginx 建立systemd服務server

 

服務文件模板 參考: https://www.nginx.com/resources/wiki/start/topics/examples/systemd/ip

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

根據實際狀況修改  ssl

PIDFile \ExecStartPer \ExecStart \ ExexReload rem

保存到  /lib/systemd/system/nginx.service

PID 文件須要提早建立好。

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target 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/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx-s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

8.  systemctl daemon-reload 從新加載

9. 其餘命令

啓動nginx服務

systemctl start nginx.service

設置開機自啓動

systemctl enable nginx.service

中止開機自啓動

systemctl disable nginx.service

查看服務當前狀態

systemctl status nginx.service

從新啓動服務

systemctl restart nginx.service

查看全部已啓動的服務


systemctl list-units --type=service

10. 將nginx 添加進環境變量

10.一、編輯/etc/profile

vim /etc/profile

10.二、在最後一行添加配置,:wq保存

PATH=$PATH:/usr/local/nginx/sbin
export PATH

10.三、使配置當即生效


source /etc/profile

11. nginx 經常使用命令

11.1 啓動、從新加載、關閉

[root@VM-0-7-centos sbin]# nginx
[root@VM-0-7-centos sbin]# nginx -s reload
[root@VM-0-7-centos sbin]# nginx -s  stop

11.2 查看版本

nginx -v

[root@VM-0-7-centos sbin]# nginx -v
nginx version: nginx/1.18.0

12. 指定配置文件啓動

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
相關文章
相關標籤/搜索