Centos7上手动编译安装 Nginx1.16

安装依赖

yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel

下载 Nginx

cd /usr/local/src/
wget -cO nginx-1.16.0.tar.gz https://nginx.org/download/nginx-1.16.0.tar.gz
tar -zxvf nginx-1.16.0.tar.gz
cd nginx-1.16.0/

创建用户和组

groupadd nginx
useradd nginx -g nginx -s /sbin/nologin -M

创建用户和组

./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_mp4_module  \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre

编译安装

make && make install

注意:启动时,如果 mkdir /var/tmp/nginx/client 报错,请手动创建改目录结构。mkdir /var/tmp/nginx/client -p

/usr/local/nginx/sbin/nginx            # 启动
/usr/local/nginx/sbin/nginx -s reload  # 重启
/usr/local/nginx/sbin/nginx -s stop    # 停止

建立服务

 

1、在系统服务目录里创建nginx.service文件

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

2、写入内容如下:

[Unit]
Description=nginx
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

Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

3、设置开机自启动
systemctl enable nginx.service

4、查看nginx状态
systemctl status nginx.service

很奇怪,明明启动成功了,为什么显示Active: inactive (dead)?

5、杀死nginx重启nginx

pkill -9 nginx
ps aux | grep nginx
systemctl start nginx

再次查看状态,变成了active,搞定。

6、重启服务器
reboot
7、再次连接后,查看服务状态
systemctl status nginx.service

Nginx管理命令

服务 启动

systemctl start nginx

服务 停止

systemctl stop nginx

无间断服务重启操作

systemctl reload nginx
恐龙先生

作者:

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Demon_运维笔记