雲平臺快速部署與源碼編譯lamp應用wordpress

前言

這篇博客我決定寫的精簡一點,不寫過多的廢話。
所有步驟均在雲平臺上完成與成功。
lamp指的是
L: linux
A: apache (httpd)
M: mysql, mariadb
M:memcached
P: php, perl, python
我這裏用到的是linux+apache+mariadb+php
wordpress是使用PHP語言開發的博客平臺,用戶可以在支持PHP和MySQL數據庫的服務器上架設屬於自己的網站。也可以把 WordPress當作一個內容管理系統(CMS)來使用。
首先介紹如何用在CentOS7上快速的部署lamp加wordpress。
後面再介紹關於如何在centos6上面源碼編譯搭建最新的lamp-wordpress.
最後我會放上在CentOS6上一鍵部署lamp的腳本。

快速部署lamp應用

安裝各種包

[root@localhost ~]# yum install httpd php php-mysql 
[root@localhost ~]# yum install mariadb-server
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl start mariadb

創建數據庫及用戶

不過首先要做安全措施
運行mysql初始安全腳本

[root@localhost ~]# mysql_secure_installation 

按要求設定密碼還有其他的一些選項等。
設定完成可以登錄mysql了,我這裏不允許匿名賬戶就用的root登陸的。

[root@localhost ~]# mysql -uroot -p
Enter password: 

輸入你的root密碼,登陸數據庫。
(如果是在雲平臺上面的虛擬機,不是在本地的虛擬機,注意密碼一定要複雜,我的IP一天被別人登錄失敗3W多次…)
首先創建一個數據庫

MariaDB [(none)]> create database wpdb;

然後我這裏直接創建用戶加授權一起做了。
爲了安全,一定要鎖定你這臺服務器的IP,不允許從其他登錄。

mysql> grant all  on wpdb.* to wpuser@'123.123.123.123' identified by "wppass";
mysql> grant all  on wpdb.* to wpuser@'127.0.0.1' identified by "wppass";
mysql> grant all  on wpdb.* to wpuser@'localhost' identified by "wppass";

測試

測試apache是否正常,
測試php和mariadb連接是否正常。
首先在/val/www/html/目錄下創建index.php文件
寫入代碼如下:

[root@localhost /var/www/html]# vim index.php
<html><body><h1> LAMP</h1></body></html>
<?php
$mysqli=new mysqli("localhost","root","centos");
if(mysqli_connect_errno()){
echo "連接數據庫失敗!";
$mysqli=null;
exit;
}
echo "連接數據庫成功!";
$mysqli->close();
phpinfo();
?>

然後修改apache的配置文件

[root@localhost /var/www/html]# vim /etc/httpd/conf/httpd.conf 
DirectoryIndex index.php index.html index.html.var

搜索DirectoryIndex然後在後下面寫下index.php,使其優先識別.php後綴文件。
重新加載apache的配置文件

[root@localhost /var/www/html]# systemctl reload httpd

如果一切成功,那麼會出現下面這張圖片的樣子。
這裏寫圖片描述
那麼接下來我們就要部署應用wordpress了

部署wordpress

我這裏用的是從網上下載的最新的wordpress-4.8.1-zh_CN.tar.gz

[root@localhost ~/src]# tar xvf wordpress-4.8-zh_CN.tar.gz -C /var/www/html/
[root@localhost ~/src]# cd /var/www/html
[root@localhost /var/www/html]# wordpress/ blog/
[root@localhost /var/www/html]#cd blog
[root@localhost /var/www/html/blog]# cp wp-config-sample.php  wp-config.php
[root@localhost /var/www/html/blog]# vim wp-config.php
修改這幾樣:
    // ** MySQL 設置 - 具體信息來自您正在使用的主機 ** //
    /** WordPress數據庫的名稱 */
    define('DB_NAME', 'wpdb');

    /** MySQL數據庫用戶名 */
    define('DB_USER', 'wpuser');

    /** MySQL數據庫密碼 */
    define('DB_PASSWORD', '你之前設置的密碼');

