CentOS下Yum安装Nginx

最近弄了个VPS,后台安装好centos后用SSH登入。
首先查看下是否安装了yum,没有yum可自行安装,网上有的是文章这里不重复了。接着安装EPEL,在命令行敲入:
shell>rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm 回车即可。
shell>yum update #更新。
shell>yum install gcc  gcc-c++
接 下来是安装mysql,我安装的版本是5.1的,据官方介绍5.1版本的性能提升了20%,不管数字是真是假,反正有提升就是了。安装可以选择二进制方式 安装,也可以选择源代码 方式安装。官方推荐二进制方式安装,原因是二进制方式用了许多优化措施,可提供较高的负载。下面分别介绍两种方式的安装方法:
二进制方式:首先要知道自己的cpu架构,下载相应的安装包。我用的是x86 (non RPM packages),32位的,比较通用。
我的vps地址在美国所以选择美国的下载地址
shell>wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.34-linux-i686-glibc23.tar.gz/from/http://mysql.mirror.redwire.net/
shell>tar xzvf mysql-5.1.34-linux-i686-glibc23.tar.gz
shell>mv  mysql-5.1.34-linux-i686-glibc23  mysql
shell>groupadd mysql
shell>useradd   -g mysql mysql
shell>cd mysql
shell>./scripts/mysql_install_db –user=mysql         #user前是两个-
shell>chown -R root .
shell>chown -R mysql data
shell>chgrp -R mysql .
ok ,这就装完了,不用编译挺省事。就是下载包有点大,一百多兆。
启动mysql:
shell>./bin/mysqld_safe  –user=mysql &                 #user前是两个-
源代码方式:
shell>wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.34.tar.gz/from/http://mysql.mirror.redwire.net/
shell>tar xzvf mysql-5.1.34.tar.gz
shell>groupadd mysql
shell>useradd   -g mysql mysql
shell>cd  mysql-5.1.34
shell>./configure –prefix=/usr/local/mysql/
shell>make && make install
shell>cd /usr/local/mysql/share/mysql
shell>cp my-medium.cnf  /etc/my.cnf
shell>cd /usr/local/mysql/
shell>./bin/mysqld_safe  –user=mysql &
如果出现  unknown option ‘–skip-federated’ 错误,只需要将my.cnf 中skip-federated注释掉即可。
接下来安装php
centos 上没有libiconv 的安装包,我们去下载一个
shell>wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.tar.gz
shell>tar  xzvf  libiconv-1.13.tar.gz
sehll>cd  libiconv-1.13
shell>./configure  –prefix=/usr/local/libiconv/
shell>make && make install
下载php5.2.9
shell>wget  http://cn.php.net/get/php-5.2.9.tar.gz/from/uk2.php.net/mirror
shell>tar xzvf php-5.2.9.tar.gz
shell>cd php-5.2.9
shell>./configure –prefix=/usr/local/php/ –with-mysql=/usr/local/mysql/ –enable-fastcgi –enable-force-cgi-redirect  –with-iconv-dir=/usr/local/libiconv
shell>make && make install
安装nginx
首先要安装pcre
shell>yum pcre pcre-devel
shell>wget http://sysoev.ru/nginx/nginx-0.6.35.tar.gz
shell>tar xzvf nginx-0.6.35
shell> cd nginx-0.6.35
shell>./configure –prefix=/usr/local/nginx/
shell>make && make install
nginx 安装完毕, 打开配置文件进行配置
shell>vi /usr/local/nginx/conf/nginx.conf
配置文件请查看这里
启动nginx
shell>/usr/local/nginx/sbin/nginx
如果显示下面的字样则表示启动成功
the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
the configuration file /usr/local/nginx//conf/nginx.conf was tested successfully
接下来安装spawn-fcgi 来启动管理php-cgi
shell>yum install  lighttpd-fastcgi
shell>/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -g www -f /usr/local/php/bin/php-cgi
这样就可以运行PHP程序了。
为方便管理,我写了个简单的脚本 ,保存后 chmod 755 就可以了
#!/bin/sh
fcgi_spawn=”/usr/bin/spawn-fcgi”
fcgi_php=”/usr/local/php/bin/php-cgi”
fcgi_addr=”127.0.0.1″
fcgi_port=9000
fcgi_child=2
fcgi_phpchild=5
user=”www”
group=”www”
cmd=”$fcgi_spawn -a $fcgi_addr -p $fcgi_port -u $user -g $group -F $fcgi_child -C $fcgi_phpchild -f $fcgi_php”
PHP_FCGI_MAX_REQUESTS=1000
export PHP_FCGI_MAX_REQUESTS
case $1 in
start)
echo ’starting nginx…’
echo /usr/local/nginx/sbin/nginx
echo ’starting fastcgi’
ret=$cmd
“nginx_server” 42L, 798C
#!/bin/sh
fcgi_spawn=”/usr/bin/spawn-fcgi”
fcgi_php=”/usr/local/php/bin/php-cgi”
fcgi_addr=”127.0.0.1″
fcgi_port=9000
fcgi_child=2
fcgi_phpchild=5
user=”www”
group=”www”
cmd=”$fcgi_spawn -a $fcgi_addr -p $fcgi_port -u $user -g $group -F $fcgi_child -C $fcgi_phpchild -f $fcgi_php”
PHP_FCGI_MAX_REQUESTS=1000
export PHP_FCGI_MAX_REQUESTS
case $1 in
start)
echo ’starting nginx…’
echo /usr/local/nginx/sbin/nginx
echo ’starting fastcgi’
ret=$cmd
echo $ret
;;
restart)
echo ’stopping fastcgi’
killall php-cgi
echo ’stopping nginx’
killall nginx
echo ’starting nginx…’
echo /usr/local/nginx/sbin/nginx
echo ’starting fastcgi’
echo $cmd
;;
stop)
echo ’stopping php-cgi…’
killall php-cgi
echo ’stopping nginx…’
killall nginx
echo ’stopping complete’
;;
esac