CentOS 7.2.1511 编译安装Nginx1.10.1+MySQL5.7.14+PHP7.0.11

  • Post category:Linux

CentOS7.2.1511编译安装Nginx1.10.1+MySQL5.7.14+PHP7.0.11

1.安装必要软件包和依赖

yum install -y gcc gcc-c++ make zlib-devel openssl-devel pcre-devel 
yum install -y libxml2-devel libjpeg-devel libpng-devel libtool-ltdl-devel 
yum install -y freetype-devel libmcrypt-devel libicu-devel libcurl-devel

2.编译安装MySQL

2.1 下载MySQL

从MySQL官网下载tar.gz格式的压缩包并解压:

cd /usr/local/src/
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.14.tar.gz
tar zxvf mysql-5.7.14.tar.gz
cd mysql-5.7.14

2.2 新建用户和组

groupadd mysql
useradd -r -g mysql mysql

2.3 编译安装

关闭selinux

setenforce 0

进入源代码目录

cd /usr/local/src/mysql-5.7.14/

开始编译

cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=/usr/local/boost \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_ZLIB=system \
-DWITH_SSL=system \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DENABLED_LOCAL_INFILE=1
make && make install

初始化MySQL数据库

cd /usr/local/mysql
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

安装mysql服务器

cd support-files
cp mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql 
chkconfig --add mysql
chkconfig mysql on
chkconfig --list mysql

启动MySQL服务器

systemctl start mysql

登录MySQL并设置密码

cd /usr/local/mysql
./bin/mysql -uroot -p$(cat data/default_password.log)

3.编译安装PHP

3.1 下载PHP

从PHP官网下载tar.gz格式的压缩包并解压,或者使用如下命令下载最新版本:

cd /usr/local/src/
wget https://www.php.net/distributions/php-7.0.11.tar.gz
tar zxvf php-7.0.11.tar.gz
cd php-7.0.11

3.2 编译安装

./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=/usr/local/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 \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-zlib-dir \
--with-xsl \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd
make && make install

3.3 部署PHP-FPM

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

修改配置文件

编辑/usr/local/php/etc/php-fpm.conf,设置以下参数:

user = www
group = www
; 进程数
pm.max_children = 16
; 进程空闲时长
pm.start_servers = 2
; 最小进程数
pm.min_spare_servers = 1
; 最大进程数
pm.max_spare_servers = 3
; pid文件路径
pid = /usr/local/php/var/run/php-fpm.pid
; 错误日志路径
error_log = /usr/local/php/var/log/php-fpm.log

启动PHP-FPM

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

4.编译安装Nginx

4.1 下载Nginx

从Nginx官网下载tar.gz格式的压缩包并解压,或者使用如下命令下载最新版本:

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.10.1.tar.gz
tar zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1

4.2 编译安装

./configure --user=www \
--group=www \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-ipv6 \
--with-pcre \
--with-pcre-jit \
--with-http_realip_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-openssl \
--with-zlib \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-file-aio \
--with-http_auth_request_module
make && make install

4.3 启动Nginx

/usr/local/nginx/sbin/nginx

5.创建网站

5.1 新建网站目录

mkdir -p /data/www/default
echo "<?php phpinfo();" > /data/www/default/index.php

5.2 配置Nginx虚拟主机

将以下内容保存为/usr/local/nginx/conf/vhost/default.conf

server {
    listen       80;
    server_name  localhost;
    location / {
        root   /data/www/default/;
        index  index.html index.htm index.php;
    }

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

5.3 重启Nginx、PHP-FPM、MySQL

/usr/local/nginx/sbin/nginx -s reload
/usr/local/php/sbin/php-fpm -s reload
service mysql restart

至此,CentOS7.2.1511编译安装Nginx1.10.1+MySQL5.7.14+PHP7.0.11完整攻略就完成了。