一、背景

项目需统一安装服务器环境,阿里云的centos6安装,需要部署的环境是Apache2.4.20 + Mysql5.6.30 + PHP5.5.30

文件目录统一路径:/www/

如php放在/www/php,防火墙放在/www/scripts/,mysql放在/www/data


二、参考博客

http://cubix.blog.51cto.com/7251166/1901501


三、配置

1、关闭SELINUX,配置yum源。

setenforce 0

sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config 

##此处使用的是阿里镜像源作为yum源。

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

##下载CentOS6源

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

##根据系统下载好对应版本的源后,需要yum makecache

yum makecache

2、安装Mysql5.6

2.1、检查&安装cmake

rpm -qa cmake

wKioL1mC3I_izc9AAAAKrHWswh4801.png

如未安装,用yum方式安装cmake

yum install cmake -y


安装其他依赖包

yum install gcc gcc-c++ cmake ncurses-devel bison perl  -y

2.2、环境配置

mkdir -p  /www/data

useradd mysql 

chown -R mysql:mysql /www/data/

2.3、安装包准备

cd /usr/local/src/

wget http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.30.tar.gz

tar -xzf mysql-5.6.30.tar.gz 

cd mysql-5.6.30

2.4、编译安装

cmake \

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.30 \

-DMYSQL_DATADIR=/www/data \

-DSYSCONFDIR=/etc/my.cnf \

-DWITH_MYISAM_STORAGE_ENGINE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_MEMORY_STORAGE_ENGINE=1 \

-DWITH_PARTITION_STORAGE_ENGINE=1 \

-DWITH_READLINE=1 \

-DMYSQL_UNIX_ADDR=/www/data/mysqld.sock \

-DENABLED_LOCAL_INFILE=1 \

-DEXTRA_CHARSETS=all \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DMYSQL_TCP_PORT=3306

make && make install

2.5、修改Mysql配置文件

cat  > /etc/my.cnf <<EOF

[mysqld]

datadir=/www/data

socket=/www/data/mysqld.sock 

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

[mysqld_safe]

log-error=/www/data/mysqld.log

pid-file=/www/data/mysqld.pid

EOF

2.6、添加Mysql服务,配置开机自动启动

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

chkconfig --add mysqld

chkconfig mysqld --level 345 on

2.7、初始化数据库,设置Mysql超级用户密码

ln -s /usr/local/mysql-5.6.30 /usr/local/mysql