登錄測試就好了,
在瀏覽器輸出http://IP/blog

這是最簡單的,最迅速(十分鐘之內即可完成)也是最穩定的部署lamp應用wordpress的方法,缺點是,性能不太好。可以使用xcache,epel源裏面就有,直接安裝即可。

在CentOS6上源碼編譯PHP-FPM模式的LAMP

軟件版本:
apr-1.6.2.tar.gz    httpd-2.4.27.tar.bz2    php-5.6.31.tar.xz             
xcache-3.2.0.tar.bz2    apr-util-1.6.0.tar.gz  mariadb-5.5.57-linux-x86_64.tar.gz
wordpress-4.8.1-zh_CN.tar.gz
(爲了使用xcache加速PHP沒有使用最新的版本,再更新的版本,xcache不支持)

編譯httpd2.4

首先安裝開發包組

[root@localhost ~]# yum groupinstall "development tools" -y

我這裏把接下來編譯過程中缺少的包都放在這裏,可以提前安裝好,也可以按報錯再裝。

[root@localhost ~]# yum install openssl-devel pcre-devel expat-devel
[root@localhost ~]# tar xvf apr-1.6.2.tar.gz 
[root@localhost ~]# tar xvf apr-util-1.6.0.tar.gz 
[root@localhost ~]# tar xvf httpd-2.4.27.tar.bz2 

把apr和apr-util都放在httpd下只用編譯一次就好了。

[root@localhost ~]# cp -r apr-1.6.2 httpd-2.4.27/srclib/apr
[root@localhost ~]# cp -r apr-util-1.6.0 httpd-2.4.27/srclib/apr-util

準備編譯

[root@localhost ~]# cd httpd-2.4.27/
[root@localhost ~/src/httpd-2.4.27]# . /configure --prefix=/app/httpd24 \
--enable-so --enable-ssl \
--enable-rewrite --with-zlib \
--with-pcre --with-included-apr \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork

如果沒什麼問題就可以開始編譯安裝了

[root@localhost ~/src/httpd-2.4.27]# make -j 4 && make install

加入PATH變量。

[root@localhost ~/src/httpd-2.4.27] # /etc/profile.d/lamp.sh
PATH=/app/httpd24/bin/:$PATH
保存後
[root@localhost ~/src/httpd-2.4.27] # . /etc/profile.d/lamp.sh
重載lamp.sh文件

編輯服務腳本,也可以複製過來後再修改。我這裏就放上這個服務腳本

#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#          server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server 
#  implementing the current HTTP standards.
### END INIT INFO

# Source function library.
. /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 may 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=/app/httpd24/bin/apachectl
httpd=${HTTPD-/app/httpd24/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/app/httpd24/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd24}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
    status -p ${pidfile} $httpd > /dev/null
    if [[ $? = 0 ]]; then
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
    else
        echo -n $"Stopping $prog: "
        success
    fi
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"httpd shutdown"
        fi
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
        status -p ${pidfile} $httpd
    RETVAL=$?
    ;;
  restart)
    stop
    start
    ;;
  condrestart|try-restart)
    if status -p ${pidfile} $httpd >&/dev/null; then
        stop
        start
    fi
    ;;
  force-reload|reload)
        reload
    ;;
  graceful|help|configtest|fullstatus)
    $apachectl $@
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
    RETVAL=2
esac

exit $RETVAL

可以直接複製上面這個腳本,作爲服務較本。

接下來我們要把httpd24加入開價啓動,並啓動httpd24

[root@localhost ~]# chkconfig --add httpd24
[root@localhost ~]# chkconfig --list httpd24
[root@localhost ~]# service httpd24 start

二進制安裝mariadb

解壓縮
並創建軟連接
創建mysql用戶
運行腳本安裝制定路徑

