Nginx源码安装,配置开机自启

22 篇文章 2 订阅

背景

最近,全球都在制裁 NginxClickHouse 的诞生地。以前都是通过 yum 直接安装的 Nginx ,今天试试源码安装。

2022-03-06-NginxIndex.jpg

系统环境

CentOS7 上进行安装,虚拟主机信息如下:

[root@hadoop1 local]# uname -a
Linux hadoop1 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@hadoop1 local]# cat /proc/version
Linux version 3.10.0-1127.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue Mar 31 23:36:51 UTC 2020
[root@hadoop1 local]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)

下载解压

# 下载
[root@hadoop1 local]# wget http://nginx.org/download/nginx-1.20.1.tar.gz

# 解压
[root@hadoop1 local]# tar -xvf nginx-1.20.1.tar.gz

编译安装

[root@hadoop1 local]# cd nginx-1.20.1
[root@hadoop1 nginx-1.20.1]# ./configure
[root@hadoop1 nginx-1.20.1]# make
[root@hadoop1 nginx-1.20.1]# make install

# 配置环境变量
[root@hadoop1 nginx-1.20.1]# nginx -V
-bash: nginx: 未找到命令
[root@hadoop1 nginx-1.20.1]# vi /etc/profile
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin

# 刷新配置
[root@hadoop1 nginx-1.20.1]# source /etc/profile

[root@hadoop1 nginx-1.20.1]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
configure arguments:

Note:

  1. 如果在编译时报错缺少组件,可以先安装基础依赖:yum -y install gcc openssl openssl-devel pcre-devel zlib zlib-devel
  2. 如果要配置 HTTPS ,需要在编译时安装 SSL 模块,configure时附带参数:./configure --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/bin/openssl

启动验证

# 启动
[root@hadoop1 nginx-1.20.1]# nginx -c /usr/local/nginx/conf/nginx.conf

通过80端口访问,看 Nginx 是否启动成功。

2022-03-06-NginxHome.jpg

配置开机自启

# 这里是用源码编译安装的,所以需要手动创建nginx.service服务文件。
[root@hadoop1 nginx-1.20.1]# vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx service
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 quit
PrivateTmp=true 
   
[Install] 
WantedBy=multi-user.target

验证开机自启

[root@hadoop1 nginx-1.20.1]# systemctl list-unit-files | grep nginx
nginx.service                                 disabled
[root@hadoop1 nginx-1.20.1]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@hadoop1 nginx-1.20.1]# systemctl list-unit-files | grep nginx
nginx.service                                 enabled 

常用命令

# Nginx启停
systemctl start nginx.service          启动nginx服务
systemctl stop nginx.service           停止服务
systemctl restart nginx.service        重新启动服务
systemctl status nginx.service          查看服务状态
systemctl enable nginx.service          设置开机自启动
systemctl disable nginx.service         取消开机自启动

# 查看开机启动项
systemctl list-unit-files
systemctl list-unit-files | grep enabled
systemctl list-unit-files | grep nginx

Note:

  1. 修改完配置后,可通过nginx -t测试配置是否存在语法错误或者typo;
  2. 修改完配置后,记得通过nginx -s reload刷新使配置生效;

Reference

关于 Nginx ,我之前总结过各种掉进的坑以及如何跳出的解决方法。如果恰巧你也遇到了类似问题,那么很高兴能够为你节省点时间。

历史文章链接:


If you have any questions or any bugs are found, please feel free to contact me.

Your comments and suggestions are welcome!

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Heartsuit

别说什么鼓励,这就是互联网乞讨

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值