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

系统版本

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

CentOS Linux release 7.5.1804 (Core)

安装nginx

安装依赖


 
 
  1. [root@n1 ~] # yum -y install gcc gcc-c++
  2. [root@n1 ~] # yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
  3. gcc gcc-c++编译环境
  4. gzip 模块需要 zlib 库
  5. rewrite 模块需要 pcre 库
  6. ssl 功能需要openssl库

下载安装包


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

添加nginx组,用户


 
 
  1. [root@n1 nginx- 1.14. 2] # groupadd nginx
  2. [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

编译安装


 
 
  1. [root@n1 nginx- 1.14. 2] # make && make install
  2. [root@n1 nginx- 1.14. 2] # echo $?
  3. 0

查看nginx版本号


 
 
  1. [root@n1 ~] # /usr/local/nginx/sbin/nginx -v
  2. nginx version: nginx/ 1.14. 2

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


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

启动nginx


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

 查看端口

图片.png

客户端访问:

图片.png

编写启动脚本

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


 
 
  1. [Unit]
  2. Description=nginx
  3. After=network.target remote-fs.target nss-lookup.target
  4. [Service]
  5. Type=forking
  6. PIDFile=/usr/ local/nginx/logs/nginx.pid
  7. ExecStartPost=/bin/sleep 0.1
  8. ExecStartPre=/usr/ local/nginx/sbin/nginx -t -c /usr/ local/nginx/conf/nginx.conf
  9. ExecStart=/usr/ local/nginx/sbin/nginx -c /usr/ local/nginx/conf/nginx.conf
  10. ExecReload=/bin/ kill -s HUP $MAINPID
  11. ExecStop=/bin/ kill -s QUIT $MAINPID
  12. PrivateTmp= true
  13. [Install]
  14. WantedBy=multi-user.target

使用:


 
 
  1. systemctl start| stop|reload|restart| status nginx.service
  2. 开机自启:
  3. systemctl enable nginx.service
  4. 关闭开机自启:
  5. systemctl disable nginx.service

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值