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,如果一切正常,应该能看到如下字样的内容。