1、下载nginx相关依赖包
sudo apt-get install gccsudo apt-get install libpcre3 libpcre3-devsudo apt-get install zlib1g zlib1g-devsudo sudo apt-get install opensslsudo apt-get install libssl-devsudo apt install make
2、进入想下载安装包位置
cd /usr/local
3、通过wget下载nginx安装包
sudo wget https://nginx.org/download/nginx-1.24.0.tar.gz
4、解压nginx的安装包
sudo tar -zvxf nginx-1.24.0.tar.gz
5、进入解压后的包
cd nginx-1.24.0
6、配置自己所需要的模块
sudo ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
7、编译
sudo make
8、安装
sudo make install
9、启动
cd /usr/local/nginx/sbin #打开文件夹sudo ./nginx #启动nginxsudo ./nginx -s stop # 强制停止 Nginxsudo ./nginx -s quit # 优雅停止 Nginx,会等待所有请求处理完成后再关闭
sudo ./nginx -s reload # 重新加载 Nginx 配置,而不会中断现有的连接
sudo ./nginx -t # 检查配置文件是否有语法错误sudo ./nginx -v # 显示 Nginx 的版本号
10、创建开机自启动
创建或编辑 nginx.service 文件,目录在/etc/systemd/system/nginx.service
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=/bin/kill -s TERM $MAINPID[Install]
WantedBy=multi-user.target
12、重新加载 systemd 守护进程
sudo systemctl daemon-reload
13、启用并启动 Nginx 服务
sudo systemctl enable nginx # 设置开机自启动sudo systemctl start nginx # 启动 Nginx 服务