[root@localhost ~src/]# tar xvf mariadb-5.5.57-linux-x86_64.tar.gz  -C /usr/local/ 
[root@localhost ~src/]# cd /usr/local/
[root@localhost /usr/local]# ln -s mariadb-5.5.57-linux-x86_64/ mysql
[root@localhost /usr/local]# useradd -r -m -d /app/mysqldb -s /sbin/nologin mysql 
[root@localhost /usr/local]# cd mysql/
[root@localhost /usr/local/mysql]# scripts/mysql_install_db --datadir=/app/mysqldb --user=mysql

在/etc/下創建目錄mysql
並複製配置文件到/etc/mysql/目錄下

[root@localhost /usr/local/mysql]# mkdir /etc/mysql
[root@localhost /usr/local/mysql]# cp support-files/my-large.cnf   /etc/mysql/my.cnf

修改配置文件

[root@localhost /usr/local/mysql]#  vim /etc/mysql/my.cnf
[mysqld]後面加上三行
datadir = /app/mysqldb
innodb_file_per_table = ON
skip_name_resolve = ON

複製服務腳本
加入開機啓動項
創建日誌文件,給與權限。
啓動

[root@localhost /usr/local/mysql]#  cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost /usr/local/mysql]#  chkconfig --add mysqld
[root@localhost /usr/local/mysql]#  chkconfig --list 
[root@localhost /usr/local/mysql]#  
touch /var/log/mysqld.log
[root@localhost /usr/local/mysql]#  chown mysql /var/log/mysqld.log
[root@localhost /usr/local/mysql]#  service mysqld start

加入PATH路徑

[root@localhost /usr/local/mysql]#  vim /etc/profile.d/lamp.sh 
PATH=/app/httpd24/bin/:$PATH
PATH=/usr/local/mysql/bin/:$PATH
保存退出重載lamp.sh
. /etc/profile.d/lamp.sh

執行mysql安全腳本
root登錄數據庫
創建數據庫和用戶

[root@localhost ~]# mysql_secure_installation
[root@localhost ~]# mysql -uroot -pcentos
MariaDB [(none)]> grant all on wpdb.* to wpuser@'192.168.25.%' identified by 'centos';
MariaDB [(none)]> grant all on wpdb.* to wpuser@'127.%' identified by 'centos';
MariaDB [(none)]> grant all on wpdb.* to wpuser@'localhost' identified by 'centos';

源碼編譯PHP

編譯時會以來的包我都提前列出來,安裝
解壓
編譯前準備
編譯

[root@localhost ~/src]# yum install libxml2-devel bzip2-devel libmcrypt-devel
[root@localhost ~/src]# tar xvf php-5.6.31.tar.xz 
[root@localhost ~/src]# cd php-5.6.31
[root@localhost ~/src/php-5.6.31]# . /configure --prefix=/app/php \
--with-mysql=/usr/local/mysql \
--with-openssl \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--enable-mbstring \
--with-png-dir \
--with-jpeg-dir \
--with-freetype-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--enable-sockets \
--with-apxs2=/app/httpd24/bin/apxs \
--with-mcrypt \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--with-bz2
[root@localhost ~/src/php-5.6.31]# make -j 4 && make install

加入PATH路徑

[root@localhost ~/src/php-5.6.31]#  vim /etc/profile.d/lamp.sh 
PATH=/app/php/bin:$PATH
PATH=/app/httpd24/bin/:$PATH
PATH=/usr/local/mysql/bin/:$PATH
保存退出
重載PATH路徑
[root@localhost ~/src/php-5.6.31]#  .  /etc/profile.d/lamp.sh 

複製文件做配置文件

[root@localhost ~/src/php-5.6.31]# cp php.ini-production /etc/php.ini
修改httpd24的配置文件
[root@localhost ~/src/php-5.6.31]# vim /app/apache24/conf/httpd.conf
啓動httpd的相關模塊
去掉下面兩行註釋
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
 添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
定位至DirectoryIndex index.html
修改爲:
DirectoryIndex index.php index.htm
加下面兩行
ProxyRequests Off 關閉正向代理
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/httpd24/htdocs/$1
重啓服務
[root@localhost ~/src/php-5.6.31]# service httpd24 restart

測試

