ubuntu nginx源码编译升级

一、在安装之前,请确认已经安装以下依赖包:

  1. epoll,linux内核版本为2.6或者以上

  2. gcc编译器,g++编译器

  3. pcre库,函数库,支持解析正则表达式

  4. zlib库:压缩解压功能

  5. openssl库:ssl功能相关库,用于网站加密通讯

二、下载以及安装

  1. 官网地址,http://www.nginx.org

  2. 下载地址(以nginx-1.18.0为例),http://nginx.org/download/nginx-1.18.0.tar.gz

  3. nginx的版本介绍:

  • mianline版本,版本号中间数字一般为奇数,更新快,一个月就会发布一个新版本,最新功能,bug修复等,稳定性差点。

  • stable版本:稳定版,版本号中间数字一般为偶数。经过了长时间的测试,比较稳定,商业化环境中使用这种版本。

  • Lengacy版本,遗产,遗留版本,以往的老版本。

  1. 编译参数介绍:
  • --prefix:指定最终安装到的目录 默认值 /usr/local/ngnix

  • --sbin-path:用来指定可执行文件目录:默认的是 sbin/nginx

  • --conf-path:用来指定配置文件目录:默认的是 conf/nginx.conf

  1. 编译过程以及代码
apt update

apt install libxslt-dev libpcre3-dev zlib1g-dev libssl-dev libxml2-dev libxslt1-dev  libgd-dev google-perftools libgoogle-perftools-dev libperl-dev libgeoip-dev

wget http://nginx.org/download/nginx-1.20.2.tar.gz

tar xzf nginx-1.20.2.tar.gz

cd nginx-1.20.2 

 ./configure --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --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 --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads

make && make install

注:编译参数可根据实际情况而定

三、启动以及重启

  1. 建立软链接
    ln -s /usr/share/nginx/sbin/nginx /usr/sbin/nginx
  2. service nginx restart/start/stop
  3. 建立nginx服务
    touch /lib/systemd/system/nginx.service
    vim /lib/systemd/system/nginx.service
    复制内容:
# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

你可能感兴趣的:(ubuntu nginx源码编译升级)