0
点赞
收藏
分享

微信扫一扫

部署编译安装的nginx

书呆鱼 2021-09-30 阅读 67
Linux
1.编写systemd配置文件

先看看官方nginx的systemd配置文件

vim /usr/lib/systemd/system/nginx.service          #yum安装的nginx配置文件目录
[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=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target

拷贝一份改名为nginx2.service

cp /usr/lib/systemd/system/nginx.service /usr/lib/systemd/system/nginx2.service

修改nginx2.service配置文件

[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= /tmp/nginx/pid/nginx.pid      #PID路径修改为编译安装的路径(需要在nginx目录下创建一个pid目录)
ExecStart=/tmp/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf       #启动选项使用编译包中的启动
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /tmp/nginx/pid/nginx.pid)"    #修改路径
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /tmp/nginx/pid/nginx.pid)"     #修改路径

[Install]
WantedBy=multi-user.target
2.修改nginx配置文件
vim /tmp/nginx/conf/nginx.conf     #打开编译安装的nginx配置文件
#user  nobody;
worker_processes  1;

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

pid        /tmp/nginx/pid/nginx.pid;    #将pid注释取消,指定路径


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
3.启动nginx服务
systemctl daemon-reload            #重载systemd配置文件
systemctl start nginx2.service     #启动编译安装的nginx

启动服务时出现报错,提示80端口被占用

[root@Centos7-100 tmp]# systemctl status nginx2.service
● nginx2.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx2.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 四 2021-04-08 15:56:52 CST; 9s ago
     Docs: http://nginx.org/en/docs/
  Process: 18401 ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf (code=exited, status=1/FAILURE)

4月 08 15:56:49 Centos7-100 nginx[18401]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
4月 08 15:56:50 Centos7-100 nginx[18401]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
4月 08 15:56:50 Centos7-100 nginx[18401]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
4月 08 15:56:51 Centos7-100 nginx[18401]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
4月 08 15:56:51 Centos7-100 nginx[18401]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

修改编译安装nginx配置文件中的端口

 #gzip  on;

    server {
        listen       8080;        #默认为80,修改为8080端口
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;

            index  index.html index.htm;
        }

重启一下nginx2

[root@Centos7-100 tmp]#  systemctl start nginx2
[root@Centos7-100 tmp]#

查看一下服务状态

[root@Centos7-100 tmp]# systemctl status nginx2
● nginx2.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx2.service; disabled; vendor preset: disabled)
   Active: active (running) since 四 2021-04-08 15:48:34 CST; 42s ago
     Docs: http://nginx.org/en/docs/
  Process: 18188 ExecStart=/tmp/nginx/sbin/nginx -c /tmp/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 18189 (nginx)
   CGroup: /system.slice/nginx2.service
           ├─18189 nginx: master process /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
           └─18190 nginx: worker process

OK启动成功

举报

相关推荐

0 条评论