centos7编译安装nginx

1.安装gcc、gcc-c++编译器

yum -y install gcc gcc-c++

2.查看80端口占用情况

netstat -tunlp 

使用kill pid结束掉占用80端口的进程

3.分析nginx服务默认安装目录

yum install nginx
systemctl status nginx

查到的服务路径:/etc/systemd/system/multi-user.target.wants/nginx.service, 复制此文件内的全部文本备用,我们自定义服务时创建路径与此相同的服务

yum remove nginx

4.编译安装nginx
创建根目录env

cd /
mkdir env
cd /env

下载稳定版nginx源文件

wget http://nginx.org/download/nginx-1.20.2.tar.gz
tar zxvf  nginx-1.20.2.tar.gz

参考http://nginx.org/en/docs/configure.html

./configure --prefix=/env/nginx/1.20.2
make & make install
make clean
cd /env/nginx/1.20.2

目录说明
conf -----配置文件
html -----网页文件
logs -----日志文件
sbin -----主要二进制程序

5.测试nginx

cd /env/nginx/1.20.2
./sbin/nginx

浏览网页显示如下,则nginx工作正常

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.

关闭nginx

./sbin/nginx -s stop

6.systemctl添加自定义nginx服务

vim /etc/systemd/system/multi-user.target.wants/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/env/nginx/1.20.2/logs/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/bin/rm -f /env/nginx/1.20.2/logs/nginx.pid
ExecStartPre=/env/nginx/1.20.2/sbin/nginx -t
ExecStart=/env/nginx/1.20.2/sbin/nginx
ExecReload=/env/nginx/1.20.2/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

添加权限

chmod 754  /etc/systemd/system/multi-user.target.wants/nginx.service

7.启动nginx服务

systemctl daemon-reload
systemctl status nginx 
systemctl start nginx

8.将nginx添加到环境变量PATH

echo 'export PATH=$PATH:/env/nginx/1.20.2/sbin' > /etc/profile.d/nginx.sh
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值