centos部署nginx服务
centos部署nginx服务
本文基于centos7部署nginx -1.26.1
1.下载包
官网下载nginx: download
下载的包名为nginx-1.26.1.tar.gz,上传到服务器上
2.安装
2.1 安装依赖
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
2.2 解压nginx
tar –xvf nginx-1.26.1.tar.gz
2.3 安装nginx
# 进入解压后的文件
cd nginx-1.26.1
# 检查
./configure
# 编译
make
# 安装
make install
结束后,nginx就被安装在/usr/local/nginx路径下了,
3.启动nginx
# 进入nginx安装目录
cd /usr/local/nginx/sbin/
# 启动nginx服务
./nginx
用ps -aux | grep nginx
查看nginx是否运行,出现下例证明成功运行
[root@localhost nginx-1.26.1]# ps -aux | grep nginx
root 4027 0.0 0.0 112812 972 pts/0 S+ 10:50 0:00 grep --color=auto nginx
root 14367 0.0 0.0 20724 1348 ? Ss 10:22 0:00 nginx: master process ./nginx
nobody 15692 0.0 0.0 21148 1428 ? S 10:25 0:00 nginx: worker process
nobody 15693 0.0 0.0 21148 1428 ? S 10:25 0:00 nginx: worker process
在浏览器的地址栏输入centos的ip就可以看到nginx的欢迎界面了
⚠️ 有时访问nginx会不成功,可能是因为防火墙,关闭centos的防火墙(生产环境不建议这样做)
systemctl stop firewalld.service
其他nginx命令
# 停止服务 ./nginx -s stop # 重载配置 ./nginx -s reload
将nginx配置为系统服务
在 /etc/systemd/system/
目录下创建一个新的服务文件 nginx.service
,授权 chmod +x nginx.service
,打开此文件并填入下述代码
[Unit]
Description=Nginx HTTP Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重载systemd文件
systemctl daemon-reload
命令
# 启动nginx服务
systemctl start nginx
# 停止服务
systemctl stop nginx
# 查看状态
systemctl status nginx
查看状态(systemctl status nginx)为如下则成功
[root@localhost sbin]# systemctl status nginx
● nginx.service - Nginx HTTP Server
Loaded: loaded (/etc/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2024-06-17 11:20:03 EDT; 8s ago
Process: 14896 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 14897 (nginx)
CGroup: /system.slice/nginx.service
├─14897 nginx: master process /usr/local/nginx/sbin/nginx
└─14898 nginx: worker process
Jun 17 11:20:03 localhost.localdomain systemd[1]: Starting Nginx HTTP Server...
Jun 17 11:20:03 localhost.localdomain systemd[1]: Started Nginx HTTP Server.
如果出现了
[root@localhost sbin]# systemctl status nginx
● nginx.service - Nginx HTTP Server
Loaded: loaded (/etc/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2024-06-17 11:12:42 EDT; 6min ago
Process: 12211 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=1/FAILURE)
Jun 17 11:12:39 localhost.localdomain nginx[12211]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jun 17 11:12:40 localhost.localdomain nginx[12211]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jun 17 11:12:40 localhost.localdomain nginx[12211]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jun 17 11:12:41 localhost.localdomain nginx[12211]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jun 17 11:12:41 localhost.localdomain nginx[12211]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jun 17 11:12:42 localhost.localdomain nginx[12211]: nginx: [emerg] still could not bind()
Jun 17 11:12:42 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
Jun 17 11:12:42 localhost.localdomain systemd[1]: Failed to start Nginx HTTP Server.
Jun 17 11:12:42 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
Jun 17 11:12:42 localhost.localdomain systemd[1]: nginx.service failed.
用命令lsof -i :80
查看端口被谁占用,然后用kill 进程ID
命令杀进程
设置nginx开机自启动
systemctl enable nginx
本文来自博客园,作者:勤匠,转载请注明原文链接:https://www.cnblogs.com/JarryShu/articles/18253439
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现