CentOS 7 安装 LNMP 环境 ( nginx )

Nginx 安装源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

Nginx 安装
yum install -y nginx

管理Nginx 服务

# 开机启动
systemctl enable nginx.service
# 开启
systemctl start nginx.service
# 停止
systemctl stop nginx.service
# 重启
systemctl restart nginx.service
# 平滑重启
systemctl reload nginx.service
 

启动Nginx 服务
CentOS 7 安装 LNMP 环境 ( nginx )

Nginx自定义server

nginx -t查看 nginx 的配置问题

CentOS 7 安装 LNMP 环境 ( nginx )

进入 /etc/nginx/conf.d复制 default.conf 为 myhost.conf
配置 myhost.conf
CentOS 7 安装 LNMP 环境 ( nginx )

本地配置hosts指向服务器IP,ip地址 www.myhost.me
这是浏览器访问 www.myhost.me 应该能能访问nginx欢迎页。然而页面提示 403

注:解决访问显示 403 Forbidden问题

1:由于启动用户和nginx工作用户不一致所致

查看nginx的启动用户,发现是nobody,而为是用root启动的

命令:ps aux | grep “nginx: worker process” | awk’{print $1}’

nginx.config的user改为和启动用户一致

命令:vi conf/nginx.conf

CentOS 7 安装 LNMP 环境 ( nginx )

2:SELinux设置为开启状态(enabled)的原因

查看当前selinux的状态。

命令: /usr/sbin/sestatus

SELINUX=enforcing 修改为 SELINUX=disabled 状态。
CentOS 7 安装 LNMP 环境 ( nginx )

重启生效。reboot

浏览器再次访问 www.myhost.me 应该能能访问nginx欢迎页

如何访问PHP文件

按照下面的php部分安装成功并启动php-fpm服务后配置:开启红框部分代码。便访问成功。
root /workspace/myhost 被定义了两次,所以可以将两处合并到最上面的红色字体部分

CentOS 7 安装 LNMP 环境 ( nginx )

访问:
CentOS 7 安装 LNMP 环境 ( nginx )