CentOS 7.3.1611 编译安装MySQL5.7.17+Nginx1.10.3+PHP7.1.3

一、准备篇

1、最小化安装CentOS 7.3.1611 系统

2、配置网络

编辑网络配置文件(文件名可能不同)

vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
BOOTPROTO=static            #修改:启用静态IP地址
ONBOOT=yes                  #修改:开启自动启用网络连接
IPADDR0=192.168.1.150       #添加:设置IP地址
PREFIXO0=24                 #添加:子网掩码
GATEWAY0=192.168.1.1        #添加:网关
DNS1=114.114.114.114        #添加:主DNS
DNS2=115.115.115.115        #添加:备用DNS

重启网络,测试网络是否正常

service network restart
ping -c 5 www.baidu.com
3、配置防火墙

开启80端口,开启3306端口,重启防火墙

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
4、关闭SELINUX

编辑SELINUX配置文件,修改添加以下内容

vi /etc/selinux/config    
#SELINUX=enforcing        #修改:注释掉
#SELINUXTYPE=targeted     #修改:注释掉
SELINUX=disabled          #增加

使配置立即生效

setenforce 0
5、安装依赖

升级软件(可选)

yum -y update

安装依赖

yum -y install wget gcc gcc-c++ make cmake  bison bison-devel ncurses ncurses-devel pcre pcre-devel openssl openssl-devel zlib zlib-devel libxml2 libxml2-devel curl libcurl libcurl-devel libwebp libwebp-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel libXpm libXpm-devel freetype freetype-devel gd gd-devel libicu libicu-devel libxslt libxslt-devel
5、下载软件源码包

软件源码包存放位置

cd /usr/local/src

下载MySQL源码包(带boost库),Nginx源码包,PHP源码包,mcrypt库源码包

wget https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-boost-5.7.17.tar.gz  
wget http://nginx.org/download/nginx-1.10.3.tar.gz
wget http://cn2.php.net/distributions/php-7.1.3.tar.gz
wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz

二、安装篇

1、编译安装MySQL

1)安装前准备工作
添加mysql组,创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统

groupadd mysql && useradd -g mysql mysql -s /bin/false

创建MySQL数据库存放目录,并设置权限

mkdir -p /data/mysql && chown -R mysql:mysql /data/mysql

2)编译安装
解压MySQL源码包

cd /usr/local/src && tar zxvf mysql-boost-5.7.17.tar.gz && cd mysql-5.7.17

预编译

cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_BOOST=boost

编译安装

make && make install

3)初始化数据目录
删除系统默认的配置文件(如果默认没有就不用删除)

rm -rf /etc/my.cnf

初始化数据目录(--initialize 表示生成默认密码, --initialize-insecure 表示不生成密码,密码为空)

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

添加 --initialize 后会提示默认生成的密码:

[Note] A temporary password is generated for root@localhost: H3=ZKqpfTl?t  

4)配置并启动
从源码包中复制MySQL配置文件到安装目录,软连接到/etc目录

cp /usr/local/mysql/support-files/my-default.cnf /usr/local/mysql/my.cnf 
ln -s /usr/local/mysql/my.cnf /etc/my.cnf

从源码包中复制MySQL控制脚本,增加执行权限 ,将mysql加入开机启动

cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig mysqld on

编辑控制脚本,指定mysql程序安装路径及mysql数据库存放目录

vi /etc/rc.d/init.d/mysqld
basedir=/usr/local/mysql  
datadir=/data/mysql

启动mysql

service mysqld start

5)把mysql服务加入系统环境变量

vi /etc/profile

在最后添加这一行

export PATH=$PATH:/usr/local/mysql/bin

使环境变量配置立刻生效

source /etc/profile

6)修改root账户密码,授权root账户远程登陆

mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
mysql> exit;
2、编译安装nginx

1)安装前准备工作
添加www组,并创建用户www并加入到www组,不允许www用户直接登录系统

groupadd www && useradd -g www www -s /bin/false

2)编译安装
解压源码包

cd /usr/local/src && tar zxvf nginx-1.10.3.tar.gz && cd nginx-1.10.3

预编译

./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module

编译&&安装

make && make install

3)测试
启动Nginx

/usr/local/nginx/sbin/nginx

在浏览器中打开192.168.1.150地址,会看到nginx欢迎界面,说明nginx安装成功
3)设置nginx开机启动
编辑启动文件,添加下面内容

vi /etc/rc.d/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
#configtest || return $?
stop
sleep 1
start
}
reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

赋予文件执行权限,设置开机启动,重启Nginx

chmod 775 /etc/rc.d/init.d/nginx
chkconfig nginx on
service nginx restart
3、编译安装PHP

1)编译安装mcrypt库(php依赖且yum源中没有)

cd /usr/local/src && tar zxvf libmcrypt-2.5.8.tar.gz && cd libmcrypt-2.5.8 && ./configure && make && make install

2)编译安装
解压源码包

cd /usr/local/src && tar zxvf php-7.1.3.tar.gz && cd php-7.1.3

预编译

./configure \
--prefix=/usr/local/php \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-config-file-path=/usr/local/php/etc \
--with-libxml-dir \
--with-openssl \
--with-zlib \
--with-zlib-dir \
--enable-bcmath \
--with-curl \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-webp-dir \
--with-jpeg-dir \
--with-png-dir \
--with-xpm-dir \
--with-freetype-dir \
--enable-gd-native-ttf \
--with-gettext \
--with-mhash \
--enable-intl \
--enable-mbstring \
--with-mcrypt \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--with-xmlrpc \
--with-iconv-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd \
--with-mysqli \
--with-pdo-mysql

编译安装

make && make install
  1. 设置 php-fpm开机启动
    删除系统自带配置文件,复制php配置文件到安装目录,添加软链接到 /etc目录
rm -rf /etc/php.ini
cp php.ini-production /usr/local/php/etc/php.ini
ln -s /usr/local/php/etc/php.ini /etc/php.ini

拷贝模板文件为php-fpm配置文件,添加软连接到 /etc目录

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf

编辑php-fpm配置文件,取消pid参数前面的分号

vi /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid

拷贝www.conf模板文件

cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

拷贝php-fpm启动文件到启动目录,添加执行权限,设置开机启动

cp /usr/local/src/php-7.1.3/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
chmod 755 /etc/rc.d/init.d/php-fpm
chkconfig php-fpm on

4)配置nginx支持php
修改/usr/local/nginx/conf/nginx.conf 配置文件,需做如下修改

vi /usr/local/nginx/conf/nginx.conf

首行user去掉注释,修改Nginx运行组为www www;必须与/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否则php运行出错

user www www;

取消FastCGI server部分location的注释,注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
  root html;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}

重启nginx,启动php-fpm

service nginx restart
service php-fpm start

把php服务加入系统环境变量,在最后添加下面这一行

vi /etc/profile
export PATH=$PATH:/usr/local/php/bin

使系统环境配置立刻生效

source /etc/profile

5)测试
新建index.php测试文件,调用phpinfo函数

vi /usr/local/nginx/html/index.php

在浏览器中打开192.168.1.150/index.php,就会看到phpinfo页面

到此CentOS 7.3.1611 编译安装MySQL5.7.17+Nginx1.10.3+PHP7.1.3全部完成!

你可能感兴趣的:(CentOS 7.3.1611 编译安装MySQL5.7.17+Nginx1.10.3+PHP7.1.3)