如何在centos 6上通过yum安装nginx和php-fpm
原文地址:http://www.lifelinux.com/how-to-install-nginx-and-php-fpm-on-centos-6-via-yum/
开始安装nginx和php-fpm之前,你必须卸载系统中以前安装的apache和php。用root登录输入下面的命令:
# yum remove httpd* php*增加额外资源库
默认情况下,centos的官方资源是没有php-fpm的, 但我们可以从remi的rpm资源中获得,它依赖于epel资源。我们可以这样增加两个资源库:
# yum install yum-priorities -y# rpm -uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm# rpm -uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
输出样例
retrieving http://download.fedora.redhat.com/pub/epel/6/x86_64/epel-release-6-7.noarch.rpmwarning: /var/tmp/rpm-tmp.00kidx: header v3 rsa/sha256 signature, key id 0608b895: nokeypreparing...########################################### [100%]1:epel-release ########################################### [100%]
安装nginx
输入下列命令
# yum install nginx输出样例
dependencies resolved================================================================================ package arch version repository size================================================================================installing: nginx x86_64 0.8.54-1.el6 epel 358 kinstalling for dependencies: geoip x86_64 1.4.8-1.el6 epel 620 k fontconfig x86_64 2.8.0-3.el6 base 186 k freetype x86_64 2.3.11-6.el6_1.8 updates 358 k gd x86_64 2.0.35-10.el6 base 142 k libx11 x86_64 1.3-2.el6 base 582 k libx11-common noarch 1.3-2.el6 base 188 k libxau x86_64 1.0.5-1.el6 base 22 k libxpm x86_64 3.5.8-2.el6 base 59 k libjpeg x86_64 6b-46.el6 base 134 k libpng x86_64 2:1.2.46-1.el6_1 base 180 k libxcb x86_64 1.5-1.el6 base 100 k libxslt x86_64 1.1.26-2.el6 base 450 k perl x86_64 4:5.10.1-119.el6_1.1 base 10 m perl-module-pluggable x86_64 1:3.90-119.el6_1.1 base 37 k perl-pod-escapes x86_64 1:1.04-119.el6_1.1 base 30 k perl-pod-simple x86_64 1:3.13-119.el6_1.1 base 209 k perl-libs x86_64 4:5.10.1-119.el6_1.1 base 575 k perl-version x86_64 3:0.77-119.el6_1.1 base 49 ktransaction summary================================================================================install 19 package(s)upgrade 0 package(s)total download size: 14 minstalled size: 47 mis this ok [y/n]: y
如果你想在系统启动时自动运行nginx,输入下列命令:
# chkconfig --level 345 nginx on
第一次启动nginx,输入下列命令:
# /etc/init.d/nginx start
输出样例
starting nginx: [ ok ]
安装php-fpm
输入下列命令:
# yum --enablerepo=remi install php php-fpm
输出样例
dependencies resolved==================================================================================== package arch version repository size====================================================================================installing: php x86_64 5.3.10-2.el6.remi remi 2.3 m php-fpm x86_64 5.3.10-2.el6.remi remi 1.1 minstalling for dependencies: apr x86_64 1.3.9-3.el6_1.2 base 123 k apr-util x86_64 1.3.9-3.el6_0.1 base 87 k apr-util-ldap x86_64 1.3.9-3.el6_0.1 base 15 k httpd x86_64 2.2.15-15.el6.centos.1 updates 813 k httpd-tools x86_64 2.2.15-15.el6.centos.1 updates 70 k libedit x86_64 2.11-4.20080712cvs.1.el6 base 74 k mailcap noarch 2.1.31-2.el6 base 27 k php-cli x86_64 5.3.10-2.el6.remi remi 2.2 mtransaction summary====================================================================================install 10 package(s)upgrade 0 package(s)total download size: 6.8 minstalled size: 21 mis this ok [y/n]: y
如果你想在系统启动时自动运行php-fpm,输入下列命令:
# chkconfig --level 345 php-fpm on
php仅安装了核心模块,你很可能需要安装其他的模块,比如mysql、 xml、 gd等等,你可以输入下列命令:
# yum --enablerepo=remi install php-gd php-mysql php-mbstring php-xml php-mcrypt
第一次启动php-fpm,输入下列命令:
# /etc/init.d/php-fpm restart
输出样例
starting php-fpm: [ ok ]
配置php-fpm和nginx,让他们一起工作nginx的配置文件在/etc/nginx/nginx.conf,输入下列命令编辑这个文件:
# vi /etc/nginx/nginx.conf
像下面这样编辑取消注释:
... location / { root /usr/share/nginx/html; index index.html index.htm index.php; } ... location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param script_filename $document_root$fastcgi_script_name; include fastcgi_params; } ...
重启nginx会重新读取配置文件,输入
# /etc/init.d/nginx reload
现在在document root目录下建立下列php文件
# vi /usr/share/nginx/html/info.php
文件内容如下:
访问 http://your-server-ip
nginx虚拟主机设置
设置例子
ip: 192.168.1.113domain: domain.localhosted at: /home/www/domain.local
输入下列命令新建名叫“www”的用户
# useradd www创建必要的目录
# mkdir -p /home/www/domain.local/public_html# mkdir -p /home/www/domain.local/log# chown -r www.www /home/www/# chmod 755 /home/www/
创建虚拟主机配置文件
# cd /etc/nginx/conf.d/# cp virtual.conf www.conf
输入下面命令打开www.conf文件
# vi /etc/nginx/conf.d/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:9000; fastcgi_index index.php; fastcgi_param script_filename $document_root$fastcgi_script_name; }}
你可以用下列方法检查配置文件是否有语法错误
# /etc/init.d/nginx configtest
输出样例
the configuration file /etc/nginx/nginx.conf syntax is okconfiguration file /etc/nginx/nginx.conf test is successful
现在编辑/etc/php-fpm.d/www.conf文件,将运行php-fpm进程的用户改为“www”,输入
# vi /etc/php-fpm.d/www.conf找到“group of processes”,编辑成下面的样子:
; unix user/group of processes; note: the user is mandatory. if the group is not set, the default user's group; will be used.; rpm: apache choosed to be able to access some dir as httpduser = www; rpm: keep a group allowed to write in log dir.group = www
最后重启nginx
# /etc/init.d/nginx restart# /etc/init.d/php-fpm restart