centos7下安装、配置Nginx、设置Nginx开机自启动

测试环境:

[root@centos-linux ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

[root@centos-linux ~]# php --version

PHP 7.2.32 (cli)

 

首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库)

1、安装相关的依赖包。

yum install -y gcc-c++

yum install -y pcre pcre-devel

yum install -y zlib zlib-devel

yum install -y openssl openssl-devel

 

2、下载Nginx

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

 

3、解压安装包

tar zxvf nginx-1.18.0.tar.gz 

 

4、进入安装包目录

cd nginx-1.18.0/

 

5、编译安装

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

make

make install

 

6、创建 Nginx 运行使用的用户 www:

/usr/sbin/groupadd www

/usr/sbin/useradd -g www www

 

7、nginx.conf最小配置(/usr/local/nginx/conf/nginx.conf),红色为新增内容

user www www;
worker_processes  1;
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/nginx/nginx.pid;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;#站点目录
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

 

8、检查配置文件正确性的命令

# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

  

9、启动Nginx

/usr/local/nginx/sbin/nginx

  

10、站点访问

  

 11、Nginx常用命令

/usr/local/webserver/nginx/sbin/nginx -t     # 检查配置

/usr/local/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件

/usr/local/webserver/nginx/sbin/nginx -s reopen # 重启Nginx

/usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx

 

12、Nginx调用PHP

先启动php-fpm

systemctl start php-fpm  // 需要加入systemctl服务后才可以

  在配置文件中增加如下内容

location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
         # include fastcgi.conf;  #或用此句替代以上两句。不通版本的nginx不一定都包含该文件,如果没有该配置文件,则使用上面两句话。
}

 

12.1、如果是编译安装的php

  可以参考https://www.cnblogs.com/iverson-3/p/14163035.html 配置php-fpm

 

13、使用yum安装Nignx

// yum -y install epel-release  // 如果需要添加CentOS EPEL仓库
yum -y install nginx

启动Nginx

systemctl start nginx

14、卸载Nginx

查找nginx相关文件

find / -name nginx*

从源头删除

rm -rf /usr/sbin/nginx
rm -rf /etc/nginx
rm -rf /etc/init.d/nginx

在使用yum清理

yum remove nginx

 

15、设置Nginx开机自启动

在/lib/systemd/system目录下,创建nginx.service文件

编辑nginx.service文件,增加如下内容

[Unit]
Description=nginx service
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
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
 
服务的启动/停止/刷新配置文件/查看状态 
# systemctl start nginx.service          // 启动nginx服务
# systemctl stop nginx.service           // 停止服务
# systemctl restart nginx.service        // 重新启动服务
# systemctl list-units --type=service     // 查看所有已启动的服务
# systemctl status nginx.service          // 查看服务当前状态
# systemctl enable nginx.service          // 设置开机自启动
# systemctl disable nginx.service         //停止开机自启动

 

可能遇到的错误提示:

Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
直接按照提示执行命令systemctl daemon-reload 即可。
 



posted @ 2020-11-27 15:40  阿木工作室  阅读(1405)  评论(0编辑  收藏  举报