首页 > 系统相关 >Centos7 安装nginx

Centos7 安装nginx

时间:2023-06-16 10:48:12浏览次数:37  
标签:服务 log Nginx Centos7 nginx systemctl error 安装

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


标签:服务,log,Nginx,Centos7,nginx,systemctl,error,安装
From: https://www.cnblogs.com/shiding/p/17484963.html

相关文章

  • linux服务器mysql的简单安装和使用(基于二进制文件方式)
    mysql下载地址https://downloads.mysql.com/archives/community/版本选择 直接放置/root目录下     常规操作查找以前是否装有mysqlrpm-qa|grep-imysql如果找到或者以前安装过有遗留文件,操作如下命令删除命令:rpm-e--nodeps包名删除老版本m......
  • MyBatis安装&入门
    MyBatis创建在pom.xml中添加依赖<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><dependency>&l......
  • Servlet安装以及基础操作
    Servlet创建新建一个类,继承Servlet接口publicclassTestServlet2implementsServlet{publicvoidinit(ServletConfigservletConfig)throwsServletException{}publicServletConfiggetServletConfig(){returnnull;}publicvoid......
  • Linux yum 软件安装
    背景安装软件经常用到RPM,默认的源里面内容非常有限,在安装一些特殊软件的时候,需要自己制定源,例如Gitlab-ce,Gogs等。然而,源是个什么鬼,我从没搞明白过。借助安装Gogs的机会学习下。安装Gogs必须执行下面的指令:sudorpm--importhttps://rpm.packager.io/keyecho"[gogs]name=Reposi......
  • 安装docker
    curl-o/etc/yum.repos.d/CentOS-Base.repohttps://mirrors.aliyun.com/repo/Centos-7.repoyuminstall-yyum-utilsdevice-mapper-persistent-datalvm2yum-config-manager--add-repohttps://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.reposed-i-e'......
  • if判断访问的域名 nginx
    单个if($http_Host!='127.0.0.1:80'){return403;}多个域名set$flag0;if($http_Host!='127.0.0.1:80'){ set$flag"${flag}1";}if($http_Host!="www.baidu.com:80"){ set$flag"${flag}2";}if($flag="01......
  • 在centos7.6系统下搭建smba服务器
    ​一、samba服务器的安装 1、安装samba[root@server1~]#yum-yinstallsamba*安装完成后,可以使用以下命令查看Samba服务的运行状态systemctlstatussmb如果服务未运行,则可以使用以下命令启动Samba服务:systemctlstartsmb需要关闭samba服务,使用下面命令system......
  • IPMItool安装后出现找不到libfreeipmi.so.17库文件问题
    描述:IPMItool安装后出现找不到libfreeipmi.so.17库文件问题解决:通过find查找libfreeipmi.so.17文件,设置环境变量:exportLD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH(单次生效) 永久生效 ......
  • Linux下的Jenkins安装教程
    sudowget-O/etc/yum.repos.d/jenkins.repohttps://pkg.jenkins.io/redhat-stable/jenkins.repo--no-check-certificatesudorpm--importhttps://pkg.jenkins.io/redhat-stable/jenkins.io.key通过yum安装Jenkinsyum-yinstalljenkinsJenkins默认端口号为8080,输入vim......
  • Linux环境下JDK安装
    安装JDK将JDK导入opt目录下面的software文件夹解压JDK到/opt/module目录下tar-zxvfjdk-8u144-linux-x64.tar.gz-C/opt/module/配置JDK环境变量获取jdk路径[[email protected]_144]$pwd/opt/module/jdk1.8.0_144打开/etc/profile文件vim/etc/......