CentOS 编译安装 nginx

CentOS 编译安装 nginx

修改 yum 源地址为 阿里云

bash 复制代码
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
bash 复制代码
yum makecache

升级内核和软件

bash 复制代码
yum -y update

安装常用软件和依赖

bash 复制代码
yum -y install gcc gcc-c++ make cmake zlib zlib-devel openssl openssl-devel pcre-devel vim net-tools man wget tar

创建用户用户组

bash 复制代码
groupadd nginx
useradd nginx -g nginx -s /sbin/nologin -M

下载并编译安装

下载

bash 复制代码
wget https://nginx.org/download/nginx-1.24.0.tar.gz

解压

bash 复制代码
tar -zxvf nginx-1.24.0.tar.gz

进入nginx源代码目录

bash 复制代码
cd nginx-1.24.0

预编译

bash 复制代码
./configure --prefix=/usr/local/nginx \
 --user=nginx --group=nginx \
 --with-http_stub_status_module --with-http_ssl_module \
 --with-http_realip_module --with-http_gzip_static_module \
 --with-file-aio --with-http_realip_module  

验证 是否 成功

bash 复制代码
echo $?

编译

bash 复制代码
make -j4 

验证 是否 成功

bash 复制代码
echo $?

安装

bash 复制代码
make -j4 install

验证 是否 成功

bash 复制代码
echo $?

ls 以下为验证程序步骤可忽略

bash 复制代码
/usr/local/nginx/sbin/nginx -v

/usr/local/nginx/sbin/nginx -t

# 启动nginx
/usr/local/nginx/sbin/nginx

# 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reload  

# 快速关闭 Nginx
/usr/local/nginx/sbin/nginx -s stop

# 关闭Nginx
/usr/local/nginx/sbin/nginx -s quit                  

编写启动脚本

bash 复制代码
cd /usr/lib/systemd/system/
vim nginx.service
bash 复制代码
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target
bash 复制代码
# systemctl start|stop|reload|restart|status nginx.service

# 重新加载服务文件
systemctl daemon-reload 

# 启动 nginx 服务
systemctl start nginx.service

# 开机自启:
systemctl enable nginx.service

# 关闭开机自启:
systemctl disable nginx.service

防火墙开放端口

--zone 作用域

--add-port=80/tcp 添加端口

--permanent 永久生效

bash 复制代码
firewall-cmd --zone=public --add-port=80/tcp --permanent
bash 复制代码
firewall-cmd --reload

浏览器访问测试

http://服务器IP地址

相关推荐
用户46043852250861 小时前
关于paimon-rust 数据写入链路
大数据
飞奔的瓜牛2 小时前
Nacos 三节点集群部署手册:MySQL 持久化 + Nginx 负载均衡
mysql·nginx·负载均衡
数字融合3 小时前
透明化视频驱动化工生产动态三维重建管控技术
大数据·人工智能·python
2601_967980114 小时前
Python 与云计算:AWS、GCP、Azure 实践
python·云计算·azure·aws·gcp
演艺大赏4 小时前
两个 NameNode 一起起不来以及Hadoop 3.3.2 里那个认不出自家 JournalNode 的 Servlet
大数据·hadoop
知行产研4 小时前
会感知、能协同,金科【钻孔】智能化路线 | bauma CHINA 2026
大数据·人工智能
无奈笑天下4 小时前
银河麒麟桌面操作系统上磁盘无法自动挂载问题处理
linux·运维·服务器
木头左4 小时前
【Linux运维】服务器磁盘清理与Docker日志治理实战:一次巡检释放70G空间
linux·docker·磁盘清理·日志治理
演艺大赏4 小时前
RESTServer 起不来,日志只有一行:headerCacheSize 65536 exceeds maximum value of 65534
大数据·hadoop·hbase