LAMP环境搭建

系统环境及软件版本

系统版本:Red Hat Enterprise Linux Server release 6.5 (Santiago)

内核版本:2.6.32-431.el6.x86_64

apr版本:apr-1.5.2

apr-util版本:apr-util-1.5.4

pcre版本:pcre-8.39

Apache版本:Apache/2.4.20 (Unix)

memcached版本:1.4.28

MySQL版本:5.6.30

php版本:5.6.23

XCache版本:3.2.0

一、安装MySQL

如果没有mysql用户则添加:

# useradd -r mysql -s /sbin/nologin

# tar zxvf mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz -C /usr/local/

# ln -sv /usr/local/mysql-5.6.30-linux-glibc2.5-x86_64 /usr/local/mysql

# chown mysql.mysql -R mysql-5.6.30-linux-glibc2.5-x86_64

创建MySQL的数据文件目录

# mkdir -pv /data/mysql

# chown -R mysql.mysql /data/mysql

初始化MySQL数据库

# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

复制MySQL的服务启动脚本到/etc/rc.d/init.d/下

# cp support-files/mysql.server /etc/rc.d/init.d/mysqld

将mysqld添加到服务聊表并设置开机自启动

# chkconfig --add mysqld

# chkconfig mysqld on

MySQL的配置文件:/etc/my.cnf或/etc/mysql/my.cnf-->$MYSQL_Base/my.cnf -->

--defaults-extra-file=/path/to/some_my.cnf -->.my.cnf

如果找到的不同,则合并,如果不同,则以最后一个为准

本人把所有配置文件都删了只留了下面这个文件

# vim /etc/my.cnf

加入如下内容:

[client]

port = 3306

socket = /tmp/mysql.sock

[mysqld]

server_id = 1

log_bin = /data/mysql/mysql_binlog

lower_case_table_names=1

binlog-ignore-db=mysql,test

port = 3306

pid-file=/usr/local/mysql/mysql.pid

socket = /tmp/mysql.sock

skip-external-locking

key_buffer_size = 256M

max_allowed_packet = 1M

table_open_cache = 256

sort_buffer_size = 1M

read_rnd_buffer_size = 4M

thread_cache_size = 8

query_cache_size = 16M

thread_concurrency = 2

datadir = /data/mysql

 

character_set_server=utf8

 

添加MySQL的到环境变量:

# vim /etc/profile.d/mysql.sh加入如下内容:

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

# source /etc/profile.d/mysql.sh

 

初始化数据库:

 

/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql --collation-server=utf8_general_ci

 

删除匿名用户:

mysql> drop user ''@'localhost';

mysql> drop user ''@'master.fansik.com';

给root用户添加密码:

mysql> update user set password=password('fanjinbao') where user='root';

mysql> flush privileges;

如果增加密码后还想直接用mysql直接登录那就:

# vim ~/.my.cnf加入:

[mysql]

user = root

password = fanjinbao

socket = /tmp/mysql.sock

如果要基于MySQL做开发:导出头文件:

# ln -sv /usr/local/mysql/include/ /usr/include/mysql

导出库文件:

# vim /etc/ld.so.conf.d/mysql.conf加入:/usr/local/mysql/lib

# ldconfig -v

查看是否添加成功:

# ldconfig -p | grep mysql

二、安装Apache

1、安装Apache可移植运行库(apr:Apache portable runtime)

# tar zxvf apr-1.5.2.tar.gz

# cd apr-1.5.2

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

# make && make install

2、安装Apache可移植运行库工具包

# tar -zxvf apr-util-1.5.4.tar.gz

# cd apr-util-1.5.4

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

# make && make install

3、安装PCRE(Perl Compatible Regular Expressions)Perl库文件

# tar zxvf pcre-8.39.tar.gz

# cd pcre-8.39

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

# make && make install

4、安装Apache服务器

   # tar zxvf httpd-2.4.20.tar.gz

   # cd httpd-2.4.20

   # ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-rewrite --enable-so --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre/ --enable-ssl --enable-deflate --enable-expires --enable-cgi --with-zlib --enable-modules=most --enable-mpms-shared=all --with-mpm=event

   # make && make install

5、设置Apache的环境变量:

# vim /etc/profile.d/httpd.sh加入

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

# . /etc/profile

启动httpd服务:apachectl start

上传httpd的启动脚本,添加执行权限,添加到服务列表,设置开机自启动。

httpd的启动脚本及设置方法:http://www.cnblogs.com/fansik/p/5644007.html

三、安装PHP

PHP原始为Personal Home Page的缩写,已经正式更名为 "PHP: Hypertext Preprocessor"。注意不是“Hypertext Preprocessor”的缩写,这种将名称放到定义中的写法被称作递归缩写

