Liunx-CentOS安装Nginx

0 卸载Nginx

# 查看nginx是否运行
ps -ef | grep nginx
# 停止用stop、或者用kill
/usr/local/nginx/sbin/nginx -s stop
# 查询Nginx安装的文件
find / -name nginx
# 删除Nginx的相关文件
rm -rf /usr/local/nginx
... 全部删除 ...

如果设置了Nginx开机自启动的话,可能还需要下面两步

chkconfig nginx off
rm -rf /etc/init.d/nginx

可以再用yum指令清理

yum remove nginx

1 Ninx安装包

官方下载https://nginx.org/en/download.html
上传服务器

2 安装依赖

gcc

yum install -y gcc

perl库

yum install -y pcre pcre-devel

zlib库

yum install -y zlib zlib-devel

openssl

yum install -y penssl openssl-devel

或者直接一条命令yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

3 安装Nginx

# 解压
tar -zxvf nginx-1.22.0.tar.gz
# 进入解压目录
cd nginx-1.22.0
# 开始编译安装
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-stream
make
make install

4 访问测试

localhost出现nginx页面则成功
可以删除源码目录
到此安装完毕

5 conf.d

# 新建
mkdir conf.d
chmod 666 conf.d
...

在conf.d中添加server

server {
listen 8009;
server_name localhost;
root html;
index index.html index.htm;
location ^~ /w-server/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8008/w-server/;
}
location ^~ /w-admin/ {
alias /mnt/wlt/html/dist/;
index index.html;
try_files $uri $uri/=404 /index.html last;
}
}

原配置文件http{}中添加

include /usr/local/nginx/conf.d/*.conf;

重启Nginx

/usr/local/nginx/sbin/nginx -s reload

6 设置服务

创建 nginx.service 文件
vim /etc/systemd/system/nginx.service

[Unit]
Description=nginx service
After=network.target
[Service]
User=root
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecStartPre=/bin/sleep 10
[Install]
WantedBy=multi-user.target
# 确保 systemd 重新加载新的服务文件
systemctl daemon-reload
# 启动 Nginx 并设置开机启动
systemctl start nginx
systemctl enable nginx

环境变量

vim /etc/profile
#Nginx enviroment
export NGINX_HOME=/usr/local/environment/nginx1.22
export PATH=$NGINX_HOME/sbin:$PATH
source /etc/profile
nginx -v

7 相关命令

# 检擦配置文件是否正确
nginx -t
# 重新载入配置文件
nginx -s reload
# 重启 Nginx
nginx -s reopen
# 停止 Nginx
nginx -s stop
# 设置nginx文件上传大小限制
client_max_body_size 200M;
client_body_buffer_size 50M;
fastcgi_intercept_errors on;

image

posted @   生生灯火半杯月  阅读(100)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示