fedora中安装运行nginx

背景:

在windows10 系统的开发环境经常出各种问题,今天上午又出现了让人想不明白的问题,但是任务又比较急,想着使用自己的电脑开发。自己的电脑时fedora系统,项目中用到了nginx,因此需要在fedora中安装,故有此文。

安装步骤

本来是想下载源码后编译安装的,但是make总是失败,不得已只好从仓库中dnf来安装了。

参考

  1. dnf install nginx
  2. systemctl enable nginx.service 开机时自动启动nginx服务
  3. systemctl start nginx.service 手动启动nginx,也可以使用 service nginx start|stop|restart 来控制nginx
  4. 配置nginx.conf文件,因为nginx.conf文件是我从windows系统中复制来的,复制后文件属于自己而非root用户/用户组,因为nginx是属于root用户的,不改的话,就会因为文件权限问题而失败。chown -R root:root nginx.conf

此时启动项目,在浏览器中访问发现一些资源,js,css,图片报403禁止访问错误。自己也找了很多方法都不行。最后发现是seLinux问题。seLinux我不熟悉,只是知道和安全息息相关。
可以使用以下命令查看详细信息:journalctl -xe 会发现提示关于seLinux的问题。

journalctl  -xe
                                                       Do
                                                       allow this access for now by executing:
                                                       # ausearch -c 'nginx' --raw | audit2allow -M my-nginx
                                                       # semodule -X 300 -i my-nginx.pp

1226 11:37:22 localhost.localdomain setroubleshoot[4902]: SELinux is preventing nginx from name_connect access on the tcp_socket port 20050. For complete SELinux messages
1226 11:37:22 localhost.localdomain python3[4902]: SELinux is preventing nginx from name_connect access on the tcp_socket port 20050.

                                                       *****  Plugin connect_ports (85.9 confidence) suggests   *********************

                                                       If you want to allow nginx to connect to network port 20050
                                                       Then you need to modify the port type.
                                                       Do
                                                       # semanage port -a -t PORT_TYPE -p tcp 20050
                                                           where PORT_TYPE is one of the following: dns_port_t, dnssec_port_t, http_port_t, kerberos_port_t, ocsp_port_t.

                                                       *****  Plugin catchall_boolean (7.33 confidence) suggests   ******************

                                                       If you want to allow httpd to can network connect
                                                       Then you must tell SELinux about this by enabling the 'httpd_can_network_connect' boolean.

                                                       Do
                                                       setsebool -P httpd_can_network_connect 1

                                                       *****  Plugin catchall_boolean (7.33 confidence) suggests   ******************

                                                       If you want to allow nis to enabled
                                                       Then you must tell SELinux about this by enabling the 'nis_enabled' boolean.

                                                       Do
                                                       setsebool -P nis_enabled 1

                                                       *****  Plugin catchall (1.35 confidence) suggests   **************************

                                                       If you believe that nginx should be allowed name_connect access on the port 20050 tcp_socket by default.
                                                       Then you should report this as a bug.
                                                       You can generate a local policy module to allow this access.
                                                       Do
                                                       allow this access for now by executing:
                                                       # ausearch -c 'nginx' --raw | audit2allow -M my-nginx
                                                       # semodule -X 300 -i my-nginx.pp

1226 11:37:25 localhost.localdomain google-chrome.desktop[4501]: [4501:4540:1226/113725.433785:ERROR:connection_factory_impl.cc(386)] Failed to connect to MCS endpoint wi

seLinux的安全名单中添加nginx就可以了。
seLinux有三种模式,分别是:

  • enforcing:强制模式,代表 SELinux 运作中,且已经正确的开始限制 domain/type 了;
  • permissive:宽容模式:代表 SELinux 运作中,不过仅会有警告讯息并不会实际限制 domain/type 的存取。这种模式可以运来作为 SELinux 的 debug 之用;
  • disabled:关闭,SELinux 并没有实际运作。
    先用宽容模式试一下:setenforce 0 再访问就可以了。看来就是这个问题了,恢复开启安全模式setenforce 1

    1. 添加seLinux安全名单:semodule -i mynginx.pp

以上步骤就完成了nginx的安装了。

猜你喜欢

转载自blog.csdn.net/u010715440/article/details/79504252