安装前准备:

# yum groupinstall -y  "Desktop Platform Development"

libmcrypt-devel是属于epel源的,如果没有要么编译,要么安装epel源

# yum -y install libmcrypt-devel bzip2-devel

安装freetype,安装libpng,安装jpeg:http://www.cnblogs.com/fansik/diary/2016/11/22/6088799.html

安装php

# tar zxvf php-5.6.23.tar.gz

# cd php-5.6.23

# ./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-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts

如果php不在本机可以使用:

./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd

最后的参数--enable-maintainer-zts是为了支持Apache的worker或event这两个mpm

# cp php.ini-production /etc/php.ini

编辑httpd的配置文件,添加对php类型文件的支持

AddType application/x-httpd-php .php

AddType application/x-httpd-php-source .phps

并且在index.html后面添加index.php

DirectoryIndex index.html index.php

重启apache服务,并在/usr/local/apache/htdocs中添加测试页

# mv index.html index.php

# vim index.php加入如下内容:

<?php

    phpinfo();

?>

访问http://10.10.10.201/进行测试即可

测试是否可以连接数据库:

<?php

    $link = mysql_connect('127.0.0.1','root','fanjinbao');

    if ($link)

        echo "successful...";

    else

        echo "Failure...";

    mysql_close();

?>

也可以用phpMyAdmin进行测试:

将phpMyAdmin-4.6.3-all-languages.tar.gz上传到/usr/local/apache/htdocs目录下

# tar zxvf phpMyAdmin-4.6.3-all-languages.tar.gz

# mv phpMyAdmin-4.6.3-all-languages pma

http://10.10.10.201/pma

四、安装XCache为php加速

1、安装xcache

# tar zxvf xcache-3.2.0.tar.gz

# cd xcache-3.2.0

# /usr/local/php/bin/phpize

# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config

# make && make install

make install完成后的最后一行出现的路径就是xcache安装位置:

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20131226/

创建php的配置文件目录

# mkdir /etc/php.d

使用XCache之前需要对缓存进行预热,使用XCache能够提高三倍左右的性能

五、配置Apache与fpm方式的php(基于fastcgi模式通信的)

相当于Apache服务器是一个反向代理服务器,这第五项是替换的第三项的安装php,如果已经安装需要删除之前php及其配置文件,进行重新编译

1、编译php

# tar zxvf php-5.6.23.tar.gz

# cd php-5.6.23

# ./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 --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2

# make && make install

2、配置php

# cp php.ini-production /etc/php.ini

3、配置php-fpm,设置开机自启动

# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm

# chmod 755 /etc/rc.d/init.d/php-fpm

# chkconfig --add php-fpm

# chkconfig php-fpm on

配置php-fpm的参数

# cd /usr/local/php/etc/

# cp php-fpm.conf.default php-fpm.conf

# vim php-fpm.conf

pid = /usr/local/php/var/run/php-fpm.pid

pm.max_children = 50

pm.start_servers = 5

pm.min_spare_servers = 3

pm.max_spare_servers = 8

4、配置Apache与php通信

将mod_proxy.so和mod_proxy_fcgi.so模块的注释去掉

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

关闭中心主机:

#DocumentRoot "/usr/local/apache/htdocs"

打开虚拟主机配置文件:

将Include /etc/httpd/extra/httpd-vhosts.conf的注释去掉

编辑httpd的配置文件,添加对php类型文件的支持

AddType application/x-httpd-php .php

AddType application/x-httpd-php-source .phps

并且在index.html后面添加index.php

DirectoryIndex index.html index.php

配置Apache的虚拟主机

# vim /etc/httpd/extra/httpd-vhosts.conf

<VirtualHost *:80>

    ServerAdmin webadmin@a.com

    DocumentRoot "/web/a"

    ServerName www.a.com

    ServerAlias a.com

    ErrorLog "logs/a-error_log"

    CustomLog "logs/a-access_log" common

    <Directory "/web/a">

        Options None

        Require all granted

    </Directory>

    ProxyRequests Off

    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/web/a/$1

</VirtualHost>

<VirtualHost *:80>

    ServerAdmin webadmin@b.com

    DocumentRoot "/web/b"

    ServerName www.b.com

    ErrorLog "logs/b-error_log"

    CustomLog "logs/b-access_log" common

    <Directory "/web/b">

        Options None

        Require all granted

    </Directory>

    ProxyRequests Off

    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/web/b/$1

</VirtualHost>

重启Apache访问www.a.com进行测试即可,需要在本机的hosts文件中添加www.a.com 10.10.10.201

posted @ 2016-07-06 11:31  fansik  阅读(355)  评论(0编辑  收藏  举报