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