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地址

相关推荐
Michelle80231 分钟前
23大数据 数据挖掘复习1
大数据·人工智能·数据挖掘
ayaya_mana7 分钟前
Linux环境下Node.js任意版本安装与pnpm、yarn包管理
linux·node.js·vim
IT学长编程9 分钟前
计算机毕业设计 基于k-means的校园美食推荐系统 Python 大数据毕业设计 Hadoop毕业设计选题【附源码+文档报告+安装调试】
大数据·python·毕业设计·kmeans·课程设计·毕业论文·美食推荐系统
乌龙玛奇朵51910 分钟前
Finalshell建立连接
linux
Serverless 社区43 分钟前
阿里云函数计算 AgentRun 全新发布,构筑智能体时代的基础设施
人工智能·阿里云·云原生·serverless·云计算
Maple_land1 小时前
Linux进程第八讲——进程状态全景解析(二):从阻塞到消亡的完整生命周期
linux·运维·服务器·c++·centos
嵌入式分享1 小时前
嵌入式分享#41:RK3576改UART波特率【精简版】
linux·嵌入式硬件·ubuntu·嵌入式
爱吃生蚝的于勒1 小时前
【Linux】零基础学会Linux之权限
linux·运维·服务器·数据结构·git·算法·github
惜.己1 小时前
linux中jenkins正常启动外部无法访问
linux·servlet·jenkins