centos5.8使用yum配置LNMP环境

介绍:使用yum安装LNMP省去了很多时间,比较快速,但是nginx和php的版本就没有编译安装的高了,如果对版本要求比较高的话,建议使用编译方式配置LNMP

本文使用的系统是Centos 5.8 x86_64

一、安装运行LNMP所需的库文件和依赖环境

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc

glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel

openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers


二、安装nginx,由于centos没有默认的nginx软件包,需要添加额外的yum源才能安装

#rpm -ivh http://nginx.org/packages/centos/5/noarch/RPMS/nginx-release-centos-5-0.el5.ngx.noarch.rpm 执行成功会显示如下信息:

Retrieving http://nginx.org/packages/centos/5/noarch/RPMS/nginx-release-centos-5-0.el5.ngx.noarch.rpm

warning: /var/tmp/rpm-xfer.kvEvKj: Header V3 RSA/SHA1 signature: NOKEY, key ID 7bd9bf62

Preparing... ########################################### [100%]

1:nginx-release-centos ########################################### [100%]

到/etc/yum.repos.d/目录下查看,会发现多了一个名为nginx.repo的yum源

然后开始安装nginx

#yum -y install nginx

#chkconfig --add nginx

#chkconfig nginx on //设置开机启动

#service nginx start //启动ngin服务

需要说明一下:

使用nginx.repo源安装的nginx版本是nginx.x86_64 1.4.3-1.el5.ngx 如果想使用高一点的版本,则需要添加另外一个yum源,

此yum源是一个俄罗斯人定期做成rpm包,更新比较快!使用这个yum源安装的nginx版本是nginx.x86_64 1.5.6-1.el5。后面在安装php-fpm、spawn-fcgi时都会用到centalt.repo源。

#vi /etc/yum.repos.d/centalt.repo

[CentALT]

name=CentALT Packages for Enterprise Linux 5 - $basearch

baseurl=http://centos.alt.ru/repository/centos/5/$basearch/

enabled=1

gpgcheck=0

保存退出!

具体使用哪一个版本根据个人爱好而定。本人使用centalt.repo源来安装nginx

#yum -y install nginx

#service nginx start

#chkconfig --add nginx

#chkconfig nginx on

查看nginx启动状况:

#netstat -tunlp

150958453.png

三、安装php和mysql

#yum -y install php mysql mysql-server mysql-devel php-mysql php-mbstring php-xml php-xmlrpc php-imap php-odbc php-mcrypt php-mhash php-pear php-soap php-bcmath php-pdo php-gd php-cli php-tidy php-pecl-memcache

我在执行以上命令的时候出错,提示:

php-gd-5.2.17-36.el5.x86_64 from CentALT has depsolving problems

--> Missing Dependency: libt1.so.5()(64bit) is needed by package php-gd-5.2.17-36.el5.x86_64 (CentALT)

Error: Missing Dependency: libt1.so.5()(64bit) is needed by package php-gd-5.2.17-36.el5.x86_64 (CentALT)

You could try using --skip-broken to work around the problem

You could try running: package-cleanup --problems

package-cleanup --dupes

rpm -Va --nofiles --nodigest

The program package-cleanup is found in the yum-utils package.

看来是依赖关系的原因,在网上找到的解决方法:

下载安装对应版本的t1lib的rpm包后,重新yum安装php-gd即可。rpm包下载路径如下:

32位系统:

http://rpm.pbone.net/index.php3/stat/4/idpl/5782702/dir/centos_5/com/t1lib-5.1.0-9.el5.kb.i386.rpm.html

64位系统:

http://rpm.pbone.net/index.php3/stat/4/idpl/5786767/dir/centos_5/com/t1lib-5.1.0-9.el5.kb.x86_64.rpm.html

我的系统是Centos 5.8 x86_64,下载好t1lib-5.1.0-9.el5.kb.x86_64.rpm之后安装

#yum localinstall t1lib-5.1.0-9.el5.kb.x86_64.rpm 单纯这样安装会提示t1lib-5.1.0-9.el5.kb.x86_64.rpm is not signed,所以

#yum localinstall t1lib-5.1.0-9.el5.kb.x86_64.rpm --nogpgcheck 就可以成功安装了


启动mysql

#service mysqld start

#chkconfig --add mysqld

#chkconfig mysqld on

查看php版本

#php -v

PHP 5.2.17 (cli) (built: Aug 26 2013 15:43:03)

Copyright (c) 1997-2010 The PHP Group

查看mysql启动状况

151131699.png

四、安装第三方的FastCGI进程管理器(有两种方法,任选一种)

方法1、安装spawn-fcgi来运行cgi //spawn-fcgi是用来调用php提供动态php格式的网页

说明:Nginx是个轻量级的HTTP server,必须借助第三方的FastCGI处理器才可以对PHP进行解析,因此Nginx+spawn-fcgi的组合也可以实现对PHP的解析


centos5.8自带的yum源无法直接安装spawn-fcgi 软件包,需要添加第二步提到的centalt.repo yum源才能安装。

#yum install spawn-fcgi -y 版本是1.6.3-1.el5

