01.15 Centos7编译安装nginx并设置开机自启教程

nginx社区版官网 www.nginx.org

Centos7编译安装nginx并设置开机自启教程


  • 安装nignx的依赖库

yum install -y install pcre-devel zlib-devel openssl-devel gcc*

  • 下载nginx源码包

wget -c https://nginx.org/download/nginx-1.16.1.tar.gz

  • 编写nginx安装脚本

vim nginx.sh

<code>#!/bin/bash
tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
useradd -r -s /sbin/nolgoin www
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
make&& make install/<code>
  • 执行脚本,一键安装nginx

sh nginx.sh

  • 启动nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

访问IP 检查nginx启动情况,如果无法访问则端口占用或者防护墙没关


Centos7编译安装nginx并设置开机自启教程

  • 手动关闭nginx服务

/usr/local/nginx/sbin/nginx -s stop

  • 注册nginx服务

vim /usr/lib/systemd/system/nginx.service

<code>[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

#Type=forking 代表后台运行/<code>

此时服务已经注册完成,可以通过以下命令完成nginx的启动,停止,重载等。

systemctl start nignx

systemctl enable nginx

systemctl stop nginx

systemctl status nginx

systemctl reload nginx

Centos7编译安装nginx并设置开机自启教程

以上介绍完nginx的编译安装和注册服务过程,我是元元,目前就职系统运维工程师,编写文档只为记录及分享自己的学习成果,文笔欠佳还请看官多多谅解,如果不明白之处可留言,一起讨论学习。


分享到:


相關文章: