[乐意黎]Centos安装nginx1.18后, 将nginx制作成服务设置为自启动

Nginx 1.18 下载地址 http://nginx.org/download/nginx-1.18.0.tar.gz

创建 nginx 用户

创建 Nginx 运行使用的用户  nginx:

[root@aerchi]# /usr/sbin/groupadd nginx
[root@aechi]# /usr/sbin/useradd -g nginx nginx

Nginx 1.18 安装的配置文件: 

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-http_realip_module \
--with-pcre

 安装完成后的显示:

....
....
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

编译和安装 

make && make install

设置 /usr/local/nginx/conf/nginx.conf, 部分如下:

 #user  nobody;
 user nginx nginx;
 worker_processes  1;

 #crit
 error_log logs/error.log crit;
 #error_log  logs/error.log;
 #error_log  logs/error.log  notice;
 #error_log  logs/error.log  info;

 pid        /var/run/nginx.pid;


 events {
     worker_connections  1024;
 }


 http {
     include       mime.types;
     default_type  application/octet-stream;

     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
     #                  '$status $body_bytes_sent "$http_referer" '
     #                  '"$http_user_agent" "$http_x_forwarded_for"';

     #access_log  logs/access.log  main;

     sendfile        on;
     #tcp_nopush     on;

     #keepalive_timeout  0;
     keepalive_timeout  65;

     gzip  on;
     #max upload file size
     client_max_body_size 6m;

......



设置自动启动服务

在/usr/lib/systemd/system/目录下面新建一个nginx.service文件
注意: nginx 安装的路径为 /usr/local/nginx

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

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
#/usr/local/nginx/sbin/nginx
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=/usr/local/nginx/sbin/nginx -c reload /usr/local/nginx/conf/nginx.conf
ExecStop=/usr/local/nginx/sbin/nginx quit
#ExecReload=/bin/kill -s HUP $MAINPID
#ExecStop=/bin/kill -s QUIT $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

另: 
service nginx start 启动时抛如下错误  


[root@aerchi-centos system]# service nginx restart
Redirecting to /bin/systemctl restart nginx.service
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.

出错原因:  

[root@aerchi-centos system]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2020-09-29 16:04:35 CST; 3min 35s ago
     Docs: http://nginx.org/en/docs
  Process: 2216 ExecStartPre=/usr/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=203/EXEC)

Sep 29 16:04:35 aerchi-centos systemd[1]: Starting nginx - high performance web server...
Sep 29 16:04:35 aerchi-centos systemd[1]: nginx.service: Control process exited, code=exited status=203
Sep 29 16:04:35 aerchi-centos systemd[1]: nginx.service: Failed with result 'exit-code'.
Sep 29 16:04:35 aerchi-centos systemd[1]: Failed to start nginx - high performance web server.

解决方法:

出现以上错误 , 请检查  nginx.service 里(nginx 安装路径是否正确)
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

之后, 再次运行 'systemctl daemon-reload' 来重新加载


乐意黎
20200929

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页