CentOS 6.8 配置 LNMP(Linux+Nginx+PHP5+MySQL)

1、使用 yum 直接安装 Nginx

[root@hexingxing ~]#  yum install -y nginx
已加载插件:fastestmirror, refresh-packagekit, security
设置安装进程
Determining fastest mirrors
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.nwsuaf.edu.cn
No package nginx available.
错误:无须任何处理

## 系统不能直接 yum install nginx,判断可能 yum 源有问题

2、下载 Nginx 源

[root@hexingxing html]# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
Retrieving http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
warning: /var/tmp/rpm-tmp.KcjpHW: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing...                ########################################### [100%]
1:nginx-release-centos   ########################################### [100%]

##下载成功

3、查看 Nginx 源是否下载成功

[root@hexingxing ~]# ls /etc/yum.repos.d/
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-fasttrack.repo  CentOS-Media.repo  CentOS-Vault.repo  nginx.repo

## 在/etc/yum.repos.d/ 存在 nginx.repo 文件,那么可以安装了,为了确认是否可安装,先查看下 Nginx 的软件信息

4、查看 Nginx 软件信息

[root@hexingxing html]# yum info nginx
已加载插件:fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: centos.ustc.edu.cn
nginx                                                                                                                                                     | 2.9 kB     00:00
nginx/primary_db                                                                                                                                          |  15 kB     00:00
可安装的软件包
Name        : nginx
Arch        : x86_64
Version     : 1.10.1
Release     : 1.el6.ngx
Size        : 821 k
Repo        : nginx
Summary     : High performance web server
URL         : http://nginx.org/
License     : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
: a mail proxy server.

## 可以获取到 Nginx 的软件信息,说明已经可以安装了

5、安装 Nginx

[root@hexingxing ~]# yum install nginx
已加载插件:fastestmirror, refresh-packagekit, security
设置安装进程
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: centos.ustc.edu.cn
解决依赖关系
--> 执行事务检查
---> Package nginx.x86_64 0:1.10.1-1.el6.ngx will be 安装
--> 完成依赖关系计算

依赖关系解决

=================================================================================================================================================================================
软件包                                 架构                                    版本                                                仓库                                    大小
=================================================================================================================================================================================
正在安装:
nginx                                  x86_64                                  1.10.1-1.el6.ngx                                    nginx                                  821 k

事务概要
=================================================================================================================================================================================
Install       1 Package(s)

总下载量:821 k
Installed size: 2.1 M
确定吗?[y/N]:y
下载软件包:
nginx-1.10.1-1.el6.ngx.x86_64.rpm                                                                                                                         | 821 kB     00:23
运行 rpm_check_debug
执行事务测试
事务测试成功
执行事务
Warning: RPMDB altered outside of yum.
正在安装   : nginx-1.10.1-1.el6.ngx.x86_64                                                                                                                                 1/1
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
Verifying  : nginx-1.10.1-1.el6.ngx.x86_64                                                                                                                                 1/1

已安装:
nginx.x86_64 0:1.10.1-1.el6.ngx

完毕!

##Nginx 安装完成

6、启动 Nginx

[root@hexingxing ~]# service nginx start
正在启动 nginx:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[失败]

##发现启动 Nginx 失败,根据以上错误信息,我发现可能是因为之前已安装并启动了 Apache,可能因此而报错,所以我现在停止 Apache 服务

6、停止 Apache

[root@hexingxing ~]# service httpd stop
停止 httpd:                                               [确定]

##停止 Apache

7、启动 Nginx

[root@hexingxing ~]# service nginx start
正在启动 nginx:                                           [确定]

##启动 Nginx 成功

8、访问服务器 IP

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

##访问服务器 IP 出现以上提示,说明 Nginx 已经安装成功并且可用,Nginx 的 Web 默认目录为 /usr/share/nginx/html/ ,可在此目录下放置网站文件,安装了 PHP 后可运行 PHP 网站。

 

到此,Nginx 已经安装成功。如果 Nginx 的 Web 页面不能访问可能还需要配置服务器的防火墙,配置防火墙和继续安装 MySQL 和 PHP ,可以参考何星星之前写的一篇《CentOS 6.8 配置 LAMP(Linux+Apache+PHP+MySQL)环境》。

 

搭建 LNMP 环境之 Nginx 与 PHP (FastCGI) 的配置

一、安装和启动 nginx
1.1、安装 nginx

[root@hexingxing ~]# yum install -y nginx
// ...

Installed:
nginx.x86_64 0:1.10.2-1.el6

Dependency Installed:
GeoIP.x86_64 0:1.6.5-1.el6                                             GeoIP-GeoLite-data.noarch 0:2016.07-1.el6
GeoIP-GeoLite-data-extra.noarch 0:2016.07-1.el6                        gd.x86_64 0:2.0.35-11.el6
geoipupdate.x86_64 0:2.2.1-2.el6                                       libXpm.x86_64 0:3.5.10-2.el6
libxslt.x86_64 0:1.1.26-2.el6_3.1                                      nginx-all-modules.noarch 0:1.10.2-1.el6
nginx-filesystem.noarch 0:1.10.2-1.el6                                 nginx-mod-http-geoip.x86_64 0:1.10.2-1.el6
nginx-mod-http-image-filter.x86_64 0:1.10.2-1.el6                      nginx-mod-http-perl.x86_64 0:1.10.2-1.el6
nginx-mod-http-xslt-filter.x86_64 0:1.10.2-1.el6                       nginx-mod-mail.x86_64 0:1.10.2-1.el6
nginx-mod-stream.x86_64 0:1.10.2-1.el6

