1.卸载系统中默认的php和httpd
[root@x ~]# yum remove httpd* php*
Loaded plugins: fastestmirror
Setting up Remove Process
No Match for argument: php*
base | 3.7 kB :
base/primary_db | 3.6 MB :
extras | 3.4 kB :
extras/primary_db | kB :
updates | 3.4 kB :
updates/primary_db | 2.5 MB :
vz-base | B :
vz-base/primary | 1.3 kB :
vz-base /
vz-updates | B :
vz-updates/primary | 2.1 kB :
vz-updates /
Package(s) php* available, but not installed.
Resolving Dependencies
--> Running transaction check
---> Package httpd.i686 :2.2.-.el6.centos will be erased
---> Package httpd-tools.i686 :2.2.-.el6.centos will be erased
--> Finished Dependency ResolutionDependencies Resolved=================================================================================================================================================================
Package Arch Version Repository Size
=================================================================================================================================================================
Removing:
httpd i686 2.2.-.el6.centos installed 2.8 M
httpd-tools i686 2.2.-.el6.centos installed kTransaction Summary
=================================================================================================================================================================
Remove Package(s)Installed size: 2.9 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Erasing : httpd-2.2.-.el6.centos.i686 /
warning: /etc/httpd/conf/httpd.conf saved as /etc/httpd/conf/httpd.conf.rpmsave
Erasing : httpd-tools-2.2.-.el6.centos.i686 /
Verifying : httpd-2.2.-.el6.centos.i686 /
Verifying : httpd-tools-2.2.-.el6.centos.i686 / Removed:
httpd.i686 :2.2.-.el6.centos httpd-tools.i686 :2.2.-.el6.centos Complete!
2.添加安装源
[root@x ~]# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
Retrieving http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.2QHCGj: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [%]
:epel-release ########################################### [%]
3.安装nginx
[root@x ~]# yum install -y nginx
Loaded plugins: fastestmirror
Determining fastest mirrors
epel/metalink | kB :
* base: centos.sonn.com
* epel: linux.mirrors.es.net
* extras: centos.sonn.com
* updates: repos.lax.quadranet.com
epel | 4.3 kB :
epel/primary_db | 4.9 MB :
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package nginx.i686 :1.0.-.el6 will be installed
--> Processing Dependency: nginx-filesystem = 1.0.-.el6 for package: nginx-1.0.-.el6.i686
--> Processing Dependency: nginx-filesystem for package: nginx-1.0.-.el6.i686
--> Processing Dependency: libxslt.so.(LIBXML2_1.0.18) for package: nginx-1.0.-.el6.i686
--> Processing Dependency: libxslt.so.(LIBXML2_1.0.11) for package: nginx-1.0.-.el6.i686
--> Processing Dependency: libxslt.so. for package: nginx-1.0.-.el6.i686
--> Processing Dependency: libgd.so. for package: nginx-1.0.-.el6.i686
--> Processing Dependency: libexslt.so. for package: nginx-1.0.-.el6.i686
--> Processing Dependency: libGeoIP.so. for package: nginx-1.0.-.el6.i686
--> Processing Dependency: gd for package: nginx-1.0.-.el6.i686
--> Processing Dependency: GeoIP for package: nginx-1.0.-.el6.i686
--> Running transaction check
---> Package GeoIP.i686 :1.6.-.el6 will be installed
--> Processing Dependency: geoipupdate for package: GeoIP-1.6.-.el6.i686
--> Processing Dependency: GeoIP-data for package: GeoIP-1.6.-.el6.i686
---> Package gd.i686 :2.0.-.el6 will be installed
--> Processing Dependency: libfreetype.so. for package: gd-2.0.-.el6.i686
--> Processing Dependency: libfontconfig.so. for package: gd-2.0.-.el6.i686
--> Processing Dependency: libXpm.so. for package: gd-2.0.-.el6.i686
--> Processing Dependency: libX11.so. for package: gd-2.0.-.el6.i686
---> Package libxslt.i686 :1.1.-.el6_3. will be installed
---> Package nginx-filesystem.noarch :1.0.-.el6 will be installed
--> Running transaction check
---> Package GeoIP-GeoLite-data.noarch :2015.04-.el6 will be installed
--> Processing Dependency: GeoIP-GeoLite-data-extra = 2015.04-.el6 for package: GeoIP-GeoLite-data-2015.04-.el6.noarch
---> Package fontconfig.i686 :2.8.-.el6 will be installed
---> Package freetype.i686 :2.3.-.el6_6. will be installed
---> Package geoipupdate.i686 :2.2.-.el6 will be installed
---> Package libX11.i686 :1.6.-.el6 will be installed
--> Processing Dependency: libX11-common = 1.6.-.el6 for package: libX11-1.6.-.el6.i686
--> Processing Dependency: libxcb.so. for package: libX11-1.6.-.el6.i686
---> Package libXpm.i686 :3.5.-.el6 will be installed
--> Running transaction check
---> Package GeoIP-GeoLite-data-extra.noarch :2015.04-.el6 will be installed
---> Package libX11-common.noarch :1.6.-.el6 will be installed
---> Package libxcb.i686 :1.9.-.el6 will be installed
--> Processing Dependency: libXau.so. for package: libxcb-1.9.-.el6.i686
--> Running transaction check
---> Package libXau.i686 :1.0.-.el6 will be installed
--> Finished Dependency ResolutionDependencies Resolved=================================================================================================================================================================
Package Arch Version Repository Size
=================================================================================================================================================================
Installing:
nginx i686 1.0.-.el6 epel k
Installing for dependencies:
GeoIP i686 1.6.-.el6 epel k
GeoIP-GeoLite-data noarch 2015.04-.el6 epel k
GeoIP-GeoLite-data-extra noarch 2015.04-.el6 epel M
fontconfig i686 2.8.-.el6 base k
freetype i686 2.3.-.el6_6. base k
gd i686 2.0.-.el6 base k
geoipupdate i686 2.2.-.el6 epel k
libX11 i686 1.6.-.el6 base k
libX11-common noarch 1.6.-.el6 base k
libXau i686 1.0.-.el6 base k
libXpm i686 3.5.-.el6 base k
libxcb i686 1.9.-.el6 base k
libxslt i686 1.1.-.el6_3. base k
nginx-filesystem noarch 1.0.-.el6 epel 8.1 kTransaction Summary
=================================================================================================================================================================
Install Package(s)Total download size: M
Installed size: M
Downloading Packages:
(/): GeoIP-1.6.-.el6.i686.rpm | kB :
(/): GeoIP-GeoLite-data-2015.04-.el6.noarch.rpm | kB :
(/): GeoIP-GeoLite-data-extra-2015.04-.el6.noarch.rpm | MB :
(/): fontconfig-2.8.-.el6.i686.rpm | kB :
(/): freetype-2.3.-.el6_6..i686.rpm | kB :
(/): gd-2.0.-.el6.i686.rpm | kB :
(/): geoipupdate-2.2.-.el6.i686.rpm | kB :
(/): libX11-1.6.-.el6.i686.rpm | kB :
(/): libX11-common-1.6.-.el6.noarch.rpm | kB :
(/): libXau-1.0.-.el6.i686.rpm | kB :
(/): libXpm-3.5.-.el6.i686.rpm | kB :
(/): libxcb-1.9.-.el6.i686.rpm | kB :
(/): libxslt-1.1.-.el6_3..i686.rpm | kB :
(/): nginx-1.0.-.el6.i686.rpm | kB :
(/): nginx-filesystem-1.0.-.el6.noarch.rpm | 8.1 kB :
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 1.1 MB/s | MB :
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
Userid : CentOS- Key (CentOS Official Signing Key) <centos--key@centos.org>
Package: centos-release--.el6.centos.11.2.i686 (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
Importing GPG key 0x0608B895:
Userid : EPEL () <epel@fedoraproject.org>
Package: epel-release--.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
Installing : GeoIP-GeoLite-data-extra-2015.04-.el6.noarch /
Installing : GeoIP-GeoLite-data-2015.04-.el6.noarch /
Installing : libX11-common-1.6.-.el6.noarch /
Installing : nginx-filesystem-1.0.-.el6.noarch /
Installing : freetype-2.3.-.el6_6..i686 /
Installing : fontconfig-2.8.-.el6.i686 /
Installing : geoipupdate-2.2.-.el6.i686 /
Installing : GeoIP-1.6.-.el6.i686 /
Installing : libXau-1.0.-.el6.i686 /
Installing : libxcb-1.9.-.el6.i686 /
Installing : libX11-1.6.-.el6.i686 /
Installing : libXpm-3.5.-.el6.i686 /
Installing : gd-2.0.-.el6.i686 /
Installing : libxslt-1.1.-.el6_3..i686 /
Installing : nginx-1.0.-.el6.i686 /
Verifying : libX11-1.6.-.el6.i686 /
Verifying : nginx-filesystem-1.0.-.el6.noarch /
Verifying : freetype-2.3.-.el6_6..i686 /
Verifying : GeoIP-GeoLite-data-2015.04-.el6.noarch /
Verifying : libxslt-1.1.-.el6_3..i686 /
Verifying : GeoIP-1.6.-.el6.i686 /
Verifying : gd-2.0.-.el6.i686 /
Verifying : libX11-common-1.6.-.el6.noarch /
Verifying : libxcb-1.9.-.el6.i686 /
Verifying : nginx-1.0.-.el6.i686 /
Verifying : GeoIP-GeoLite-data-extra-2015.04-.el6.noarch /
Verifying : libXpm-3.5.-.el6.i686 /
Verifying : libXau-1.0.-.el6.i686 /
Verifying : fontconfig-2.8.-.el6.i686 /
Verifying : geoipupdate-2.2.-.el6.i686 / Installed:
nginx.i686 :1.0.-.el6 Dependency Installed:
GeoIP.i686 :1.6.-.el6 GeoIP-GeoLite-data.noarch :2015.04-.el6 GeoIP-GeoLite-data-extra.noarch :2015.04-.el6 fontconfig.i686 :2.8.-.el6
freetype.i686 :2.3.-.el6_6. gd.i686 :2.0.-.el6 geoipupdate.i686 :2.2.-.el6 libX11.i686 :1.6.-.el6
libX11-common.noarch :1.6.-.el6 libXau.i686 :1.0.-.el6 libXpm.i686 :3.5.-.el6 libxcb.i686 :1.9.-.el6
libxslt.i686 :1.1.-.el6_3. nginx-filesystem.noarch :1.0.-.el6 Complete!
4.设置nginx服务开机自启动
[root@x ~]# chkconfig --level nginx on
5.安装php和php-fpm
[root@x ~]# yum install -y php php-fpm
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.sonn.com
* epel: linux.mirrors.es.net
* extras: centos.sonn.com
* updates: repos.lax.quadranet.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package php.i686 :5.3.-.el6_6 will be installed
--> Processing Dependency: php-common(x86-) = 5.3.-.el6_6 for package: php-5.3.-.el6_6.i686
--> Processing Dependency: php-cli(x86-) = 5.3.-.el6_6 for package: php-5.3.-.el6_6.i686
--> Processing Dependency: httpd-mmn = for package: php-5.3.-.el6_6.i686
--> Processing Dependency: httpd for package: php-5.3.-.el6_6.i686
---> Package php-fpm.i686 :5.3.-.el6_6 will be installed
--> Running transaction check
---> Package httpd.i686 :2.2.-.el6.centos will be installed
--> Processing Dependency: httpd-tools = 2.2.-.el6.centos for package: httpd-2.2.-.el6.centos.i686
---> Package php-cli.i686 :5.3.-.el6_6 will be installed
---> Package php-common.i686 :5.3.-.el6_6 will be installed
--> Running transaction check
---> Package httpd-tools.i686 :2.2.-.el6.centos will be installed
--> Finished Dependency ResolutionDependencies Resolved=================================================================================================================================================================
Package Arch Version Repository Size
=================================================================================================================================================================
Installing:
php i686 5.3.-.el6_6 updates 1.1 M
php-fpm i686 5.3.-.el6_6 updates 1.2 M
Installing for dependencies:
httpd i686 2.2.-.el6.centos updates k
httpd-tools i686 2.2.-.el6.centos updates k
php-cli i686 5.3.-.el6_6 updates 2.2 M
php-common i686 5.3.-.el6_6 updates kTransaction Summary
=================================================================================================================================================================
Install Package(s)Total download size: 5.9 M
Installed size: M
Downloading Packages:
(/): httpd-2.2.-.el6.centos.i686.rpm | kB :
(/): httpd-tools-2.2.-.el6.centos.i686.rpm | kB :
(/): php-5.3.-.el6_6.i686.rpm | 1.1 MB :
(/): php-cli-5.3.-.el6_6.i686.rpm | 2.2 MB :
(/): php-common-5.3.-.el6_6.i686.rpm | kB :
(/): php-fpm-5.3.-.el6_6.i686.rpm | 1.2 MB :
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Total kB/s | 5.9 MB :
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : php-common-5.3.-.el6_6.i686 /
Installing : php-cli-5.3.-.el6_6.i686 /
Installing : httpd-tools-2.2.-.el6.centos.i686 /
Installing : httpd-2.2.-.el6.centos.i686 /
Installing : php-5.3.-.el6_6.i686 /
Installing : php-fpm-5.3.-.el6_6.i686 /
Verifying : httpd-2.2.-.el6.centos.i686 /
Verifying : php-fpm-5.3.-.el6_6.i686 /
Verifying : php-common-5.3.-.el6_6.i686 /
Verifying : httpd-tools-2.2.-.el6.centos.i686 /
Verifying : php-5.3.-.el6_6.i686 /
Verifying : php-cli-5.3.-.el6_6.i686 / Installed:
php.i686 :5.3.-.el6_6 php-fpm.i686 :5.3.-.el6_6 Dependency Installed:
httpd.i686 :2.2.-.el6.centos httpd-tools.i686 :2.2.-.el6.centos php-cli.i686 :5.3.-.el6_6 php-common.i686 :5.3.-.el6_6 Complete!
6.设置php-fpm服务开机自启动
[root@x ~]# chkconfig --level php-fpm on
7.安装php其他模块,如mysql相关模块php-mysql,xml相关模块php-xml等
[root@x ~]# yum install -y php-gd php-mysql php-mbstring php-xml php-mcrypt
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.sonn.com
* epel: linux.mirrors.es.net
* extras: centos.sonn.com
* updates: repos.lax.quadranet.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package php-gd.i686 :5.3.-.el6_6 will be installed
---> Package php-mbstring.i686 :5.3.-.el6_6 will be installed
---> Package php-mcrypt.i686 :5.3.-.el6 will be installed
--> Processing Dependency: libmcrypt.so. for package: php-mcrypt-5.3.-.el6.i686
---> Package php-mysql.i686 :5.3.-.el6_6 will be installed
--> Processing Dependency: php-pdo(x86-) for package: php-mysql-5.3.-.el6_6.i686
--> Processing Dependency: libmysqlclient.so.(libmysqlclient_16) for package: php-mysql-5.3.-.el6_6.i686
--> Processing Dependency: libmysqlclient.so. for package: php-mysql-5.3.-.el6_6.i686
---> Package php-xml.i686 :5.3.-.el6_6 will be installed
--> Running transaction check
---> Package libmcrypt.i686 :2.5.-.el6 will be installed
---> Package mysql-libs.i686 :5.1.-.el6_6 will be installed
---> Package php-pdo.i686 :5.3.-.el6_6 will be installed
--> Finished Dependency ResolutionDependencies Resolved=================================================================================================================================================================
Package Arch Version Repository Size
=================================================================================================================================================================
Installing:
php-gd i686 5.3.-.el6_6 updates k
php-mbstring i686 5.3.-.el6_6 updates k
php-mcrypt i686 5.3.-.el6 epel k
php-mysql i686 5.3.-.el6_6 updates k
php-xml i686 5.3.-.el6_6 updates k
Installing for dependencies:
libmcrypt i686 2.5.-.el6 epel k
mysql-libs i686 5.1.-.el6_6 base 1.2 M
php-pdo i686 5.3.-.el6_6 updates kTransaction Summary
=================================================================================================================================================================
Install Package(s)Total download size: 2.2 M
Installed size: 7.1 M
Downloading Packages:
(/): libmcrypt-2.5.-.el6.i686.rpm | kB :
(/): mysql-libs-5.1.-.el6_6.i686.rpm | 1.2 MB :
(/): php-gd-5.3.-.el6_6.i686.rpm | kB :
(/): php-mbstring-5.3.-.el6_6.i686.rpm | kB :
(/): php-mcrypt-5.3.-.el6.i686.rpm | kB :
(/): php-mysql-5.3.-.el6_6.i686.rpm | kB :
(/): php-pdo-5.3.-.el6_6.i686.rpm | kB :
(/): php-xml-5.3.-.el6_6.i686.rpm | kB :
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Total kB/s | 2.2 MB :
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : libmcrypt-2.5.-.el6.i686 /
Installing : mysql-libs-5.1.-.el6_6.i686 /
Installing : php-pdo-5.3.-.el6_6.i686 /
Installing : php-mysql-5.3.-.el6_6.i686 /
Installing : php-mcrypt-5.3.-.el6.i686 /
Installing : php-gd-5.3.-.el6_6.i686 /
Installing : php-mbstring-5.3.-.el6_6.i686 /
Installing : php-xml-5.3.-.el6_6.i686 /
Verifying : php-xml-5.3.-.el6_6.i686 /
Verifying : php-pdo-5.3.-.el6_6.i686 /
Verifying : mysql-libs-5.1.-.el6_6.i686 /
Verifying : php-mysql-5.3.-.el6_6.i686 /
Verifying : php-mbstring-5.3.-.el6_6.i686 /
Verifying : php-gd-5.3.-.el6_6.i686 /
Verifying : php-mcrypt-5.3.-.el6.i686 /
Verifying : libmcrypt-2.5.-.el6.i686 / Installed:
php-gd.i686 :5.3.-.el6_6 php-mbstring.i686 :5.3.-.el6_6 php-mcrypt.i686 :5.3.-.el6 php-mysql.i686 :5.3.-.el6_6 php-xml.i686 :5.3.-.el6_6Dependency Installed:
libmcrypt.i686 :2.5.-.el6 mysql-libs.i686 :5.1.-.el6_6 php-pdo.i686 :5.3.-.el6_6 Complete!
8.完成上面操作后就可以启动nignx和php-fpm服务了
[root@x ~]# service php-fpm start
Starting php-fpm: [ OK ]
[root@x ~]# service nginx start
Starting nginx: [ OK ]
9.接下来我们需要详配置一下nginx服务器,如服务器主目录,用户等
(1).为nginx服务器添加一个专用账号
[root@x html]# useradd www
(2).创建nginx服务主目录并设置访问权限
[root@x html]# mkdir -p /home/www/domain.local/public_html
[root@x html]# mkdir -p /home/www/domain.local/log
[root@x html]# chown -R www.www /home/www/
[root@x html]# chmod /home/www/
(3).添加nginx配置文件,可以复制一份 /etc/nginx/conf.d/virtual.conf ,然后用以下配置覆盖原内容
[root@x html]# cd /etc/nginx/conf.d/
[root@x conf.d]# cp virtual.conf www.conf
[root@x conf.d]# vi www.conf
server {
server_name domain.local; #这里改为你的域名,其他不动
root /home/www/domain.local/public_html;
access_log /home/www/domain.local/log/domain.local-access.log;
error_log /home/www/domain.local/log/domain.local-error.log; location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
配置完成后保存退出,并检查是否有语法错误:
[root@x conf.d]# /etc/init.d/nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
如果你的输出信息和上面相似,则表示配置文件没有问题了。
(4).编辑php-fpm的配置文件,将用于nginx进程的用户和组修改为刚才创建的账号,即www
vi /etc/php-fpm.d/www.conf
在文件中搜索 user = apache 和 group = apache ,改为
user = www
group = www
(5).将nignx运行账号修改为www
[root@x conf.d]# vi /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/user www; #这里修改为你创建的账号
(6).重启php-fpm和nignx服务
[root@x conf.d]# service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
[root@x conf.d]# service php-fpm restart
Stopping php-fpm: [ OK ]
Starting php-fpm: [ OK ]
10.配置防火墙
如果防火墙不开放80端口,是无法访问的,这里需要配置一下:
/sbin/iptables -I INPUT -p tcp --dport -j ACCEPT
/etc/rc.d/init.d/iptables save
11.至此,关于nginx服务器的基本配置都已经完成了,接下来可以测试一下
在/home/www/domain.local/public_html/ 目录中添加 info.php 文件,并加入如下代码:
<?php
phpinfo();
?>
保存退出后,访问此页面,如果能看到如下界面,则表示配置正确。
参考资料:http://www.lifelinux.com/how-to-install-nginx-and-php-fpm-on-centos-6-via-yum/