Centos7 安装nginx

1.添加Nginx到YUM源

添加CentOS 7 Nginx yum资源库,打开终端,使用以下命令:

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2.安装Nginx

在你的CentOS 7 服务器中使用yum命令从Nginx源服务器中获取来安装Nginx:

sudo yum install -y nginx

Nginx将完成安装在你的CentOS 7 服务器中。

3.启动Nginx

刚安装的Nginx不会自行启动。运行Nginx:

sudo systemctl start nginx.service

如果一切进展顺利的话,现在你可以通过你的域名或IP来访问你的Web页面来预览一下Nginx的默认页面;
如果看到这个页面,那么说明你的CentOS 7 中 web服务器已经正确安装。

4,CentOS 7 开机启动Nginx

sudo systemctl enable nginx.service

5,Nginx配置信息

网站文件存放默认目录

/usr/share/nginx/html
网站默认站点配置

/etc/nginx/conf.d/default.conf
自定义Nginx站点配置文件存放目录

/etc/nginx/conf.d/
Nginx全局配置

/etc/nginx/nginx.conf
Nginx启动

nginx -c nginx.conf
在这里你可以改变设置用户运行Nginx守护程序进程一样,和工作进程的数量得到了Nginx正在运行,等等。

遇见的问题

1 nginx报错 open() failed (13 permission denied)

初步排查

初步怀疑是权限问题 于是给前端目录最高权限 chmod -R 777 <前端目录> ,再次用systemctl restart nginx重启nginx还是报权限的错误

继续摸排

继续搜索资料发现 nginix默认用户是nobody与用户目录不一致可能会出现权限的错误,于是修改nginx.conf文件将#user nobody; 改为 user root; 例如:

user  root;
worker_processes  1;

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

#pid        logs/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;

    server {
        listen       80;
        server_name  localhost;
    
        charset utf-8;
    
        #access_log  logs/host.access.log  main;
    
        location / {
            root   /root/www/;          ## 设置的地方
            index  index.html index.htm;
        }
                #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }
}

然后执行 systemctl restart nginx重启nginx,执行ps aux | grep nginx 查看进程都是root用户启动的,但是仍然报权限错误,无语了都.

最后

在StackOverflow的一个回答下面发现是SELinux的锅,原文: Using NGINX and NGINX Plus with SELinux

解决办法

查看SELinux状态
运行命令getenforce,验证SELinux状态。
返回状态如果是enforcing,表明SELinux已开启。

选择临时关闭或者永久关闭SELinux
执行命令setenforce 0临时关闭SELinux。

永久关闭SElinux。

运行以下命令,编辑SELinux的config文件。

vi /etc/selinux/config
找到SELINUX=enforcing,按i进入编辑模式,将参数修改为SELINUX=disabled

附:

systemctl命令用法

1、启动服务 systemctl start 服务名

2、停止服务 systemctl stop 服务名

3、重启服务 systemctl restart 服务名

4、查看服务是否已启动 systemctl is-active 服务名

5、查看服务的状态 systemctl status 服务名

6、启用开机自启动服务
systemctl enable 服务名

7、停用开机自启动服务
systemctl disable 服务名

8、查看服务是否为开机自启动
systemctl is-enabled 服务名

9、只重启正在运行中的服务
systemctl try-restart 服务名

10、显示所有的服务状态---空格翻页 q推出
systemctl list-units --type service --all

11、查看启动成功的服务列表
systemctl list-unit-files|grep enabled

12、查看启动失败的服务列表
systemctl --failed

13、查看所有服务的状态---空格翻页 q推出

systemctl list-unit-files --type service


posted @ 2023-06-16 10:28  誓鼎  阅读(164)  评论(0编辑  收藏  举报