1. Centos 编译环境搭建:
yum -y install yum-fastestmirror

使用fastestmirror插件给yum加速

yum check-update

安装一些必要的软件:

yum -y install gcc gcc-c++ screen autoconf automake libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel pcre pcre-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libtiff libtiff-devel gettext gettext-devel pam pam-devel fontconfig-devel libXpm-devel libtool

关闭SELINUX

/usr/sbin/setenforce 0 立刻关闭 SELINUX
vi /etc/sysconfig/selinux
SELINUX=disabled
mkdir /downloads 将软件放到此目录下:
Nginx 使用80端口,Apache2使用8080端口,且使PHP分别支持Nginx和Apache2,所以需要编译安装两次PHP。
2. 安装mysql:
groupadd mysql
useradd -g mysql -s /sbin/nologin -M  mysql
tar zxvf mysql-5.1.63.tar.gz
cd mysql-5.1.63

./configure  \

--prefix=/usr/local/mysql   \
--with-unix-socket-path=/tmp/mysql.sock   \
--enable-thread-safe-client   \
--with-mysqld-user=mysql   \
--with-big-tables   \
--without-debug   \
--with-pthread   \
--enable-assembler   \
--with-extra-charsets=complex   \
--with-readline   \

--with-ssl   \

--with-embedded-server   \
--enable-local-infile   \
--with-plugins=partition,innobase   \
--with-plugin-PLUGIN   \
--with-mysqld-ldflags=-all-static   \
--with-client-ldflags=-all-static
make && make install

建立配置文件

cp support-files/my-medium.cnf /etc/my.cnf

初始化数据库,并修改相关目录所有权

/usr/local/mysql/bin/mysql_install_db --user=mysql

chown -R root.mysql /usr/local/mysql/
chown -R mysql /usr/local/mysql/var/

调整lib库路径

echo "/usr/local/mysql/lib/mysql/" >>/etc/ld.so.conf

ldconfig

使用mysqld_safe脚本安全启动服务(后台)

/usr/local/mysql/bin/mysqld_safe --user=mysql &

查看一下mysqld是否启动

netstat -ntpl | grep 3306

将mysql添加为系统服务

cp support-files/mysql.server /etc/init.d/mysqld

chmod a+x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on

把MySQL加入环境变量(设置mysql的执行路径)

echo "export PATH=$PATH:/usr/local/mysql/bin">>/etc/profile

source /etc/profile // 使环境变量生效

设置"root"用户密码,登陆且验证数据库正确安装

mysqladmin -u root password 1234
mysql -u root -p

mysql>use mysql;
mysql>select Host,User,Password,Select_priv,Grant_priv from user;
mysql>delete from user where user='';                 
(删除user为空的用户)
mysql>delete from user where password='';               (删除密码为空的用户)
mysql>delete from user where host='';                     (删除主机名为空的用户)
mysql>drop database test;             (删除默认的test数据库)
mysql>flush privileges;               (刷新mysql的缓存,让以上设置立即生效)
mysql>quit;

为了使以上优化和安全设置生效,请重启Mysql服务或Linux。

service mysqld restart
3. 安装Nginx:
PCRE 是perl所用到的正则表达式,目的是让所装的软件支持正则表达式。默认情况下,Nginx只处理静态的网页请求,也就是html.如果是来自动态的网页请求,比如*.php,那么Nginx就要根据正则表达式查询路径,然后把*.PHP交给PHP去处理。
rpm -qa | grep pcre
// 查询系统中有没有安装PCRE,一般装系统是默认装有,所以我们要删掉系统自带的

find / -name  libpcre.so.0

cp /lib/libpcre.so.0 /
// 在删除系统自带的PCRE之前,要先备份一下libpcre.so.0这个文件,因为RPM包的关联性太强,在删除后没libpcre.so.0这个文件时我们装PCRE是装不上的
rpm -e --nodeps pcre-6.6-6
// 删除系统自带的PCRE
unzip pcre-8.10.zip
cd pcre-8.10
cp /libpcre.so.0 /lib/
 
// 把我们删除系统自带的PCRE之前备份的libpcre.so.0拷贝到/lib 目录下
./configure
// 配置PCRE,因为PCRE是一个库,而不是像pache、php、postfix等这样的程序,所以我们安装时选择默认路径即可,这样会在后面安装其它东西时避免一些不必要的麻烦,执行完这部后会显示出下图,上面显示了我们对PCRE的配置
make && make install

