前言
系统环境
1
| Linux option 5.10.0-24-cloud-amd64 #1 SMP Debian 5.10.179-5 (2023-08-08) x86_64 GNU/Linux
|
准备工作
安装 Supervisor
Supervisor 主要用于管理 Nginx 的开机自启动(带守护进程)。
1 2 3 4 5 6 7 8 9 10 11
| # yum install -y supervisor
# systemctl enable supervisord
# systemctl start supervisord
# systemctl status supervisord
|
创建 Nginx 用户和用户组
1 2 3 4 5 6 7 8
| $ sudo -i
# groupadd nginx
# useradd -g nginx nginx -s /bin/false
|
更改最大打开文件描述符数
Nginx 编译安装
下载文件
1 2 3 4 5 6 7 8 9
| # mkdir -p /home/nginx/software
# cd /home/nginx/software # wget http://nginx.org/download/nginx-1.16.0.tar.gz
# tar -xvf nginx-1.16.0.tar.gz
|
编译安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| # cd /home/nginx/software/nginx-1.16.0
# apt-get install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev
./configure \ --user=nginx \ --group=nginx \ --prefix=/usr/local/nginx \ --with-pcre \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_gzip_static_module \ --with-http_stub_status_module
# make && make install
# cd /usr/local/nginx/conf # cp nginx.conf nginx.conf.default
# chown -R nginx:nginx /usr/local/nginx
|
配置 Nginx
1 2 3 4 5 6 7
| # vim /usr/local/nginx/conf/nginx.conf worker_processes 4; error_log logs/error.log;
|
配置防火墙
1 2 3 4 5 6 7 8
| # firewall-cmd --zone=public --permanent --add-port=80/tcp
# firewall-cmd --reload
# firewall-cmd --list-ports
|
Nginx 服务管理
开机自启动 Nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| # touch /etc/supervisor/conf.d/nginx.conf
# vim /etc/supervisor/conf.d/nginx.conf [program:nginx] directory=/usr/local/nginx command=/usr/local/nginx/sbin/nginx -g 'daemon off;' -c /usr/local/nginx/conf/nginx.conf user=root numprocs=1 autostart=true autorestart=true startretries=10 process_name=%(program_name)s stdout_logfile_backups=5 stdout_logfile_maxbytes=10MB stdout_logfile=/var/log/supervisor/nginx.log stderr_logfile_backups=5 stderr_logfile_maxbytes=10MB stderr_logfile=/var/log/supervisor/nginx-error.log
# supervisorctl reload
# supervisorctl status nginx nginx RUNNING pid 9451, uptime 0:00:56
# curl -I -X GET 127.0.0.1:80
|
管理 Nginx 服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| # supervisorctl stop nginx
# supervisorctl start nginx
# supervisorctl restart nginx
# supervisorctl status nginx
|
Nginx 配置文件概述
1 2 3 4 5
| 安装目录:/usr/local/nginx 配置文件:/usr/local/nginx/conf/nginx.conf 错误日志:/usr/local/nginx/logs/error.log 访问日志:/usr/local/nginx/logs/access.log nginx的supervistor配置文件:/etc/supervisor/conf.d/nginx.conf
|
参考资料