[root@localhost ~]# vim /app/httpd24/htdocs/index.php
<html><body><h1>It works!</h1></body></html>
<?php
$mysqli=new mysqli("localhost","root","centos");
if(mysqli_connect_errno()){
echo "連接數據庫失敗!";
$mysqli=null;
exit;
}
echo "連接數據庫成功!";
$mysqli->close();
phpinfo();
?>

配置wordpress

解壓 wordpress

[root@localhost ~src]# tar xvf wordpress-4.8.1-zh_CN.tar.gz  -C /app/httpd24/htdocs
[root@localhost ~src]# cd /app/httpd24/htdocs
[root@localhost ~/app/httd24/htdocs]# mv wordpress/ blog/
[root@localhost ~/app/httd24/htdocs]# cd /app/httpd24/htdocs/blog/
[root@localhost ~/app/httd24/htdocs/blog]# cp wp-config-sample.php  wp-config.php
[root@localhost ~/app/httd24/htdocs/blog]# vim wp-config.php
修改下面的登錄信息

    /** MySQL數據庫用戶名 */
    define('DB_USER', 'wpuser');

    /** MySQL數據庫密碼 */
    define('DB_PASSWORD', 'centos');

    /** MySQL主機 */
    define('DB_HOST', 'localhost');

登錄

在瀏覽器上登錄http://websrv/blog
按順序填信息

編譯xcache實現php加速

[root@localhost ~src]# tar xvf xcache-3.2.0.tar.bz2 
[root@localhost ~src]# cd xcache-3.2.0
[root@localhost ~src/xcache-3.2.0]# phpize 
這裏要安裝php-devel.x86_64包
開始編譯
[root@localhost ~src/xcache-3.2.0]# ./configure  --enable-xcache --with-php-config=/app/php5/bin/php-config 
make && make install
創建文件夾
複製配置文件
[root@localhost ~src/xcache-3.2.0]# mkdir /etc/php.d/
[root@localhost ~src/xcache-3.2.0]# cp xcache.ini  /etc/php5.d/
修改配置文件
[root@localhost ~src/xcache-3.2.0]# vim /etc/php5.d/xcache.ini 
extension = /app/php5/lib/php/extensions/no-debug-non-zts-20131226/xcache.so
最後
重啓php-fpm
[root@localhost ~src/xcache-3.2.0]# service php-fpm restart

到這裏,編譯安裝lamp應用wordpress就成功了,剩下的就是自己起自定義自己的博客了。
這裏放上我剛剛搭建的博客的地址
http://118.89.166.61
我之前使用了,源碼編譯安裝部署博客,後來,我又換成了yum安裝的,雖然版本老舊,
但是穩定。
下面我放上一段朋友寫的在CentOS6上一鍵部署LAMP編譯安裝的腳本。

一鍵安裝腳本

創建一個文件夾,步入就叫lamp

apr-1.5.2.tar.bz2
apr-util-1.5.4.tar.bz2
httpd-2.4.25.tar.bz2
mariadb-5.5.43-linux-x86_64.tar.gz
php-5.6.31.tar.bz2
這些壓縮包都放進去
然後在lamp目錄下創建一個服務較本httpd24
代碼如下:

[root@localhost ~/lamp]# cat index.php 
<?php
    $conn=mysql_connect('127.0.0.1','root','');
    if ($conn)
        echo "success";
    else
        echo "fail";

    phpinfo();
?>
[root@localhost ~/lamp]# cat httpd24 
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#          server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server 
#  implementing the current HTTP standards.
### END INIT INFO

# Source function library.
. /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 may 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-/usr/local/apache/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
    status -p ${pidfile} $httpd > /dev/null
    if [[ $? = 0 ]]; then
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
    else
        echo -n $"Stopping $prog: "
        success
    fi
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"httpd shutdown"
        fi
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
        status -p ${pidfile} $httpd
    RETVAL=$?
    ;;
  restart)
    stop
    start
    ;;
  condrestart|try-restart)
    if status -p ${pidfile} $httpd >&/dev/null; then
        stop
        start
    fi
    ;;
  force-reload|reload)
        reload
    ;;
  graceful|help|configtest|fullstatus)
    $apachectl $@
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
    RETVAL=2
