落叶 发表于 2022-8-10 17:25:29

【编译安装】Nginx linux编译安装

1.首先进入官网下载最新的稳定分支。


2.下载后上传到服务器并解压,进入解压的文件夹。

这里对解压完成后的部分目录和文件做个简单的介绍:

[*]src 该目录存放了Nginx的所有源码;
[*]man 该目录存放了Nginx的帮助文档;
[*]html 该目录存放了两个html文件。这两个文件与Nginx服务器的运行相关,这两个文件的作用会在下文给出,这里不做赘述;
[*]conf 该目录存放的是Nginx服务器的配置文件,包含Nginx服务器的基本配置文件;
[*]auto 该目录存放了大量脚本文件,和configure脚本程序有关;
[*]configure 该文件是Nginx软件的自动脚本程序。运行configure脚本一般会完成两项工作:一是检查环境,根据环境检查结果生成C代码;二是生成编译代码需要的Makefile文件。


tar -xzvf nginx-1.22.0.tar.gz
cd nginx-1.22.0




3.配置编译环境
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel


./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --with-pcre-jit --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-stream --with-stream_ssl_module

如果出现这种报错,请源码编译安装最新的openssl, 然后重新在上面的命令后面加上一条--with-openssl=你openssl的源码路径:


./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --with-pcre-jit --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/opt/openssl-1.1.1q

4.编译安装


make && make install


5.配置nginx服务启动。
去到/usr/lib/systemd/system/目录新建一个nginx服务,给予执行权限:
vim /usr/lib/systemd/system/nginx.service
chmod +x /usr/lib/systemd/system/nginx.service
打开文件nginx.service新建内容:
                                                                                    
Description=nginx - high performance web server            
After=network.target remote-fs.target nss-lookup.target   

                                                                                 
Type=forking                                                                        
PIDFile=/usr/local/nginx/logs/nginx.pid                              
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf   
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf         
ExecReload=/usr/local/nginx/sbin/nginx -s reload                                                
ExecStop=/usr/local/nginx/sbin/nginx -s stop                                                      
ExecQuit=/usr/local/nginx/sbin/nginx -s quit                                                      
PrivateTmp=true                                                                  


WantedBy=multi-user.target
保存之后重载Ststemctl命令:
systemctl daemon-reload

6.设置自启动
systemctl enable nginx

7.命令
systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx

Felix
2022年8月10日 17:15
页: [1]
查看完整版本: 【编译安装】Nginx linux编译安装