CentOS6.5源码搭建LAMP--基于module方式实现php(单台机器)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS6.5源码搭建LAMP--基于module方式实现php(单台机器)相关的知识,希望对你有一定的参考价值。

源码搭建LAMP


软件包下载源:

搜狐镜像源:http://mirrors.sohu.com/

apache下载网:http://mirror.bit.edu.cn/apache/

pcre官网:http://www.pcre.org/

apr官网:http://apr.apache.org/

apache官网:http://httpd.apache.org/

mysql官网:https://www.mysql.com/

php官网:http://php.net/

freetds:http://download.csdn.net/download/xhu_eternalcc/7457555

官网:http://www.freetds.org/


实验环境:

CentOS6.5-x86_64 单台机器

IP 192.168.9.168

版本:

apr-1.5.2

apr-util-1.5.4

httpd-2.4.20

php-5.6.22

mysql-5.6.32


说明,因为php要连接mysql,所以安装顺序为apache,mysql,php      

注:httpd2.4版本需要较新的apr和apr-util



注:新机器要初始化:参考http://wupengfei.blog.51cto.com/7174803/1955545

注:(生产环境一般包放内网ftp下载比较快,一般前端用nginx反向代理至后端apache+php机器,mysql独立并做集群)


一、编译安装apache
1、#yum安装httpd依赖软件
[[email protected] ~]# yum install -y gcc gcc-c++ libtool libtool-ltdl-devel make gd-devel freetype-devel libxml2-devel libjpeg-devel libpng-devel openssl-devel patch  
[[email protected] ~]# yum install -y curl-devel bzip2 pcre-devel zip2-devel bzip2-devel 
[[email protected] ~]# yum install -y mcrypt-devel zlib-devel wget

