一.获取源码
从Index of /download/ 网址获取要安装的版本的源码。
二.准备编译环境
1. 安装pcre库
PCRE是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
sudo apt install libpcre3 libpcre3-dev
2. 安装zlib库
lib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
sudo apt install zlib1g zlib1g-dev
3.安装OpenSSL库 (如果系统里已经安装过,则可以不用在安装了)
penSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
sudo apt install openssl libssl-dev
三. 安装
1. 解压nginx-1.26.0.tar.gz
tar zxvf nginx-1.26.0.tar.gz
2. configure配置编译参数
其实在 nginx-1.26.0.版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。
1.使用默认配置+自定义路径(没有特殊要求,使用这个)
./configure --prefix=/usr/local/nginx
2.自定义配置
根据你自己的需要向二进制文件中添加你所需要的模块,比如我添加的为代理启用ssl模块等等,完整的可启用模块清单使用以下命令查看
./configure --help
以下可以作为参考,具体根据个人需要可以自定义。
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_geoip_module --with-http_stub_status_module
如果这一步报错请仔细查看错误日志,大概率是缺少某个依赖库无法编译,只需使用apt安装对应依赖库即可。
这里例举了一些常用模块对应的库:
module lib
the GeoIP module requires the GeoIP library libgeoip-dev
the HTTP image filter module requires the GD library libgd-dev
the HTTP XSLT module requires the libxml2/libxslt libxslt-dev
the HTTP gzip module requires the zlib library zlib1g-dev
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/HitMan_4843/article/details/138112454
3 .编译安装
使用如下命令完成二进制文件的编译,接着安装二进制文件和配置文件
make
make install
4.启动
编译,安装完成后使用如下命令检查是否安装成功,第二条命令用于启动nginx服务(进入到安装目录的sbin目录里)
nginx -V
nginx -c /etc/nginx/nginx.conf
5.使用systemctl管理nginx服务
使用源码安装的nginx没有service文件,需要自行添加并将其注册到系统服务中
使用如下命令新建文件并编辑
vim /usr/lib/systemd/system/nginx.service
写入如下内容:
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
重装载服务,设置nginx自启动,重新启动nginx
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl restart nginx
还可以使用如下命令检查nginx是否运行正常:
sudo systemctl status nginx