安装Nginx

groupadd www # 添加www组
useradd -g www -s /sbin/nologin -M www # 创建nginx运行账户www并加入到www组
tar xf nginx-1.2.1.tar.gz
cd nginx-1.2.1
./configure \
 --prefix=/usr/local/nginx \
 --user=www \

 --group=www \

 --with-http_stub_status_module \
 --with-http_ssl_module \
 --with-http_realip_module \
 --with-http_sub_module \
 --with-http_gzip_static_module \
 --with-http_flv_module \
 --with-http_dav_module \
 --with-debug \
 --with-pcre=/downloads/pcre-8.10

注意:--with-pcre=/usr/local/src/pcre-8.10指向的是源码包解压的路径,而不是安装的路径,否则会报错

make
make install
/usr/local/nginx/sbin/nginx # 启动nginx

echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local

编辑 /etc/rc.d/init.d/nginx 文件,覆盖为以下代码:(脚本里也有apache的启动,若没有安装apache会报错,可以先注释掉)

vi /etc/rc.d/init.d/nginx
 
#! /bin/sh
ulimit -n 65535
# Description: Startup script for nginx
# chkconfig: 2345 55 25
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/nginx.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {

$DAEMON -c $CONFIGFILE || echo -n "nginx already running"

}
do_stop() {

kill -QUIT `cat $PIDFILE` || echo -n "nginx not running"

}
do_reload() {

kill -HUP `cat $PIDFILE` || echo -n "nginx can't reload"

}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
/etc/init.d/httpd start
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
/etc/init.d/httpd stop
;;
reload)

echo -n "Reloading $DESC configuration..."

do_reload
echo "."
/etc/init.d/httpd restart
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
sleep 1
do_start
echo "."
/etc/init.d/httpd restart
;;
*)

echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2

exit 3
;;
esac
exit 0

添加到开机自启动

chmod a+x /etc/rc.d/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
ps aux | grep nginx

Nginx启动后有两个进程,master为主进程,worker为工作进程,如下图

elinks 127.0.0.1

4. 安装 PHP 支持 Nginx
安装一些库文件,使PHP 支持GD 库。( 默认先不要安装,先用yum 安装这些库文件,如果有问题再用编译安装)

//编译安装zlib
>tar zxvf zlib-1.2.5.tar.gz
>cd zlib-1.2.5
>./configure --prefix=/usr/local/zlib
>make
>make install

//
编译安装libpng
>tar zxvf libpng-1.5.1.tar.gz
>cd libpng-1.5.1

./configure –prefix=/usr/local/png


