在 Fedora21 上安装 nginx
在 Fedora 21 Workstation 上,通过 yum 安装 nginx。
用Yum进行安装
# yum install nginx
※截止至2015年2月7日安装的软件包有nginx.x86_64 1:1.6.2-4.fc21。
安装后的设置
安装后,服务将处于停止状态。
[root@fedora ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
Active: inactive (dead)
[root@fedora ~]#
設定檔案位於 /etc/nginx/nginx.conf。
从/etc/nginx/conf.d目录中加载模块化配置文件。有关更多信息,请参阅http://nginx.org/en/docs/ngx_core_module.html#include。包括/etc/nginx/conf.d/*.conf;
根据情况,在 /etc/nginx/conf.d 目录下创建一个 conf 文件。
server {
listen 81; #←nginx.confに書かれた設定が80のため、別ポートを指定
server_name localhost;
location / {
root /home/aznprpr/eclipsework; #←ルートディレクトリの指定
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ /\.ht {
deny all;
}
}
完成后,请确认是否为正确的描述。
[root@fedora conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@fedora conf.d]#
服务启动失败
當試圖啟動服務時,發生了失敗的情況。
root@fedora conf.d]# systemctl start nginx
Job for nginx.service failed. See "systemctl status nginx.service" and "journalctl -xe" for details.
根据留言内容,确认详细情况。
[root@fedora conf.d]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
Active: failed (Result: exit-code) since 土 2015-02-07 21:30:13 JST; 16s ago
Process: 5086 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=1/FAILURE)
2月 07 21:30:13 fedora.local nginx[5086]: nginx: the configuration file /e...k
2月 07 21:30:13 fedora.local systemd[1]: nginx.service: control process ex...1
2月 07 21:30:13 fedora.local systemd[1]: Failed to start The nginx HTTP an....
2月 07 21:30:13 fedora.local systemd[1]: Unit nginx.service entered failed....
2月 07 21:30:13 fedora.local systemd[1]: nginx.service failed.
2月 07 21:30:13 fedora.local nginx[5086]: nginx: [emerg] open() "/run/ngin...)
2月 07 21:30:13 fedora.local nginx[5086]: nginx: configuration file /etc/n...d
Hint: Some lines were ellipsized, use -l to show in full.
[root@fedora conf.d]#
[root@fedora conf.d]#
[root@fedora conf.d]# journalctl -xe
--
-- The result is failed.
2月 07 21:30:13 fedora.local systemd[1]: Unit nginx.service entered failed state
2月 07 21:30:13 fedora.local systemd[1]: nginx.service failed.
2月 07 21:30:13 fedora.local nginx[5086]: nginx: [emerg] open() "/run/nginx.pid"
2月 07 21:30:13 fedora.local nginx[5086]: nginx: configuration file /etc/nginx/n
2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:29:39 fedora.local vlc.desktop[2204]: TagLib: MPEG::Header::parse() --2月 07 21:30:13 fedora.local nginx[5086]: nginx: the configuration file /etc/ngi2月 07 21:30:13 fedora.local dbus[744]: [system] Activating service name='org.fe2月 07 21:30:13 fedora.local systemd[1]: nginx.service: control process exited, 2月 07 21:30:13 fedora.local systemd[1]: Failed to start The nginx HTTP and reve- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
2月 07 21:30:14 fedora.local dbus[744]: [system] Successfully activated service
2月 07 21:30:14 fedora.local setroubleshoot[5092]: SELinux is preventing nginx 2月 07 21:30:13 fedora.local systemd[1]: nginx.service failed.
2月 07 21:30:13 fedora.local nginx[5086]: nginx: [emerg] open() "/run/nginx.pid"
2月 07 21:30:13 fedora.local nginx[5086]: nginx: configuration file /etc/nginx/n
2月 07 21:30:14 fedora.local dbus[744]: [system] Successfully activated service
2月 07 21:30:14 fedora.local setroubleshoot[5092]: SELinux is preventing nginx f
2月 07 21:30:14 fedora.local python[5092]: SELinux is preventing nginx from 'rea
***** Plugin catchall (100. confid
If you believe that nginx should be
Then you should report this as a bu
You can generate a local policy mod
Do
allow this access for now by execut
# grep nginx /var/log/audit/audit.l
# semodule -i mypol.pp
2月 07 21:31:35 fedora.local google-chrome.desktop[2524]: Fontconfig error: Cann
2月 07 21:31:35 fedora.local google-chrome.desktop[2524]: [2524:2563:0207/213135
2月 07 21:31:51 fedora.local google-chrome.desktop[2524]: [2524:2563:0207/213151
[root@fedora conf.d]#
不知为何,我对SELinux有些怀疑
在启动时弹出了一个弹窗…
当我尝试启动SELinux通知浏览器时,我发现了两个用于故障排除的命令,我尝试着输入了它们。
root@fedora conf.d]# grep nginx /var/log/audit/audit.log | audit2allow -M mypol
******************** IMPORTANT ***********************
To make this policy package active, execute:
semodule -i mypol.pp
[root@fedora conf.d]#
[root@fedora conf.d]# semodule -i mypol.pp
[root@fedora conf.d]#
服务的启动
再次尝试启动
[root@fedora conf.d]# systemctl start nginx
[root@fedora conf.d]#
[root@fedora conf.d]#
[root@fedora conf.d]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
Active: active (running) since 土 2015-02-07 21:54:20 JST; 17s ago
Process: 8475 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 8472 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 8478 (nginx)
CGroup: /system.slice/nginx.service
├─8478 nginx: master process /usr/sbin/nginx
└─8479 nginx: worker process
2月 07 21:54:20 fedora.local nginx[8472]: nginx: the configuration file /e...k
2月 07 21:54:20 fedora.local nginx[8472]: nginx: configuration file /etc/n...l
Hint: Some lines were ellipsized, use -l to show in full.
[root@fedora conf.d]#
无事成功启动
迷上了
当使用浏览器访问localhost:81时,会出现403禁止访问的情况。
这是因为我将用户目录下的Eclipse工作空间设置为文档根目录,
所以我提前将整个目录权限更改为755。
然而,查看日志发现似乎是权限拒绝了… ‘ér, shì …)
[root@fedora conf.d]# cat /var/log/nginx/error.log
(略)
2015/02/07 22:07:10 [error] 8479#0: *9 open() "/home/aznprpr/eclipsework/adminpage/about.html" failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET /adminpage/about.html HTTP/1.1", host: "localhost:81"
2015/02/07 22:07:11 [error] 8479#0: *9 open() "/home/aznprpr/eclipsework/favicon.ico" failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:81"
[root@fedora conf.d]#
虽然没有网站图标,但是为什么…
当我尝试搜索时,的确发现用户目录本身的权限也是必要的,这是一个令人惊讶的盲点…
将用户目录本身的权限设置为755,再次访问,情况没有改变。(因为我在开发机上忽略了安全问题,所以可以做这样的设置)
你又来了
当我查看SELinux通知中心时,发现了有关nginx的另一个通知…
因此我执行了其中提到的新命令。
[root@fedora conf.d]# setsebool -P httpd_read_user_content 1
验证页面显示结果。
很长时间了… …)
我觉得禁用SELinux会使开发变得更轻松。
请参考此页面。
-
- Nginxのインストールと基本設定
-
- CentOS 6.5でnginxを動かす為の最低限の設定
-
- nginx連載4回目: nginxの設定、その2 – バーチャルサーバの設定
-
- CentOS7でのサービス(デーモン)の起動・停止方法
- CentOSでApacheで403エラーが発生した原因