分享

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

落叶 发表于 2022-8-10 17:25:29 [显示全部楼层] 只看大图 回帖奖励 阅读模式 关闭右栏 0 1457
1.首先进入官网下载最新的稳定分支。
EMA0G)4E74_RLK()[@(P3CE.png

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

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


[mw_shl_code=bash,true]tar -xzvf nginx-1.22.0.tar.gz
cd nginx-1.22.0[/mw_shl_code]

QQ截图20220810163216.png


3.配置编译环境
[mw_shl_code=bash,true]yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel

[/mw_shl_code]
[mw_shl_code=bash,true]./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[/mw_shl_code]

如果出现这种报错,源码编译安装最新的openssl, 然后重新在上面的命令后面加上一条--with-openssl=你openssl的源码路径
1660122488744.jpg
[mw_shl_code=bash,true]
./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[/mw_shl_code]

4.编译安装


[mw_shl_code=bash,true]make && make install[/mw_shl_code]
QQ截图20220810171403.png

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

[Service]                                                                                 
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                                                                  

[Install]
WantedBy=multi-user.target [/mw_shl_code]
保存之后重载Ststemctl命令:
[mw_shl_code=bash,true]systemctl daemon-reload[/mw_shl_code]

6.设置自启动
[mw_shl_code=bash,true]systemctl enable nginx[/mw_shl_code]

7.命令
[mw_shl_code=bash,true]systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx[/mw_shl_code]

Felix

2022年8月10日 17:15

回复

使用道具 举报

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则