esac

exit $RETVAL
[root@localhost ~/lamp]# chmod +X httpd24

繼續在lamp目錄下創建一個index.php文件
[root@localhost ~/lamp]# cat index.php
代碼如下:

<?php
    $conn=mysql_connect('127.0.0.1','root','');
    if ($conn)
        echo "success";
    else
        echo "fail";

    phpinfo();
?>

重點來了,在lamp目錄下創建lamp.sh腳本
代碼如下:

[root@localhost ~/lamp]# cat lamp.sh 
#!/bin/bash
software=("apr-1.5.2.tar.bz2" "apr-util-1.5.4.tar.bz2" "httpd-2.4.25.tar.bz2" "php-5.6.31.tar.bz2" "mariadb-5.5.43-linux-x86_64.tar.gz")
soft_dir=("apr-1.5.2" "apr-util-1.5.4" "httpd-2.4.25" "php-5.6.31" "mariadb-5.5.43-linux-x86_64")
apr=apr-1.5.2
apr_util=apr-util-1.5.4
httpd=httpd-2.4.25
php=php-5.6.31
mysql=mariadb-5.5.43-linux-x86_64
dir=`pwd`
# Pre-installation script
arr_software=("pcre-devel" "openssl-devel" "bzip2-devel" "libmcrypt-devel" "libxml2-devel" "Development Tools")
yum_install_depend_package() {
    for i in $(seq 0 $[${#arr_software[*]}-1]); do
        if [[ ${arr_software[$i]} == "Development Tools" ]]; then
            echo -e "\033[31mInsatll ${arr_software[$i]}, please wait...\033[0m"
            yum groupinstall -y ${arr_software[$i]} &> /dev/null 
        else
            echo -e "\033[31mInsatll ${arr_software[$i]}, please wait...\033[0m"
            rpm -q ${arr_software[$i]} &> /dev/null || yum install -y ${arr_software[$i]} &> /dev/null
        fi
    done
}
#yum install -y wget &> /dev/null
#yum install -y pcre-devel &> /dev/null
#yum install -y openssl-devel &> /dev/null 
#yum groupinstall -y "Development Tools" &> /dev/null
#yum install -y  bzip2-devel &> /dev/null
#yum install -y libmcrypt-devel &> /dev/null
#yum install -y libxml2-devel &> /dev/null
# installation httpd
tar_software() {
    for i in $(seq 0 $[${#software[*]}-1]); do
        if [ ! -d ${soft_dir[$i]} ]; then
            tar xf ${software[$i]} &> /dev/null
        fi
    done
}
install_apr() {
    echo -e "\033[33mCompile $apr, please wait...\033[0m"
    cd $apr 
    ./configure --prefix=/usr/local/apr/ &> /dev/null || { echo "Compile $apr fail." && exit 1; }
    make &> /dev/null
    make install &> /dev/null
    cd $dir
}
install_apr_util() {
    echo -e "\033[33mCompile $apr_util, please wait...\033[0m"
    cd $apr_util 
    ./configure --prefix=/usr/local/apr-util/ --with-apr=/usr/local/apr &> /dev/null || { echo "compile $apr_util fail." && exit 1; }
    make &> /dev/null 
    make install &> /dev/null
    cd $dir
}
install_httpd() {
    echo -e "\033[33mCompile $httpd, please wait...\033[0m"
    id apache &> /dev/null || { groupadd -r apache && useradd -g apache -r apache; }
    cd $httpd 
    ./configure --prefix=/usr/local/apache --sysconf=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork &> /dev/null || { echo -e "\033[32mcompile $httpd fail.\033[0m" && exit 1; }
    make &> /dev/null
    make install &> /dev/null
    cd $dir
    cp -f ./httpd24 /etc/init.d/httpd24
    chkconfig --add httpd24 &> /dev/null
    chkconfig httpd24 on &> /dev/null
}
# Install mariadb
install_mysqld() {
    echo -e "\033[33mInstall $mysql, please wait...\033[0m"
    cd $dir
    mv $mysql /usr/local/$mysql
    cd /usr/local
    ln -sv $mysql mysql &> /dev/null
    id mysql &> /dev/null || { groupadd -r mysql && useradd -g mysql -r mysql; } 
    mkdir -p /data/mydata
    chown -R mysql:mysql /data/mydata
    cd mysql
    ./scripts/mysql_install_db --user=mysql --datadir=/data/mydata/ &> /dev/null
    if [ ! -f /etc/my.cnf ]; then
        cp ./support-files/my-large.cnf /etc/my.cnf
        grep "datadir" /etc/mysql/my.cnf &> /dev/null || sed -i '/\[mysqld\]/a \datadir = /data/mydata\ninnodb_file_per_table = on\nskip_name_resolve = on' /etc/my.cnf
    else
        [ -d /etc/mysql ] &> /dev/null || mkdir /etc/mysql
        cp -f ./support-files/my-large.cnf /etc/mysql/my.cnf
        grep "datadir" /etc/mysql/my.cnf &> /dev/null || sed -i '/\[mysqld\]/a \datadir = /data/mydata\ninnodb_file_per_table = on\nskip_name_resolve = on' /etc/mysql/my.cnf
    fi
    cp -f ./support-files/mysql.server /etc/rc.d/init.d/mysqld
    [ -f /var/log/mysqld.log ] &> /dev/null || { touch /var/log/mysqld.log && chown mysql:mysql /var/log/mysqld.log; }
    chkconfig --add mysqld
    chkconfig mysqld on
}
# Install php
install_php() {
    echo -e "\033[33mCompile $php, please wait...\033[0m"
    cd $dir/$php
    ./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 &> /dev/null
    echo -e "\033[33mInstall $php, please wait...\033[0m"
    make &> /dev/null
    make install &> /dev/null
    cp -f ./php.ini-production /etc/php.ini 
    sed -i '/<IfModule mime_module>/a \    AddType application/x-httpd-php .php\n    AddType application/x-httpd-php-source .phps' /etc/httpd24/httpd.conf
    sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/g' /etc/httpd24/httpd.conf
    cd $dir
    [ -f /usr/local/apache/htdocs/index.html ] && rm -f /usr/local/apache/htdocs/index.html && cp -f ./index.php /usr/local/apache/htdocs/index.php
    service httpd24 start &> /dev/null
    service mysqld start &> /dev/null
}
main() {
    yum_install_depend_package
    tar_software
    install_apr
    install_apr_util
    install_httpd
    install_mysqld
    install_php
}
main

在給lamp.sh加上執行權限

[root@localhost ~/lamp]# chmod +x lamp.sh 

一鍵安裝的時候使用命令bash lamp.sh,就可以開始自動編譯安裝,顯示效果如下。

[root@localhost ~/lamp]# bash lamp.sh
Insatll pcre-devel, please wait...
Insatll openssl-devel, please wait...
Insatll bzip2-devel, please wait...
Insatll libmcrypt-devel, please wait...
Insatll libxml2-devel, please wait...
Insatll Development Tools, please wait...
Compile apr-1.5.2, please wait...
Compile apr-util-1.5.4, please wait...
Compile httpd-2.4.25, please wait...
Install mariadb-5.5.43-linux-x86_64, please wait...
Compile php-5.6.31, please wait...
Install php-5.6.31, please wait...

最後

lamp是一門基礎技術,每一個運維到需要掌握,
一定要擁有獨立部署環境的能力。
只有部署好了環境,才能在上面跑應用。
寫到最後都感覺自己都點精神衰弱了…






順便推薦一下個人博客地址

www.seeil.life (沒備案,域名已掛)
www.seeit.cc(沒備案,域名已掛)
http://118.89.166.61(博客地址)
博客會和這邊同步跟新的。

ღ ღ ღ 如果覺得文章對您有用,不妨贊一下ღ ღ ღ

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章