64位Linux下编译搭建Nginx1.5与PHP5.5(CentOS6.4)

时间:2021-05-17 09:59:22

(1)安装Nginx1.5.2
更新Nginx和PHP的依赖包
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 krb5-devel libidn libidn-devel \
openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients \
openldap-servers gd gd2 gd-devel gd2-devel perl-CPAN

安装Nginx所需的pcre库
tar zxvf pcre-8.33.tar.gz
cd pcre-8.33
./configure
make && make install
ln -s /usr/local/lib/libpcre.so.1 /usr/lib64/libpcre.so.1
cd ../

tar zxvf nginx-1.5.2.tar.gz
cd nginx-1.5.2
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module \
--with-http_ssl_module --with-http_realip_module --with-http_image_filter_module
make
make install
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

创建web根目录并修改权限,当然可以指定其他目录,只要和Nginx配置文件一致即可
mkdir -p /data0/htdocs/www
chmod +w /data0/htdocs/www
chown -R www:www /data0/htdocs/www

vi /usr/local/webserver/nginx/conf/nginx.conf
修改#user  nobody;为user www www;
修改location /{}中的root html;为网站目录例如/data0/htdocs/www
在/data0/htdocs/www目录中随便创建一个index.html文件 
启动nginx并访问测试
ulimit -SHn 65535
/usr/local/webserver/nginx/sbin/nginx
其他命令:
测试nginx的配置文件是否正确/usr/local/webserver/nginx/sbin/nginx -t
修改nginx配置文件后使之生效/usr/local/webserver/nginx/sbin/nginx -s reload

(2)安装PHP
安装PHP所需依赖包
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local
make
make install
cd ../

tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../

tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
cd ../

对共享库做符号链接
ln -s /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib64/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib64/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib64/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
cd ../

tar zxvf php-5.5.0.tar.gz

cd php-5.5.0

./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc \
--with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config \
--with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr \
--enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization \
--with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf \
--with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap \
--enable-opcache=no --without-pear --disable-fileinfo

如果内存较大 可以去掉--disable-fileinfo

make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-development /usr/local/webserver/php/etc/php.ini
cd ../
cp /usr/local/webserver/php/etc/php-fpm.conf.default /usr/local/webserver/php/etc/php-fpm.conf

修改
user = nobody
group = nobody

user = www
group = www
将;pid = run/php-fpm.pid前的;去掉并修改为
pid = /usr/local/webserver/php/var/run/php-fpm.pid

启动php-fpm
/usr/local/webserver/php/sbin/php-fpm

(3)配置Nginx支持PHP,并支持pathinfo
vi /usr/local/webserver/nginx/conf/fastcgi.conf
将fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;前加#注释
并在最上方添加
#------------------------------------------------------
fastcgi_pass   127.0.0.1:9000;
#fastcgi_index  index.php;

set $path_info "";
set $real_fastcgi_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
        set $real_fastcgi_script_name $1;
        set $path_info $2;
}
fastcgi_param  SCRIPT_NAME        $real_fastcgi_script_name;
fastcgi_param  PATH_INFO $path_info;
#------------------------------------------------------
vi /usr/local/webserver/nginx/conf/nginx.conf
找到第一个server中的location /{***} 然后添加
#------------------------------------------------------
     location ~* .*\.php($|/){
                 include  fastcgi.conf;
        }
#------------------------------------------------------
使配置文件生效
kill -USR2 `cat /usr/local/webserver/php/var/run/php-fpm.pid`
/usr/local/webserver/nginx/sbin/nginx -s reload

将Nginx与fpm加入自启动
vi /etc/rc.local
输入
ulimit -SHn 65535
/usr/local/webserver/php/sbin/php-fpm
/usr/local/webserver/nginx/sbin/nginx
=====================================================
编译PHP扩展模块memcache、pdo_mysql、imagick
tar zxvf memcache-2.2.7.tgz
cd memcache-2.2.7
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../

tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-pdo-mysql=/usr/local/webserver/mysql
ln -s /usr/local/webserver/mysql/include/* /usr/local/include/
make
make install
cd ../

tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
make
make install
cd ../

tar zxvf imagick-3.1.0RC2.tgz
cd imagick-3.1.0RC2
/usr/local/webserver/php/bin/phpize
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../
修改php的配置文件
vi /usr/local/webserver/php/etc/php.ini
查找;extension_dir = "/" 将前面的;去掉并修改为
extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20121212/"
并加入
extension=memcache.so
extension=pdo_mysql.so
extension=imagick.so
执行kill -USR2 `cat /usr/local/webserver/php/var/run/php-fpm.pid`
使配置文件生效
------------------------------------------------------------------
(4)nginx的其他配置及优化
将;gzip on前的;去掉开启gzip压缩
并加入详细参数

gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

如果需要将所有请求都重写到index.php在location /{}中添加如下代码即可
if (!-e $request_filename){
    rewrite ^(.*)$ /index.php/$1 last;

}