首先访问nginx的官网,获取最新版本的nginx源码,然后下载到无服务器。
地址地址:http://nginx.org/en/download.html
选择最新版本,以我的为例当时最新版地址是:
http://nginx.org/download/nginx-1.23.2.tar.gz
国外服务器网速慢,使用国内镜像地址:
https://mirrors.huaweicloud.com/nginx/nginx-1.23.2.tar.gz
通过执行 wget 下载
cd /opt
mkdir software
wget https://mirrors.huaweicloud.com/nginx/nginx-1.23.2.tar.gz
下载完成之后,进行安装,步骤如下
# 解压
tar xvzf nginx-1.23.2.tar.gz
cd nginx
# 安装ssl支持
yum install openssl-devel
# 编译
./configure \
--with-http_ssl_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-http_slice_module \
--with-file-aio \
--with-http_v2_module
# 检查没有错误之后,执行安装
make -j 4
make install
# 将nginx可执行命令软连接到/usr/local/bin
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
# 查看安装的nginx版本信息
nginx -V
安装完成之后,默认的配置路径在 /usr/local/nginx
将Nginx配置为Systemd的服务,新增 nginx.service 文件
vi /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
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
保存好nginx.service文件之后,需要让systemd重载配置
systemctl daemon-reload
执行启动/停止/重启/查看状态
# 查看状态
systemctl status nginx
# 停止
systemctl stop nginx
# 启动
systemctl start nginx
# 重启
systemctl restart nginx
开机启动
systemctl enable nginx