环境
Linux ARM 64位
安装
1. 下载
> https://github.com/nginx/nginx/tags
> 选择 1.22.0版本源码
2. 将文件拷贝到服务器中
mkdir /opt/nginx
> /opt/nginx
3. 解压
cd /opt/nginx
# 将 nginx-release-1.22.0.tar.gz 拷贝进去
tar -zxvf nginx-release-1.22.0.tar.gz
4. 编译前准备
# 更新
yum -y update
# 需要使用https执行指令
# /opt/nginx/nginx/configure --with-http_ssl_module
# 不需要使用https执行
# /opt/nginx/nginx/configure
# 编译如果提示./configure: error: the HTTP rewrite module requires the PCRE library.,则执行yum -y install pcre-devel后重新编译
# 提示./configure: error: the HTTP gzip module requires the zlib library.,则执行yum install -y zlib-devel后重新编译
yum -y install pcre-devel
yum install -y zlib-devel
# error: SSL modules require the OpenSSL library
yum -y install openssl
yum -y install openssl-devel
5. 编译
cd /opt/nginx/nginx-release-1.22.0
./auto/configure
--prefix=/usr/local/nginx # 定义将保存服务器文件的目录
--sbin-path=/usr/local/nginx/nginx # 设置 nginx 可执行文件的名称
--conf-path=/usr/local/nginx/nginx.conf # 设置nginx.conf配置文件的名称
#--error-log-path=/usr/local/nginx/logs/error.log # 设置主要错误、警告和诊断文件的名称
--pid-path=/usr/local/nginx/nginx.pid # 设置nginx.pid将存储主进程的进程 ID 的文件的名称
--with-http_ssl_module # 允许构建一个将 HTTPS 协议支持添加 到 HTTP 服务器的模块
cd /opt/nginx/nginx-release-1.22.0
make && make install
6. 启动
# 启动
cd /usr/local/nginx
# 启动
./nginx
7. 停止服务
cd /usr/local/nginx/
# 关闭
./nginx -s quit
8. 开启防火墙端口
firewall-cmd --zone=public --add-port=3000/tcp --permanent
firewall-cmd --reload
9. 测试
curl http://127.0.0.1:3000
10. 设置开机启动
- 进入目录
cd /usr/lib/systemd/system
- 将
nginx.service
拷贝到对应的服务器上 - 重新加载服务
systemctl daemon-reload
- 设置为开机启动
systemctl enable nginx.service
- 查看是否已开机启动
systemctl is-enabled nginx.service
- 查看服务状态
systemctl status nginx.service
- 启用服务
systemctl enable nginx.service
- 启动服务
systemctl start nginx.service
- 停止服务
systemctl stop nginx.service
11. 部署
- 前端编译完成
- 将编译后的产物,放到
/usr/local/nginx/html/
中 - 配置nginx代理
/usr/local/nginx/nginx.config
- 添加配置 http.server.location /test { proxy_pass http://IP:8881/test; }
- 重新加载
/usr/local/nginx/nginx -s reload
附录
nginx.service
#
# /usr/lib/systemd/system
# systemd service file for Nginx Server forking server
#
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
User=root
Type=forking
PIDFile=/usr/local/nginx/nginx.pid
#Environment="JAVA_HOME=/opt/openjdk/jdk1.8.0_311" #改成自己的jdk路径,因为服务脚本的环境和系统环境变量不能共享,所以还得设置才能生效。
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /usr/local/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /usr/local/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
或者
#
# /usr/lib/systemd/system
# systemd service file for Nginx Server forking server
#
[Unit]
Description=Nginx Server
Documentation=Nginx 1.22.0
Documentation=https://rocketmq.apache.org/
Documentation=https://github.com/nginx/nginx
Documentation=https://nginx.org/en/docs/beginners_guide.html
After=network.target
[Service]
User=root
Type=forking
Environment="JAVA_HOME=/opt/openjdk/jdk1.8.0_311" #改成自己的jdk路径,因为服务脚本的环境和系统环境变量不能共享,所以还得设置才能生效。
ExecStart=/usr/local/nginx/nginx
ExecReload=/usr/local/nginx/nginx -s reload
ExecStop=/usr/local/nginx/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target