编译安装nginx并使用systemctl方式管理

分为两个大部分:
第一部分:编译安装nginx

01 安装gcc编译器以及编译安装的依赖

yum -y install gcc gcc-c++ wget  openssl openssl-devel

02 进入src目录

cd /usr/local/src/

03 使用wget方式下载源码包

wget http://nginx.org/download/nginx-1.14.0.tar.gz

04 解压并进入目录开始编译三部曲

tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0/
./configure --prefix=/usr/local/nginx --with-http_ssl_module
make &&  make install

05 检查:返回值为0则是执行成功

echo  $? 

06 启动nginx并查看

nginx
ps -ef |grep nginx

第二部分:systemctl方式管理

01 创建配置文件
源码安装的nginx在/etc/systemd/system/multi-user.target.wants/目录下是没有nginx.service这个文件的,所以要新建

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

02 写入内容:

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

03 设置开机启动

systemctl enable nginx.service

04 关闭之前启动的nginx(编译完成后nginx启动的nginx服务程序)

pkill    -9  nginx

05 重载nginx配置文件

systemctl daemon-reload

06 重新启动nginx服务

systemctl start nginx

查看nginx服务运行状态

[root@localhost conf]# systemctl status  nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2020-02-09 09:32:55 EST; 4min 11s ago
     Docs: http://nginx.org/en/docs/
  Process: 7564 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 7565 (nginx)
   CGroup: /system.slice/nginx.service
           ├─7565 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           └─7566 nginx: worker process

Feb 09 09:32:55 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
Feb 09 09:32:55 localhost.localdomain systemd[1]: PID 7342 read from file /usr/local/nginx/logs/nginx.pid does not exist ...mbie.
Feb 09 09:32:55 localhost.localdomain systemd[1]: Started nginx - high performance web server.
Hint: Some lines were ellipsized, use -l to show in full.

完全没毛病

发布了170 篇原创文章 · 获赞 15 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_42506599/article/details/104241721