ln -s /usr/local/mysql/bin/* /usr/bin/

ln -s /usr/local/mysql/include/* /usr/local/include/

/usr/local/mysql/scripts/mysql_install_db  --basedir=/usr/local/mysql  --datadir=/www/data/  --user=mysql

/etc/init.d/mysqld start

/usr/local/mysql/bin/mysql_secure_installation

ctrl+c退出,此时你的Mysql的密码是空的,后面做修改和权限

2.8、登陆测试

wKioL1mC3M7TtFoqAABD7dGUI-8194.png


3、下载Apache、PHP源码包

wget http://apache.fayea.com//apr/apr-1.5.2.tar.gz -O /usr/local/src/apr-1.5.2.tar.gz

wget http://apache.fayea.com//apr/apr-util-1.5.4.tar.gz -O /usr/local/src/apr-util-1.5.4.tar.gz

wget --no-check-certificate https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz -O /usr/local/src/pcre-8.40.tar.gz

##下载Apache-2.4.20

wget http://archive.apache.org/dist/httpd/httpd-2.4.20.tar.gz -O /usr/local/src/httpd-2.4.20.tar.gz

##下载PHP-5.5.30

wget http://cn2.php.net/distributions/php-5.5.30.tar.gz -O  /usr/local/src/php-5.5.30.tar.gz

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz   -O /usr/local/src/libmcrypt-2.5.7.tar.gz

4、安装Apache、PHP

4.1 安装依赖包

yum install -y gcc gcc-c++ cmake ncurses-devel bison openssl openssl-devel libxml2 libxml2-devel libmcrypt libmcrypt-devel zlib zlib-devel libpng libpng-devel libtool jpeg6 libjpeg libjpeg-devel freetype freetype-devel autoconf automake gd libXpm-devel curl curl-devel

4.2 编译安装apr、apr-util、pcre。

cd /usr/local/src/

tar xf apr-1.5.2.tar.gz 

tar xf apr-util-1.5.4.tar.gz 

tar xf pcre-8.40.tar.gz 

tar xf httpd-2.4.20.tar.gz 

tar xf php-5.5.30.tar.gz 

tar xf libmcrypt-2.5.7.tar.gz

cd /usr/local/src/apr-1.5.2

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

make && make install 

cd /usr/local/src/apr-util-1.5.4

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/

make && make install

cd /usr/local/src/pcre-8.40

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

make && make install

4.3 编译安装Apache-2.4.20

cd /usr/local/src/httpd-2.4.20

./configure --prefix=/usr/local/apache-2.4.20 --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre/ --enable-so --enable-modules=most --enable-mods-shared=most --enable-rewrite --enable-ssl  --enable-mpms-shared=all

make && make install

ln -s /usr/local/apache-2.4.20 /usr/local/apache

##配置apache开机自动启动

echo '/usr/local/apache/bin/apachectl start ' >> /etc/rc.local

4.4 编译安装PHP

cd /usr/local/src/libmcrypt-2.5.7

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

make && make install

cd /usr/local/src/php-5.5.30

./configure \

--prefix=/usr/local/php5.5.30 \

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

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

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

--with-libxml-dir \

--with-png-dir \

--with-jpeg-dir \

--with-freetype-dir \

--with-gd \

--with-zlib-dir \

--with-mysqli \

--with-xpm-dir=/usr/lib64/ \

--with-curl \

--enable-soap \

--enable-mbstring=all \

--enable-sockets \

--enable-maintainer-zts \

--with-mcrypt=/usr/local/libmcrypt/

make && make install 

 

ln -s /usr/local/php5.5.30 /usr/local/php

cp /usr/local/src/php-5.5.30/php.ini-production  /usr/local/php/etc/php.ini

5、配置apache站点及防火墙

5.1、配置Apache

cp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.back

mkdir -p /usr/local/apache/conf/conf.d/

cat >>/usr/local/apache/conf/httpd.conf <<EOF

#######

ServerName localhost:80

AddType application/x-httpd-php .php

TraceEnable off

ServerTokens Prod

ServerSignature Off

EOF

##配置站点www.a.com,仅需将"web=www.a.com"修改成对应站点名称,运行以下脚本即可创建虚拟主机以及相应的目录。如没有域名的可以随便用个如www.a.com,后面有域名再去修改/usr/local/apache/conf/conf.d/www.a.com.conf文件

web=www.a.com

mkdir -p /www/$web

 

cat >>/usr/local/apache/conf/httpd.conf <<EOF

########

Include conf/conf.d/$web.conf

########

EOF

 

cat > /usr/local/apache/conf/conf.d/$web.conf <<EOF

<VirtualHost *:80>

    DocumentRoot "/www/$web/"

    ServerName   $web

    ErrorLog "|/usr/local/apache/bin/rotatelogs   /usr/local/apache/logs/$web-%Y%m%d_error.log 86400 480"

    CustomLog "|/usr/local/apache/bin/rotatelogs  /usr/local/apache/logs/$web-%Y%m%d_access.log 86400 480" common

</VirtualHost>

<Directory "/www/$web/">

    Options Indexes FollowSymLinks

    AllowOverride All

    Require all granted

    DirectoryIndex index.php index.Html index.html 

</Directory>

EOF

5.2、配置防火墙

mkdir -p /www/scripts/

cat > /www/scripts/iptables.sh <<EOF 

iptables -P INPUT ACCEPT

iptables -F

#enable the port input

iptables -A INPUT -p tcp --dport 80 -j ACCEPT

iptables -A INPUT -p tcp --dport 22 -j ACCEPT

#如果你要开启mysql让外部可以访问的话就这样配置,如果只是内网访问的话就不要下面这句,开启内网那句

iptables -A INPUT -p tcp --dport 3306 -j ACCEPT

# enable ping

iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT

iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT

#enable the interface input,看你的eth0口是否外网口,如果是则不加下面这句,阿里云的一般都是

wKiom1mC3PqQ5sHLAAAU6t_sCYc669.png

#iptables -A INPUT -i eth0 -j ACCEPT

iptables -A INPUT -i lo -j ACCEPT 

iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP

iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -j DROP

iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j DROP

iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,RST FIN,RST -j DROP

iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,ACK FIN -j DROP

iptables -A INPUT -p tcp -m tcp --tcp-flags PSH,ACK PSH -j DROP

iptables -A INPUT -p tcp -m tcp --tcp-flags ACK,URG URG -j DROP

iptables -P INPUT DROP

iptables -P OUTPUT ACCEPT

iptables -P FORWARD DROP

EOF

chmod +x /www/scripts/iptables.sh

/www/scripts/iptables.sh

## 将防火墙规则加到开机自动启动

echo '/www/scripts/iptables.sh' >>/etc/rc.local

6、编译安装php扩展模块(编译PDO)

wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz -O /usr/local/src/PDO_MYSQL-1.0.2.tgz

 

cd /usr/local/src/

tar xf PDO_MYSQL-1.0.2.tgz

 

cd PDO_MYSQL-1.0.2

/usr/local/php/bin/phpize

./configure -with-php-config=/usr/local/php/bin/php-config -with-pdo-mysql=/usr/local/mysql

make && make install

 

echo 'extension=pdo_mysql.so' >> /usr/local/php/etc/php.ini

/usr/local/apache/bin/apachectl restart

7、修改相关配置文件

7.1、配置php.ini

vi /usr/local/php5.5.30/etc/php.ini

找到date.timezone

修改

date.timezone = Asia/shanghai

7.2、开启rewrite 模块

vi /usr/local/apache/conf/httpd.conf

找到rewrite ,#去掉

/usr/local/apache/bin/apachectl restart(重启apache)


四、配置完成,访问测试

放两个php文件上去访问测试

wKioL1mC3kPhhvy7AAAV5uqDnHw252.png

数据库链接正常

$link =mysql_connect( "127.0.0.1" , "root" , "123456" );
if (! $link echo  "FAILD"

else echo "OK!"

echo "<hr/>";

$link  new  mysqli( 'localhost' 'root' '123456' 'mysql' );
if (! $link echo  "FAILD"

else echo "OK!"

echo "<hr/>";

$db  new  PDO( 'mysql:host=localhost;dbname=mysql' 'root' '123456' );
if (! $db echo  "FAILD"
else  echo  "OK!"


wKiom1mC3k3RcVAPAAAUYGJpcEw021.png

phpinfo查看

wKiom1mC3mvBuUQxAADK3mX3Kao381.png