Complete!

1.2.1、启动 nginx

[root@hexingxing ~]# service nginx start
Starting nginx: nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
[FAILED]

// 启动失败,看错误提示描述 “ [::]:80 failed ”,到 nginx 配置里修改下 [::]:80 端口的监听

1.2.2、修改 [::]:80 端口的监听

[root@hexingxing ~]# vi /etc/nginx/conf.d/default.conf

#
# The default server
#

server {
listen       80 default_server;
# listen       [::]:80 default_server;
server_name  _;
root         /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}

}

// 将 listen       [::]:80 default_server; 行注释,使其不生效
// 在这之前,正常的 Linux 操作习惯为:把 default.conf 文件备份后再修改

1.3、再次启动 nginx

[root@hexingxing ~]# service nginx start
Starting nginx:                                            [  OK  ]

// 启动 nginx 成功

1.4、设置 nginx 开机启动

[root@hexingxing ~]# chkconfig --list nginx
nginx           0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@hexingxing ~]# chkconfig nginx on
[root@hexingxing ~]# chkconfig --list nginx
nginx           0:off 1:off 2:on 3:on 4:on 5:on 6:off

1.5、验证 nginx 页面
至此,已经成功安装和启动 nginx,在浏览器上输入你的服务器 IP 即可打开你的 nginx 欢迎页面,但是在这之前你可能还需要配置防火墙和 SELINUX 才能访问。

[web view]

二、安装和配置 php

2.1 安装 php

[root@hexingxing ~]# yum install -y php
// ...

Installed:
php.x86_64 0:5.3.3-48.el6_8

Dependency Installed:
apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1     httpd.x86_64 0:2.2.15-55.el6.centos.2     httpd-tools.x86_64 0:2.2.15-55.el6.centos.2
php-cli.x86_64 0:5.3.3-48.el6_8            php-common.x86_64 0:5.3.3-48.el6_8

Complete!

2.2 安装 php-fpm
// PHP-FPM(FastCGI Process Manager) ,使 nginx 支持 php

[root@hexingxing ~]# yum install -y php-fpm
// ...

Installed:
php-fpm.x86_64 0:5.3.3-48.el6_8

Complete!

2.3 启动 php-fpm

[root@hexingxing ~]# service php-fpm start
Starting php-fpm:                                          [  OK  ]

2.4 设置 php-fpm 为开机启动

[root@iZuf6e4ja95z77dnvzmssrZ conf.d]# chkconfig --list php-fpm
php-fpm         0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@iZuf6e4ja95z77dnvzmssrZ conf.d]# chkconfig php-fpm on
[root@iZuf6e4ja95z77dnvzmssrZ conf.d]# chkconfig --list php-fpm
php-fpm         0:off 1:off 2:on 3:on 4:on 5:on 6:off

2.5 修改 nginx 支持 php

2.5.1、先将需要修改的文件备份

[root@hexingxing ~]# cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
[root@hexingxing ~]# ls /etc/nginx/conf.d/
default.conf  default.conf.bak  ssl.conf  virtual.conf

2.5.2、修改文件

#
# The default server
#

server {
listen       80 default_server;
# listen       [::]:80 default_server;
server_name  _;
root         /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
index index.html index.htm index.php;
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
include        fastcgi_params;
}

}

// 在以上文件中修改以下两处:
2.5.3、将默认的以下字段:

location / {
}

修改为:

location / {
index index.html index.htm index.php;
}

2.5.4、在最后一个 “}” 之前添加以下字段:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
include        fastcgi_params;
}

注意: fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name; 字段中的 “/usr/share/nginx/html” 为你的默认或自定义 web 文件存放处,如果没有修改即是此位置。
注意: location ~ \.php$ {        root           html;        fastcgi_pass   127.0.0.1:9000; 处的 root           html; 为存放 web 文件的文件夹,可以写调用值函数或绝对值位置。

2.5.5、如果你不只一个配置文件,将字段:

listen       80 default_server;

修改为:

listen       80;

// 以上修改完成后,重启 php-fpm 和 nginx ,service php-fpm restart | service nginx restart

2.5.6、重名默认的 index.html 为 index.html.bak

[root@hexingxing ~]# mv /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak
[root@hexingxing ~]# ls /usr/share/nginx/html/
404.html  50x.html  index.html.bak  nginx-logo.png  poweredby.png

2.5.7、写个 php 的验证文件

[root@hexingxing ~]# echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/index.php
[root@hexingxing ~]# ls /usr/share/nginx/html/
404.html  50x.html  index.html.bak  index.php  nginx-logo.png  poweredby.png

3、测试 php 版本页面
打开默认的首页,如果显示为 php 版本页面,那说明已经配置成功。

4、配置文件参考
由于环境和配置以及版本不同,可能你参考本文配置后显示的结果不一样,以下为可能是你需要用到的配置默认或能帮助到你找到问题。

“/etc/nginx/conf.d/default.conf” 文件 1

server {
listen       80;
server_name  localhost;
#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;
location / {
root   /etc/nginx/html;
index  index.html index.htm index.php;
}
#error_page  404              /404.html;
# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   /etc/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
include        fastcgi_params;
}
}

“/etc/nginx/conf.d/default.conf” 文件 2

server {
listen 80;
server_name yourdomain.com;
location / {
root   /opt/app/yourdomain-dir;
index  index.php index.html index.htm;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
fastcgi_param  SCRIPT_FILENAME  /opt/app/yourdomain-dir$fastcgi_script_name;
include        fastcgi_params;
}
}