LAMP環境搭建

系統環境及軟件版本

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

內核版本:2.6.32-431.el6.x86_64html

apr版本:apr-1.5.2mysql

apr-util版本:apr-util-1.5.4linux

pcre版本:pcre-8.39web

Apache版本:Apache/2.4.20 (Unix)sql

memcached版本:1.4.28數據庫

MySQL版本:5.6.30apache

php版本:5.6.23vim

XCache版本:3.2.0api

1、安裝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

2、安裝Apache

一、安裝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

二、安裝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

三、安裝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

四、安裝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

五、設置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

3、安裝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

4、安裝XCache爲php加速

一、安裝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可以提升三倍左右的性能

5、配置Apache與fpm方式的php(基於fastcgi模式通訊的)

至關於Apache服務器是一個反向代理服務器,這第五項是替換的第三項的安裝php,若是已經安裝須要刪除以前php及其配置文件,進行從新編譯

一、編譯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

二、配置php

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

三、配置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

四、配置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

相關文章
相關標籤/搜索