2、#编译安装apr
[[email protected] ~]# cd /usr/local/src/
[[email protected] src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.5.2.tar.bz2
[[email protected] src]# tar xf apr-1.5.2.tar.bz2 
[[email protected] src]# cd apr-1.5.2
[[email protected] apr-1.5.2]# ./configure --prefix=/usr/local/apr
[[email protected] apr-1.5.2]# make && make install

3、编译安装apr-util
[[email protected] apr-1.5.2]# cd /usr/local/src/
[[email protected] src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.5.4.tar.bz2
[[email protected] src]# tar xf apr-util-1.5.4.tar.bz2
[[email protected] src]# cd apr-util-1.5.4
[[email protected] apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[[email protected] apr-util-1.5.4]# make && make install

4、编译安装httpd
[[email protected] apr-util-1.5.4]# useradd -s /sbin/nologin www
[[email protected] apr-util-1.5.4]# cd /usr/local/src/
[[email protected] src]# wget http://apache.fayea.com//httpd/httpd-2.4.20.tar.bz2 
[[email protected]  src]# tar xf httpd-2.4.20.tar.bz2 
[[email protected]  src]# cd httpd-2.4.20 
[[email protected] httpd-2.4.20]# ./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
[[email protected] httpd-2.4.20]# make
[[email protected] httpd-2.4.20]# make install
[[email protected] httpd-2.4.20]# sed -i ‘s/User daemon/User www/g‘ /usr/local/apache/conf/httpd.conf
[[email protected] httpd-2.4.20]# sed -i ‘s/Group daemon/Group www/g‘ /usr/local/apache/conf/httpd.conf
[[email protected] httpd-2.4.20]# sed -i "/ServerName/s/#ServerName.*/ServerName localhost/g" /usr/local/apache/conf/httpd.conf


6、启动方法
[[email protected] httpd-2.4.20]#/usr/local/apache/bin/apachectl -k start|reload|restart|stop

7、测试验证
[[email protected] httpd-2.4.20]# curl -I http://192.168.9.168
HTTP/1.1 200 OK
Date: Fri, 11 Aug 2017 15:22:43 GMT
Server: Apache/2.4.20 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html


二、编译安装MySQL

1、#下载安装包
[[email protected] httpd-2.4.20]# cd /usr/local/src/
[[email protected] src]# wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz

2、#建立运行mysql用户和组为系统账号  
[[email protected] src]# groupadd -r mysql
[[email protected] src]# useradd -r -g mysql -s /sbin/nologin mysql

3、#编译安装
[[email protected] src]# yum install -y libaio libaio-devel numactl numactl-devel cmake ncurses-devel libxml2-devel libtool-ltdl-devel gcc-c++ autoconf automake bison zlib-devel
[[email protected] src]# tar xf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz
[[email protected] src]# mv mysql-5.6.37-linux-glibc2.12-x86_64 /usr/local/mysql
#建议mysql使用独立设备独立目录,本实验只建立目录,独立设备请参考磁盘分区与挂载
[[email protected] src]# mkdir -p /data0/mysql
#更改属主属组
[[email protected] src]# chown -R mysql.mysql /usr/local/mysql
#初始化数据库
[[email protected] src]# cd /usr/local/mysql
[[email protected] mysql]# ./scripts/mysql_install_db --datadir=/data0/mysql --user=mysql
[[email protected] mysql]# cp ./support-files/my-default.cnf /data0/mysql/my.cnf
[[email protected] mysql]# echo "basedir = /usr/local/mysql
datadir = /data0/mysql
port = 3306
server_id = 1000
socket = /tmp/mysql.sock" >>/data0/mysql/my.cnf
[[email protected] mysql]# chown -R mysql.mysql /data0/mysql/
4,启动:
[[email protected] mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/data0/mysql/my.cnf &
[[email protected] mysql]# echo "/usr/local/mysql/bin/mysqld_safe --defaults-file=/data0/mysql/my.cnf &" >> /etc/rc.d/rc.local

6、#安全初始化
#安全验证初始化,设置root用户密码,删除匿名用户,禁止root用户远程连接,删除test库
[[email protected] mysql]# ./bin/mysql_secure_installation
#导出头文件
[[email protected] mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
#加载库文件
[[email protected] mysql]# echo "/usr/local/mysql/lib/mysql/" >>/etc/ld.so.conf
[[email protected] mysql]# ldconfig
[[email protected] mysql]# echo "PATH=$PATH:/usr/local/mysql/bin/" >> /etc/profile


三、编译安装PHP

1、#编译安装libmcrypt扩展
[[email protected] src]# cd /usr/local/src/
[[email protected] src]# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
[[email protected] src]# tar xf libmcrypt-2.5.7.tar.gz 
[[email protected] src]# cd libmcrypt-2.5.7
[[email protected] libmcrypt-2.5.7]# ./configure
[[email protected] libmcrypt-2.5.7]# make && make install

2、#编译安装freetds扩展
[[email protected] local]# cd /usr/local/src/
[[email protected] src]# wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
[[email protected] src]# tar xf freetds-patched.tar.gz 
[[email protected] src]# cd freetds-1.00.53/
[[email protected] freetds-1.00.53]# ./configure --prefix=/usr/local/freetds --with-tdsver=7.4 --enable-msdblib 
[[email protected] freetds-1.00.53]# make && make install
[[email protected] freetds-1.00.53]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/freetds/lib/

4、#下载安装包
[[email protected] src]# cd /usr/local/src/
[[email protected] src]# wget http://cn2.php.net/distributions/php-5.6.22.tar.bz2
[[email protected] src]# tar xf php-5.6.22.tar.bz2 
[[email protected] src]# cd php-5.6.22
[[email protected] php-5.6.22]#  ./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-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-mssql=/usr/local/freetds --with-bz2 --enable-maintainer-zts --with-curl --enable-ftp --enable-bcmath --with-gd --with-freetype-dir --enable-gd-native-ttf --with-iconv-dir --enable-mbstring --enable-calendar --with-gettext --enable-dom --enable-fpm
#说明:如果php5.3以上的版本,为了链接数据库,可以指定mysqlnd,这样本机就可以不用安装mysql和mysql开发包。5.4已经是默认设置。
如: --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
[[email protected] php-5.6.22]# make
[[email protected] php-5.6.22]# make install

#为php提供配置文件
[[email protected] php-5.6.22]# cp php.ini-development /usr/local/php/etc/php.ini

5、编辑apache配置文件httpd.conf,apache支持php
[[email protected] php-5.6.22]# sed -i ‘/LoadModule php5_module/ aAddType application/x-httpd-php-source .phps‘ /usr/local/apache/conf/httpd.conf
[[email protected] php-5.6.22]# sed -i ‘/LoadModule php5_module/ aAddType application/x-httpd-php .php .php5‘ /usr/local/apache/conf/httpd.conf
[[email protected] php-5.6.22]# sed -i ‘/DirectoryIndex/ s/index.html/index.php index.html/‘ /usr/local/apache/conf/httpd.conf

#重启httpd
[[email protected] php-5.6.22]# /usr/local/apache/bin/apachectl -k restart
4、测试验证php
 "<?php
        phpinfo();
?>" > /usr/local/apache/htdocs/index.php
5、浏览器访问:
http://192.168.9.168
5、测试验证php连接数据库
[[email protected] php-5.6.22]# vim index.php 

<?php
        $link = mysql_connect(‘127.0.0.1‘,‘root‘,‘123456‘);
        if ($link)
             echo "Success...";
        else
             echo "Failure...";
        mysql_close()
?>
#浏览器访问
http://192.168.9.168

6、#ab压力测试
[[email protected] php-5.6.22]# ab -c 100 -n 10000 

四、测试Xcache为php加速
1、#编译安装
[[email protected] htdocs]# cd /usr/local/src/
[[email protected] src]# tar xf xcache-3.2.0.tar.bz2 
[[email protected] src]# cd xcache-3.2.0
[[email protected] xcache-3.2.0]# /usr/local/php/bin/phpize
[[email protected] xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config 
[[email protected] xcache-3.2.0]# make && make install
2、#编辑php.ini,整合php和xcache
[[email protected] xcache-3.2.0]# mkdir -p /etc/php.d
[[email protected] xcache-3.2.0]# cp xcache.ini /etc/php.d


#重启apache
[[email protected] xcache-3.2.0]# apachectl -k restart
#压力测试,看效果



以上是关于CentOS6.5源码搭建LAMP--基于module方式实现php(单台机器)的主要内容,如果未能解决你的问题,请参考以下文章

CentOS6.5源码安装zabbix4.0

Centos6.5 搭建LAMP环境

Centos6.5搭建LAMP

Centos6.5 下搭建LAMP环境

centos6.5 系统-搭建lamp(php7)环境--(yum在线安装)

4-源码方式基于LAMP架构搭建BBS论坛或者博客