>make
>make install
ln -s /usr/local/png/lib/* /usr/lib/

 


//
编译安装freetype
>tar zxvf freetype-2.4.4.tar.gz
>cd freetype-2.4.4
>./configure --prefix=/usr/local/freetype
>make
>make install
//
编译安装jpeg
>tar zxvf jpegsrc.v8c.tar.gz
>cd jpeg-8c/
>./configure --prefix=/usr/local/libjpeg --enable-shared --enable-static
>make
>make install

ln -s /usr/local/jpeg/lib/* /usr/lib/

//
编译安装fontconfig
>tar zxvf fontconfig-2.8.0.tar.gz
>cd fontconfig-2.8.0

#./configure --prefix=/usr/local/fontconfig --with-freetype-config=/usr/local/freetype/bin/freetype-config
>make
>make install

//
编译安装libxml
>tar zxvf libxml2-2.7.4.tar.gz
>cd libxml2-2.7.4
>./configure
>make
>make install


编译安装GD
>tar zxvf gd-2.0.35.tar.gz
>cd gd-2.0.35
> ./configure --prefix=/usr/local/gd
>make && make install

编译时显示以下信息:

** Configuration summary for gd 2.0.34:

   Support for PNG library:          yes
   Support for JPEG library:         yes
   Support for Freetype 2.x library: yes
   Support for Fontconfig library:   yes
   Support for Xpm library:          no
   Support for pthreads:             yes

 
编译安装PHP

tar xf libiconv-1.14.tar.gz

cd libiconv-1.14

./configure --prefix=/usr/local/

make && make install

 

tar xf libmcrypt-2.5.8.tar.gz

cd libmcrypt-2.5.8

./configure

make && make install

Ldconfig

cd libltdl/

./configure --enable-ltdl-install

make && make install

 

tar xf mhash-0.9.9.9.tar.gz

cd mhash-0.9.9.9

./configure

make && make install

 

ehco "/usr/local/lib" >>/etc/ld.so.conf

ldconfig
或者

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ldconfig

 

tar xf mcrypt-2.6.8.tar.gz

./configure

make && make install

 
安装PHP

默认情况下NginxPHP他俩之间是一点感觉没有的。在之前,很多朋友都搭建过Apache+PHPApache+PHP编译后生成的是模块文件,而Nginx+PHP需要PHP生成可执行文件才可以,所以要利用fastcgi技术来实现N ginxPHP的整合,这个只要我们安装是启用FastCGI即可。此次我们安装PHP不仅使用了FastCGI,而且还使用了PHP-FPM这么一个东东,PHP-FPM说白了是一个管理FastCGI的一个管理器,它作为PHP的插件纯在,在安装PHP要想使用PHP-FPM时就需要把PHP-FPM以补丁的形式安装到PHP中,而且PHP要与PHP-FPM版本一致,这是必须的,切记!

首先我们把PHP PHP-FPM 下载到同一目录下。
 

tar xf php-5.2.17.tar.gz

gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1

cd php-5.2.17

 ./configure \
 --prefix=/usr/local/php \
 --with-config-file-path=/usr/local/php/etc \
 --with-mysql=/usr/local/mysql \

 --with-mysqli=/usr/local/mysql/bin/mysql_config \

 --with-iconv-dir=/usr/local \
 --with-freetype-dir \
 --with-jpeg-dir \
 --with-png-dir \
 --with-zlib \
 --with-mhash \
 --with-libxml-dir \
 --enable-xml \
 --disable-rpath \
 --enable-discard-path \
 --enable-safe-mode \
 --enable-bcmath \
 --enable-shmop \
 --enable-sysvsem \
 --enable-inline-optimization \
 --with-curl \
 --with-curlwrappers \
 --enable-mbregex \
 --enable-fastcgi \
 --enable-fpm \
 --enable-force-cgi-redirect \
 --enable-mbstring \
 --with-mcrypt \
 --with-gd \
 --enable-gd-native-ttf \
 --with-openssl \
 --enable-pcntl \
 --enable-sockets \
 --enable-ftp \
 --with-ldap \
 --with-ldap-sasl \
 --with-xmlrpc \
 --enable-zip \
 --enable-soap \
 --without-pear

make ZEND_EXTRA_LIBS='-liconv'

make install

cp php.ini-dist /usr/local/php/etc/php.ini

ln -s /usr/local/php/bin/php /usr/bin/php

ln -s /usr/local/php/bin/phpize /usr/bin/phpize

ln -s /usr/local/php/sbin/php-fpm /usr/bin/php-fpm

下面我们就要启动PHP-FPM

/usr/local/php/sbin/php-fpm start

更改PHP-FPM
在启动PHP-FPM 时会报上面这个错误,原因是PHP-FPM 自己不知道以那个用户和组运行PHP ,所以我们要修改一个文件,把文件中的注释去掉即可(打开文件把红色部分删除),然后PHP-FPM 会以nobody 用户和组去运行PHP

vi /usr/local/php/etc/php-fpm.conf

/usr/local/php/sbin/php-fpm start

cp sapi/cgi/fpm/php-fpm /etc/init.d/php-fpm

chmod a+x /etc/init.d/php-fpm

echo "/usr/local/php/sbin/php-fpm start" >>/etc/rc.local

整合Nginx PHP
Nginx 自己并不处理动态网页的请求,而且Nginx 将得到的动态请求转交给PHP ,下面我们打开Nginx 的配置文件看一下

vi /usr/local/nginx/conf/nginx.conf         //标的部分是我们后面要修改的

看上图,Nginx 已经知道怎么把得到的请求传达给PHP Nginx 在得到*.php 请求时,会把请求通过9000 端口传给PHP 。下面我们把这些注释给去掉即可,如下图

 注:上面的/usr/local/nginx/html 是我们PHP网站放置的路径

Nginx 自己知道咋找PHP 了还不行,还需要PHP 知道咋找Nginx
PHP-FPM 已经在配置文件中定义了从哪接受PHP 请求,我们可以打开配置文件看一下

vi /usr/local/php/etc/php-fpm.conf

PHP 自己是从本机的9000 端口侦听数据 Nginx PHP 通过本机的9000 端口完成了数据请求。

 
我们在nginx 的配置文件里面已经定义了PHP 网站的存放路径,路径问/usr/local/nginx/html
下面我们在这个目录下新建一个PHP 页面测试网页,文件名为test.php ,内容如下
<?php

echo phpinfo();

?>

重启PHPnginx(可以用杀死进程的方式关闭,然后在启动)我们在浏览器中输入http://localhost/test.php,出现如下界面算成功

测试PHP 与数据库的连接
<?php
$LINK=mysql_connect('127.0.0.1','root','1234');
if ($LINK)
echo "success...";
else
echo "Failure...";
mysql_close();
?>

5.安装Apache2:

tar zxvf httpd-2.4.2.tar.gz

./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite

make && make install

vi /usr/local/apache2/conf/httpd.conf

将端口改为8080

cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd

chmod a+x /etc/init.d/httpd

echo "/usr/local/apache2/bin/apachectl start" >>/etc/rc.local

/etc/init.d/httpd start

6. 安装 PHP 支持 Apache2

tar zxvf php-5.2.17.tar.gz

cd php-5.2.17/

 ./configure \

 --prefix=/usr/local/php2 \

 --with-apxs2=/usr/local/apache2/bin/apxs \

 --with-config-file-path=/usr/local/php2/etc \

 --with-mysql=/usr/local/mysql \

 --with-mysqli=/usr/local/mysql/bin/mysql_config \

 --with-curl \

 --with-iconv-dir=/usr/local \

 --with-freetype-dir \

 --with-jpeg-dir \

 --with-png-dir \

 --with-zlib \

 --with-mhash \

 --with-libxml-dir \

 --with-gd \

 --enable-gd-native-ttf \

 --enable-mbstring \

 --enable-sockets \

 --enable-shared

如果编译的时候忘记编译一些参数,可以追加编译,如追加编译mysqli curl

追加编译安装mysqli

cd ext/mysqli/    # php5.2.17版本 安装目录下

/usr/local/php/bin/phpize        #生成configure

./configure --prefix=/usr/local/php2    \
 --with-php-config=/usr/local/php2/bin/php-config   \
 --enable-embedded-mysqli=shared   \
 --with-mysqli=/usr/local/mysql/bin/mysql_config  \
 --enable-shared

make

make install

成功后会提示安装到哪个目录(: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/)

cp /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/mysqli.so /usr/local/php/lib/

vim /usr/local/php/lib/php.ini

指定目录用绝对路径

extension_dir = "/usr/local/php2/lib"

extension=mysqli.so

OK  重启相应服务就可以了

追加编译安装curl

cd ext/curl
/usr/local/php/bin/phpize
./configure ==prefix=/usr/local/curl
./configure --with-php-config=/usrl/local/php/bin/php-config
make
make install

编译php

make ZEND_EXTRA_LIBS='-liconv'

make install

copy生成PHP配置文件

cp php.ini-dist /usr/local/php2/etc/php.ini

vi  /usr/local/php2/etc/php.ini
register_globals=On      
 //修改处

vi /usr/local/apache2/conf/httpd.conf
  查找:

AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
在其下添加:

AddType application/x-tar .tgz

AddType application/x-httpd-php .php 

AddType p_w_picpath/x-icon .ico
AddType application/x-httpd-php-source .phps
修改DirectoryIndex 行,添加index.php

DirectoryIndex index.php index.html index.html.var

查找是否有这行,没有的话要添加。

LoadModule php5_module        modules/libphp5.so
测试php脚本,写入如下代码
vi /usr/local/apache/htdocs/test.php

添加以下行:

       <?php

              phpinfo();

       ?>

wq保存退出。

/usr/local/apache2/bin/apachectl restart

在浏览器中输入:http://IP:8080/test.php进行测试。

测试PHP与数据库的连接

vi /usr/local/apache/htdocs/sql.php

<?php

$LINK=mysql_connect('127.0.0.1','root','1234');

if ($LINK)

echo "success...";

else

echo "Failure...";

mysql_close();

?>

/usr/local/apache2/bin/apachectl restart

在浏览器中输入:http://IP:8080/sql.php进行测试。

干嘛儿网-电影频道、最新电影、高清影视:http://v.ganmaer.com/