CentOS 6.5编译安装Nginx 1.10.2+MySQL 5.5.52+PHP5.5.38

  • Post category:Linux

CentOS6.5编译安装Nginx1.10.2+MySQL5.5.52+PHP5.5.38攻略

CentOS6.5编译安装Nginx1.10.2+MySQL5.5.52+PHP5.5.38的过程可以分为以下几步:

1.安装依赖库

$ yum -y install gcc gcc-c++ make automake autoconf libtool wget
$ yum -y install gd gd-devel 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

2.下载源码

$ wget http://nginx.org/download/nginx-1.10.2.tar.gz
$ wget http://downloads.mysql.com/archives/mysql-5.5/mysql-5.5.52.tar.gz
$ wget http://nl3.php.net/distributions/php-5.5.38.tar.gz

3.编译安装MySQL

$ tar -zxvf mysql-5.5.52.tar.gz
$ cd mysql-5.5.52
$ cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS:STRING=utf8,gbk \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_SSL:STRING=system \
-DWITH_READLINE=1 \
-DWITH_ZLIB=system
$ make
$ make install
$ cd /usr/local/mysql
$ cp support-files/my-medium.cnf /etc/my.cnf
$ ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
$ chown -R mysql.mysql /usr/local/mysql/data
$ chmod 755 /usr/local/mysql/support-files/mysql.server
$ cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
$ chkconfig --add mysqld
$ chkconfig --level 345 mysqld on
$ service mysqld start

4.编译安装PHP

$ tar -zxvf php-5.5.38.tar.gz
$ cd php-5.5.38
$ ./configure --prefix=/usr/local/php \
--with-config-file-path=/etc \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-mysql \
--with-mysqli \
--with-pdo-mysql \
--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-mbstring \
--enable-intl \
--enable-pcntl \
--enable-sockets \
--with-openssl \
--with-mcrypt \
--enable-zip \
--with-bz2 \
--with-pcre-regex \
--with-gettext
$ make
$ make install
$ cd /usr/local/php/etc
$ cp php-fpm.conf.default php-fpm.conf
$ cp php-fpm.d/www.conf.default php-fpm.d/www.conf
$ sed -i 's/\;pid/pid/' /usr/local/php/etc/php-fpm.conf
$ sed -i 's/127.0.0.1/0.0.0.0/g' /usr/local/php/etc/php-fpm.d/www.conf
$ cp /usr/local/php/etc/php.ini-development /usr/local/php/etc/php.ini

5.编译安装Nginx

$ tar -zxvf nginx-1.10.2.tar.gz
$ cd nginx-1.10.2
$ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
$ make
$ make install

6.配置Nginx

$ cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
$ vi /usr/local/nginx/conf/nginx.conf

将nginx.conf文件中的server配置修改为:

location ~ \.php$ {
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;
}

7.启动服务

$ /usr/local/nginx/sbin/nginx
$ /etc/init.d/php-fpm start

示例说明

示例1:配置PHP-FPM的监听端口

在3.编译安装PHP中,如果需要将PHP-FPM监听端口修改为8000,可以在./configure时加入--with-fpm-port=8000,命令如下:

$ ./configure --prefix=/usr/local/php \
--with-config-file-path=/etc \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-mysql \
--with-mysqli \
--with-pdo-mysql \
--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-mbstring \
--enable-intl \
--enable-pcntl \
--enable-sockets \
--with-openssl \
--with-mcrypt \
--enable-zip \
--with-bz2 \
--with-pcre-regex \
--with-gettext \
--with-fpm-port=8000

示例2:在Nginx中配置虚拟主机

首先在nginx.conf文件中加入以下内容:

http {
    server {
        listen 80;
        server_name www.example.com;
        root /web/www.example.com;
        index index.html index.htm index.php;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

然后创建目录/web/www.example.com,并在该目录下新建index.php文件,内容如下:

<?php 
echo "Hello, world!"; 
?>

最后重启Nginx服务即可生效:

$ /usr/local/nginx/sbin/nginx -s reload