CentOS7编译安装nginx及添加到系统服务

系统环境

系统版本

[root@n1 ~]# cat /etc/redhat-release

CentOS Linux release 7.5.1804 (Core)

安装nginx

安装依赖

[root@n1 ~]# yum -y install gcc gcc-c++
[root@n1 ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

gcc gcc-c++编译环境
gzip    模块需要 zlib 库
rewrite 模块需要 pcre 库
ssl     功能需要openssl库

下载安装包

[root@n1 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@n1 ~]# tar xf nginx-1.14.2.tar.gz
[root@n1 ~]# cd nginx-1.14.2/

添加nginx组,用户

[root@n1 nginx-1.14.2]# groupadd nginx
[root@n1 nginx-1.14.2]# useradd nginx -g nginx -s /sbin/nologin -M

对nginx进行配置

[root@n1 nginx-1.14.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module

#查看是否配置成功($? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误)

[root@n1 nginx-1.14.2]# echo $?

0

编译安装

[root@n1 nginx-1.14.2]# make && make install
[root@n1 nginx-1.14.2]# echo $?
0

查看nginx版本号

[root@n1 ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.14.2

检查配置文件语法是否正确

[root@n1 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动nginx

[root@n1 ~]# /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reload                 重新载入配置文件
/usr/local/nginx/sbin/nginx -s stop                   快速关闭 Nginx
/usr/local/nginx/sbin/nginx -s quit                   关闭Nginx

 查看端口

图片.png

客户端访问:

图片.png

编写启动脚本

[root@n1 ~]# vim  /usr/lib/systemd/system/nginx.service

[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

使用:

systemctl start|stop|reload|restart|status nginx.service
开机自启:
systemctl enable nginx.service
关闭开机自启:
systemctl disable nginx.service

 

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

real向往

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值