Nginx的安装及升级

一、编辑源仓库文件

1.官方网站查询方法

http://nginx.org,选择右侧:“documentation”,进入后点击上方:“Installing nginx”,新页面选择“Installation on Linux”中的“For Linux, nginx packages from nginx.org can be used”,点击最终页面中的“RHEL/CentOS”,网址为:

http://nginx.org/en/linux_packages.html#RHEL-CentOS

2. 建仓库源文件( yum repository), 内容如下:

vim /etc/yum.repos.d/nginx.repo

输入以下内容。 

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

3.切换使用mainline版本,禁用stable版本

yum-config-manager --enable nginx-mainline
yum-config-manager --disable nginx-stable

二、安装nginx

yum install -y nginx

三、启动Nginx并设置开机自动运行

1. 启动服务

systemctl start nginx.service # 启动服务
systemctl status nginx.service # 查看服务状态

2.设置开机运行

systemctl enable nginx.service

将Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

注:/usr/lib/systemd/system/的.service文件都是自启动的。

3.其他命令

# 关闭服务

systemctl stop nginx.service

# 重启服务

systemctl restart nginx.service

四、查看进程

#查看nginx服务
ps -ef | grep nginx 
ps aux | grep nginx
#查看服务端口情况
netstat -tunlp

五、升级版本

 编译nginx源代码包,可平滑升级版本。

1.安装依赖程序

yum install pcre-devel libxml2-devel libxslt-devel gd-devel gperftools-devel perl-devel perl-ExtUtils-Embed

点击链接,查看 openssl安装教程

按照第一点内容事先安装好openssl3.0以上版本的编译环境(1.1.1不需要),再安装第二点内容下载解压openssl源代码包。

2.下载解压源代码包

进入官方网站http://nginx.org,点击右侧的“download”,进入下载界面,选择“Stable version”中的最新版本,当前稳定版的版本为1.22.0。

wget http://nginx.org/download/nginx-1.22.0.tar.gz
tar -zxvf nginx-1.22.0.tar.gz

3.查询当前版本nginx的原编译参数

nginx -V

复制 configure arguments:后面的参数,作为下一步的编译参数

 [root@VM-0-6-centos ~]# nginx -V
nginx version: nginx/1.23.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/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 --user=nginx --group=nginx --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-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

 在原编译参数后添加以下参数,其中--with-openssl后面指定openssl的源代码目录,--with-cc后面指定当前系统内最新的gcc可执行文件,不指定则使用旧版本gcc进行编译。

--with-openssl=/root/openssl-3.0.5 --with-cc=/usr/local/gcc-11.2.0/bin/gcc

4.停止nginx服务

systemctl stop nginx.service

5.编译

cd nginx-1.22.0
/configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/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 --user=nginx --group=nginx --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-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-openssl=/root/openssl-3.0.5 --with-cc=/usr/local/gcc-11.2.0/bin/gcc
make -j2
systemctl stop nginx.service
make install
cd ..

6.查看版本号

systemctl start nginx.service
nginx -V

[root@VM-0-15-centos ~]# nginx -V
nginx version: nginx/1.22.0
built by gcc 11.2.0 (GCC) 
built with OpenSSL 3.0.5 5 Jul 2022
TLS SNI support enabled

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值