下载spawn-fcgi 的启动脚本

#wget http://bash.cyberciti.biz/dl/419.sh.zip //这是在网上找的写好的脚本,就直接拿来用了。

#unzip -q -d ./ 419.sh.zip //将419.sh.zip直接解压到当前目录下面

#mv 419.sh /etc/init.d/php_cgi

#chmod +x /etc/init.d/php_cgi

#/etc/init.d/php_cgi start //启动php_cgi

Starting php-cgi: spawn-fcgi: child spawned successfully: PID: 30876

[ OK ]

#netstat -tunlp 查看端口是否启动

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 30876/php-cgi

以上信息表示php_cgi已经启动成功了!


方法2、安装php-fpm来运行cgi(本次使用的是php-fpm)

说明:PHP-FPM是一个第三方的PHPFastCGI管理器,是只用于PHP的。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置

相对Spawn-FCGI,php-fpm在CPU和内存方面的控制都更胜一筹,而且前者很容易崩溃,必须用crontab进行监控,而PHP-FPM则没有这种烦恼。


直接安装php-fpm是无法安装的,会提示:No package php-fpm available.

此时就需要添加第二步的centalt.repo yum源才可以安装php-fpm

#yum install php-fpm -y

启动php-fpm

#service php-fpm

Starting php-fpm: [ OK ]

#netstat -tunlp 出现如下信息说明php-fpm启动成功

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 3937/php-fpm

如果启动的进程是php-cgi 也属于正常,我就到过,第一次安装启动后是php-fpm,第二次安装启动后是php-cgi,如下:

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 4789/php-cgi

查看php-fpm进程运行状况

ps aux | grep "php-fpm"

145802538.png

五、配置nginx和php的整合

nginx配置文件nginx.conf #vi /etc/nginx/nginx.conf

找到如下几行:

location / {

root /usr/share/nginx/html;

index index.html index.htm;

}

#

#location ~ \.php$ {

# root html;

# fastcgi_pass 127.0.0.1:9000;

# fastcgi_index index.php;

# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

# include fastcgi_params;

#}

1、在index后面添加index.php,改过之后是:

index index.php index.html index.htm;

2、将location ~ \.php$ {}前面的“#”号去掉,并修改 SCRIPT_FILENAME 后面路径为:

SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;

保存退出!

3、添加php测试页

#cd /usr/share/nginx/html

#rm -rf index.html

#vi index.php

<h1>welcome to Nginx !</h1>

<?php

phpinfo();

?>

保存退出!

在浏览器中测试:192.168.1.71

152250854.png

说明nginx和php整合成功,nginx可以解析php网页了!

六、Nginx添加虚拟主机介绍

在有需要的情况下,是会用到nginx虚拟主机的,在一定程度上可以节省资源。

使用yum安装的nginx配置文件中会有这样一行:

# Load config files from the /etc/nginx/conf.d directory

include /etc/nginx/conf.d/*.conf;

虚拟主机就需要在/etc/nginx/conf.d/目录下配置

1、首先修改nginx配置文件/etc/nginx/nginx.conf,修改后如下:

server {

limit_conn addr 10;

listen 80;

server_name www.nginx1.com; //此处修改为第一个虚拟主机的域名

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

root /usr/share/nginx/html;

index index.php index.html index.htm;

}

error_page 404 /404.html;

location = /404.html {

root /usr/share/nginx/html;

}

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/html;

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

# proxy_pass http://127.0.0.1;

#}

# 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 /usr/share/nginx/html$fastcgi_script_name;

include fastcgi_params;

}

}

保存退出!

添加第一个虚拟主机网页显示内容

#vi /usr/share/nginx/html/index.php

<h1>welcome to Nginx 1!</h1>

<?php

phpinfo();

?>

保存退出!

2、创建第二个虚拟主机网页存放路径

#mkdir -pv /www/html

启用/etc/nginx/conf.d/virtual.conf 配置文件,修改为如下:

#

# A virtual host using mix of IP-, name-, and port-based configuration

#

server {

listen 80;

#listen somename:8080;

#server_name somename alias another.alias;

server_name www.nginx2.com; //第二个虚拟主机的域名


location / {

root /www/html; //第二个虚拟主机的网页存放路径

index index.php index.html index.htm; //添加index.php

}


location ~ \.php$ {

root /www/html; //第二个虚拟主机的网页存放路径

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /www/html$fastcgi_script_name;

include fastcgi_params;

}

}

保存退出!

添加第二个虚拟主机网页显示内容

#vi /www/html/index.php

<h1>welcome to Nginx 2!</h1>

<?php

phpinfo();

?>

保存退出!

测试虚拟主机能否正常访问:

在自己物理机进行测试,需要添加host解析(如果原本就有解析,就不需要添加),在hosts中添加:

192.168.1.159 www.nginx1.com

192.168.1.159 www.nginx2.com

在浏览器中进行测试:

www.nginx1.com ,显示如下图:

155346204.png

www.nginx2.com ,显示如下:

155423359.png

显示以上网页信息,说明nginx虚拟主机配置完成!centos 5 使用yum配置LNMP环境