一、安装环境
Linux系统:CentOS 6.5
Apache版本:http-2.4.12
MySQL版本:MySQL 5.6.24
PHP版本:PHP-5.6.8
基本的安装顺序为:先安装httpd,然后安装mysql,最后安装PHP。以下步骤就是安装此思路进行的:
二、安装httpd-2.4.12
1、在安装httpd之前需要先安装apr、apr-util以及pcre-devel等相关包。由于CentOS是最小化安装的,在编译安装httpd之前需要Development Tools开发工具集。具体步骤如下:
[root@localhost ~]# yum groupinstall Development Tools -y [root@localhost ~]# yum install pcre-devel -y [root@localhost ~]# wget http://mirrors.hust.edu.cn/apache/apr/apr-util-1.5.4.tar.bz2 [root@localhost ~]# wget http://mirrors.hust.edu.cn/apache/apr/apr-1.5.2.tar.bz2 [root@localhost ~]# wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.12.tar.bz2 [root@localhost ~]# tar xf apr-1.5.2.tar.bz2 [root@localhost ~]# tar xf apr-util-1.5.4.tar.bz2 [root@localhost apr-1.5.2]# ./configure --prefix=/usr/local/apr [root@localhost apr-1.5.2]# make;make install [root@localhost apr-1.5.2]# cd ../apr-util-1.5.4 [root@localhost apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [root@localhost apr-util-1.5.4]# make;make install [root@localhost apr-util-1.5.4]# cd
2、接着安装httpd
[root@localhost ~]# tar xf httpd-2.4.12.tar.bz2 [root@localhost ~]# cd httpd-2.4.12 [root@localhost httpd-2.4.12]# yum install openssl-devel -y [root@localhost httpd-2.4.12]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-rewrite --enable-ssl --enable-cgi --enable-cgid --enable-modules=most --enable-mods-shared=most --enable-mpms-modules=all --with-include-apr --with-apr-util=/usr/local/apr-util/ [root@localhost httpd-2.4.12]# make [root@localhost httpd-2.4.12]# make install [root@localhost httpd-2.4.12]# cd
3、进行简单测试
[root@localhost ~]# service iptables stop #先关闭防火墙 [root@localhost apache]# cd /usr/local/apache/ [root@localhost apache]# bin/apachectl start [root@localhost apache]# netstat -tnlp | grep 80 tcp 0 0 :::80 :::* LISTEN 7753/httpd
在宿主机上使用浏览器进行测试,如果看到如下页面即表示httpd安装成功。
4、为了更好的管理和使用httpd服务,可为其提供PATH环境变量和SysV风格的脚本。
PATH环境变量:
[root@localhost apache]# vi /etc/profile.d/httpd.sh export PATH=$PATH:/usr/local/apache/bin [root@localhost apache]# . /etc/profile.d/httpd.sh [root@localhost apache]# echo $PATH /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin:/usr/local/apache/bin
SysV风格的脚本
[root@localhost apache]# vim /etc/init.d/httpd #!/bin/bash # # chkconfig: - 85 15 # description: Apache is a World Wide Web server. It is used to serve \ . /etc/rc.d/init.d/functions if [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpd fi # Start httpd in the C locale by default. HTTPD_LANG=${HTTPD_LANG-"C"} # This will prevent initlog from swallowing up a pass-phrase prompt if # mod_ssl needs a pass-phrase from the user. INITLOG_ARGS="" # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server # with the thread-based "worker" MPM; BE WARNED that some modules mays not # work correctly with a thread-based MPM; notably PHP will refuse to start. # Path to the apachectl script, server binary, and short-form for messages. apachectl=/usr/local/apache/bin/apachectl httpd=${HTTPD-/usr/local/apache/bin/httpd} prog=httpd pidfile=${PIDFILE-/var/run/httpd.pid} lockfile=${LOCKFILE-/var/lock/subsys/httpd} RETVAL=0 start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} -d 10 $httpd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} } reload() { echo -n $"Reloading $prog: " if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t &> /dev/null; then RETVAL=$? echo $"not reloading due to configuration sysntax error" else killproc -p ${pidfile} $httpd -HUP RETVAL=$? fi echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart) if [ -f ${pidfile} ]; then stop start fi ;; reload) reload ;; graceful|htlp|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo "Usage: $prog {start|stop|restart|reload|status|help}" exit 1 ;; esac exit $RETVAL [root@localhost apache]# chmod +x /etc/init.d/httpd [root@localhost apache]# chkconfig --add httpd #添加到服务列表 [root@localhost apache]# chkconfig httpd on [root@localhost apache]# chkconfig --list httpd httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@localhost apache]# vim /etc/httpd/httpd.conf 在httpd.conf中修改及添加如下内容: Pidfile "/var/run/httpd.pid" #此处为添加内容 ServerName www.junjie.com:80 #此处为修改的内容 [root@localhost apache]# service httpd start Starting httpd: [ OK ] [root@localhost apache]# service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
5、为httpd添加man帮助及输出头文件
[root@localhost apache]# vi /etc/man.config MANPATH /usr/local/apache/man [root@localhost apache]# ln -sv /usr/local/apache/include/ /usr/include/httpd `/usr/include/httpd' -> `/usr/local/apache/include/'
至此,httpd服务安装成功。
三、安装MySQL
1、创建mysql用户和mysql组
[root@localhost ~]# groupadd -r -g 306 mysql [root@localhost ~]# useradd -r -g 306 -u 306 mysql [root@localhost ~]# id mysql uid=306(mysql) gid=306(mysql) groups=306(mysql)
2、创建mysql数据存放目录
在系统中新添加一块磁盘,在该盘上创建LVM分区并挂载至数据目录。具体步骤如下:(添加磁盘的过程不再演示)
[root@localhost ~]# fdisk /dev/sdb n p 1 回车 +5G t 8e w [root@localhost ~]# pvcreate /dev/sdb1 [root@localhost ~]# vgcreate myvg /dev/sdb1 [root@localhost ~]# lvcreate -L 5G -n data myvg [root@localhost ~]# mkfs.ext3 /dev/myvg/data [root@localhost ~]# mkdir /mydata/data -pv mkdir: created directory `/mydata' mkdir: created directory `/mydata/data' [root@localhost ~]# mount /dev/myvg/data /mydata/data/
3、解压并安装MySQL 5.6.24,使用的通用二进制格式的MySQL。
[root@localhost ~]# wget http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.6/mysql-5.6.24-linux-glibc2.5-i686.tar.gz [root@localhost ~]# tar xf mysql-5.6.24-linux-glibc2.5-i686.tar.gz -C /usr/local/ [root@localhost ~]# cd /usr/local/ [root@localhost local]# ln -sv mysql-5.6.24-linux-glibc2.5-i686/ mysql `mysql' -> `mysql-5.6.24-linux-glibc2.5-i686/' [root@localhost local]# cd mysql [root@localhost mysql]# chown -R mysql.mysql . #更改mysql目录中的文件的属主、属组为mysql用户和mysql组 [root@localhost mysql]# ll total 172 drwxr-xr-x. 2 mysql mysql 4096 May 6 14:01 bin -rw-r--r--. 1 mysql mysql 17987 Mar 26 00:34 COPYING drwxr-xr-x. 3 mysql mysql 4096 May 6 14:01 data drwxr-xr-x. 2 mysql mysql 4096 May 6 14:02 docs drwxr-xr-x. 3 mysql mysql 4096 May 6 14:01 include -rw-r--r--. 1 mysql mysql 102986 Mar 26 00:35 INSTALL-BINARY drwxr-xr-x. 3 mysql mysql 4096 May 6 14:02 lib drwxr-xr-x. 4 mysql mysql 4096 May 6 14:01 man drwxr-xr-x. 10 mysql mysql 4096 May 6 14:01 mysql-test -rw-r--r--. 1 mysql mysql 2496 Mar 26 00:34 README drwxr-xr-x. 2 mysql mysql 4096 May 6 14:01 scripts drwxr-xr-x. 28 mysql mysql 4096 May 6 14:01 share drwxr-xr-x. 4 mysql mysql 4096 May 6 14:02 sql-bench drwxr-xr-x. 2 mysql mysql 4096 May 6 14:01 support-files [root@localhost mysql]# yum install libaio -y #安装mysql初始化所需的依赖库 [root@localhost mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data/ --user=mysql #初始化mysql数据库 [root@localhost mysql]# chown -R root . #为安全起见,将mysql目录中的所有文件属主更改为root用户
4、修改及配置MySQL服务脚本及配置文件
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@localhost mysql]# chkconfig --add mysqld [root@localhost mysql]# chkconfig mysqld on [root@localhost mysql]# chkconfig --list mysqld mysqld 0:off1:off2:on3:on4:on5:on6:off [root@localhost mysql]# vi my.cnf 修改如下内容 datadir = /mydata/data socket = /tmp/mysql.sock
5、启动mysql服务
[root@localhost mysql]# service mysqld start Starting MySQL... SUCCESS! [root@localhost mysql]# netstat -tnlp | grep 3306 tcp 0 0 :::3306 :::* LISTEN 1819/mysqld
6、为mysql添加PATH环境变量
[root@localhost mysql]# vi /etc/profile.d/mysqld.sh 输入以下内容: export PATH=$PATH:/usr/local/mysql/bin [root@localhost mysql]# . /etc/profile.d/mysqld.sh [root@localhost mysql]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.24 MySQL Community Server (GPL) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> quit
7、为mysql添加man帮助及输出头文件和库文件
[root@localhost mysql]# vi /etc/man.config MANPATH /usr/local/mysql/man [root@localhost mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql `/usr/include/mysql' -> `/usr/local/mysql/include/' [root@localhost mysql]# vi /etc/ld.so.conf.d/mysql.conf 添加如下内容 /usr/local/mysql/lib [root@localhost mysql]# ldconfig -v /usr/local/mysql/lib: libtcmalloc_minimal.so.0 -> libtcmalloc_minimal.so libmysqlclient.so.18 -> libmysqlclient_r.so.18.1.0 /usr/lib/mysql: libmysqlclient_r.so.16 -> libmysqlclient_r.so.16.0.0 libmysqlclient.so.16 -> libmysqlclient.so.16.0.0
至此,mysql安装完毕
四、安装PHP(以fastcgi的方式)
1、首先安装相关依赖文件
[root@bogon ~]# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.6.tar.gz [root@bogon ~]# tar xf libmcrypt-2.5.6.tar.gz [root@bogon ~]# cd libmcrypt-2.5.6 [root@bogon ~]# ./configure [root@bogon ~]# make;make install [root@bogon ~]# yum install libxml2-devel -y [root@bogon ~]# yum install bzip2-devel -y
2、解压并编译安装php-5.6.8
[root@bogon ~]# tar xf php-5.6.8.tar.bz2 [root@bogon ~]# cd php-5.6.8 [root@bogon php-5.6.8]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-fpm [root@bogon php-5.6.8]# make [root@bogon php-5.6.8]# make install
3、设置启动脚本和配置文件,并启动php-fpm
启动脚本
[root@bogon php-5.6.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@bogon php-5.6.8]# chmod +x /etc/init.d/php-fpm [root@bogon php-5.6.8]# chkconfig --add php-fpm [root@bogon php-5.6.8]# chkconfig php-fpm on
配置文件
[root@bogon php-5.6.8]# cd /usr/local/php/etc [root@bogon etc]# cp php-fpm.conf.default php-fpm.conf
启动测试
[root@bogon etc]# service php-fpm start Starting php-fpm done [root@bogon php-5.6.8]# netstat -tnlp | grep php-fpm tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 3813/php-fpm
4、配置httpd,使其能够支持php
[root@bogon etc]# cd /etc/httpd/ [root@bogon etc]# vi httpd.conf 修改如下内容: #DocumentRoot "/usr/local/apache/htdocs" LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so Include /etc/httpd/extra/httpd-vhosts.conf AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps <IfModule dir_module> DirectoryIndex index.php index.html </IfModule>
配置虚拟主机,创建虚拟主机目录
[root@bogon etc]# vi extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/www/junjie.com/ ServerName www.junjie.com ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/junjie.com/$1 <Directory "/www/junjie.com"> Options none AllowOverride none Require all granted </Directory> ErrorLog "logs/dummy-host.example.com-error_log" CustomLog "logs/dummy-host.example.com-access_log" common </VirtualHost> [root@bogon etc]# mkdir /www/junjie.com -pv [root@bogon etc] # cd /www/junjie.com/ [root@bogon junjie.com]# vi index.php 添加如下内容 <h1>www.junjie.com</h1> <?php phpinfo(); ?>
5、在浏览器中www.junjie.com进行测试(首先配置hosts文件或DNS解析)
利用phpMyAdmin工具测试httpd+php+mysql的连接
[root@bogon etc]# cd [root@bogon ~]# tar xf phpMyAdmin-4.4.4-all-languages.tar.bz2 -C /www/junjie.com/ [root@bogon ~]# cd /www/junjie.com/ [root@bogon junjie.com]# mv phpMyAdmin-4.4.4-all-languages/ pma [root@bogon xcache-3.2.0]# mysqladmin -uroot password 'redhat' #给mysql用户加密码,否则不能通过pma进行登录
在浏览器中输入www.junjie.com/pma进行测试(首先配置hosts文件或DNS解析)
6、配置xcache进行加速
[root@bogon ~]# tar xf xcache-3.2.0.tar.gz [root@bogon ~]# cd xcache-3.2.0 [root@bogon xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config [root@bogon xcache-3.2.0]# make [root@bogon xcache-3.2.0]# make install [root@bogon xcache-3.2.0]# cp xcache.ini /etc/php.d [root@bogon xcache-3.2.0]# service php-fpm restart
重新刷新www.junjie.com,即可查看到xcache的信息
大概就这些内容,后边的写的不那么相信,如果有不对的地方多做几次测试就好了。
原创文章,作者:蝙蝠侠•杰,如若转载,请注明出处:http://www.178linux.com/4020