1. 下載對應當前系統版本的nginx包(package)
# wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2. 建立nginx的yum倉庫
# rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
3.下載並安裝nginx
# yum install nginx
4.啓動nginx服務
# systemctl start nginx
==開啓服務報錯==
Starting nginx (via systemctl): Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.
出現這樣的提示,一般先執行 $ systemctl status nginx.service 看看下面的具體情況,比如:
[root@Leco init.d]# systemctl status nginx.service
nginx.service - LSB: start and stop nginx
Loaded: loaded (/etc/rc.d/init.d/nginx)
Active: failed (Result: resources) since 三 2015-06-24 20:21:31 CST; 5min ago
Process: 18620 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
6月 24 20:21:31 Leco.lan systemd[1]: Starting LSB: start and stop nginx...
6月 24 20:21:31 Leco.lan nginx[18620]: Starting nginx: nginx: [emerg] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)
6月 24 20:21:31 Leco.lan nginx[18620]: [ 確定 ]
6月 24 20:21:31 Leco.lan systemd[1]: PID file /var/run/nginx/nginx.pid not readable (yet?) after start.
6月 24 20:21:31 Leco.lan systemd[1]: Failed to start LSB: start and stop nginx.
6月 24 20:21:31 Leco.lan systemd[1]: Unit nginx.service entered failed state.
其實看到這個systemctl這個東西出來了,就可以使用新RHEL7的啓動服務方式了, 給個傳送門供大家參考下這個方式:http://my.oschina.net/liucao/blog/470458
==解決方案==
sudo systemctl enable nginx.service
就會在/etc/systemd/system/multi-user.target.wants/目錄下新建一個/usr/lib/systemd/system/nginx.service 文件的鏈接。
之後就可以使用平常的方式操作nginx
#啓動服務
$ sudo systemctl start nginx.service
#查看日誌
$ sudo journalctl -f -u nginx.service
-- Logs begin at 四 2015-06-25 17:32:20 CST. --
6月 25 10:28:24 Leco.lan systemd[1]: Starting nginx - high performance web server...
6月 25 10:28:24 Leco.lan nginx[7976]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
6月 25 10:28:24 Leco.lan nginx[7976]: nginx: configuration file /etc/nginx/nginx.conf test is successful
6月 25 10:28:24 Leco.lan systemd[1]: Started nginx - high performance web server.
#重啓
$ sudo systemctl restart nginx.service
#重載
$ sudo systemctl reload nginx.service
#停止
$ sudo systemctl stop nginx.service
5.配置
默認的配置文件在 /etc/nginx 路徑下,使用該配置已經可以正確地運行nginx;如需要自定義,修改其下的 nginx.conf 等文件即可。
6.測試
在瀏覽器地址欄中輸入部署nginx環境的機器的IP,如果一切正常,應該能看到如下字樣的內容。