1. 程式人生 > >第24章,LAMP 一鍵安裝腳本

第24章,LAMP 一鍵安裝腳本

php7 程序包 b-s sca exp modules cti 順序 init

更多內容請點擊:

Linux學習從入門到打死也不放棄,完全筆記整理(持續更新,求收藏,求點贊~~~~)

http://blog.51cto.com/13683480/2095439


腳本1:yum安裝 lamp+wordpress

# 自行配置好yum源

# 第一步,安裝並啟動服務
yum -y install httpd mariadb-server php php-mysql
echo -e "\e[1;31mstarting service....\e[0m"
systemctl start httpd mariadb

# 第二步,添加mysql授權賬號,新建數據庫wordpress
mysql -e "grant all on *.* to 'wpuser'@'localhost' identified by 'PASSWORD';"
mysql -e "create database wordpress;"

# 第三步,下載wordpress,並解壓
echo -e "\e[1;31mget the wordpress package....\e[0m"
wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.zip
unzip wordpress-4.9.4-zh_CN.zip
cp -a wordpress/* /var/www/html/
chown -R apache:apache /var/www/html

# 第四步,準備配置文件
cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
sed -i 's/database_name_here/wordpress/' /var/www/html/wp-config.php
sed -i 's/username_here/wpuser/' /var/www/html/wp-config.php
sed -i 's/password_here/PASSWORD/' /var/www/html/wp-config.php

# 第五步,重啟服務
echo -e "\e[1;31mrestarting httpd....\e[0m"
systemctl restart httpd
echo -e "\e[1;31mjob done....\e[0m"


腳本2,yum安裝lamp-fpm + wordpress

# 自行配置好yum源

# 第一步,安裝並啟動服務
yum -y install httpd mariadb-server php-fpm php-mysql
echo -e "\e[1;31mstarting service....\e[0m"

echo -e 'DirectoryIndex index.php' > /etc/httpd/conf.d/fcgi.conf
echo -e 'ProxyRequests Off' >> /etc/httpd/conf.d/fcgi.conf
echo -e 'ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1' >>/etc/httpd/conf.d/fcgi.conf

systemctl start httpd mariadb php-fpm

# 第二步,添加mysql授權賬號,新建數據庫wordpress
mysql -e "grant all on *.* to 'wpuser'@'localhost' identified by 'PASSWORD';"
mysql -e "create database wordpress;"

# 第三步,下載wordpress,並解壓
echo -e "\e[1;31mget the wordpress package....\e[0m"
wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.zip
unzip wordpress-4.9.4-zh_CN.zip
cp -a wordpress/* /var/www/html/
chown -R apache:apache /var/www/html

# 第四步,準備配置文件
cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
sed -i 's/database_name_here/wordpress/' /var/www/html/wp-config.php
sed -i 's/username_here/wpuser/' /var/www/html/wp-config.php
sed -i 's/password_here/PASSWORD/' /var/www/html/wp-config.php

# 第五步,重啟服務
echo -e "\e[1;31mrestarting httpd....\e[0m"
systemctl restart httpd
echo -e "\e[1;31mjob done....\e[0m"


腳本3:編譯安裝LAMP(module/fpm)+ wordpress + phpmyadmin

# 編譯安裝LAMP
#
##############################################################################################
# 安裝程序順序
# mariadb:通用二進制格式:mariadb-10.2.15
#       下載地址:

#       需提前準備好二進制安裝包
# httpd:       編譯安裝,http-2.4.33
#       下載地址:
#        http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.33.tar.bz2 
#        http://mirrors.sohu.com/apache/httpd-2.4.33.tar.bz2
#       需要更新apr,apr-util
#       下載地址:
#       APR: http://mirrors.hust.edu.cn/apache//apr/apr-1.6.3.tar.bz2
#       APR-UTIL: http://mirrors.hust.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
#
# php7:編譯安裝,php-7.2.7
#       下載地址:http://mirrors.sohu.com/php/php-7.2.7.tar.bz2
#
# phpmyadmin:安裝phpmyadmin4.8.2
#       下載地址:https://files.phpmyadmin.net/phpMyAdmin/4.8.2/phpMyAdmin-4.8.2-all-languages.zip
# worppress: 版本wordpress-4.9.4
#       下載地址:https://cn.wordpress.org/wordpress-4.9.4-zh_CN.zip
# 
# xcache:php7 以內置加速功能,不需要安裝xcache
#
#############################################################################################
# 安裝選項 
# 選擇PHP編譯方式,"module" or "fpm"

phpmod="fpm"

httpd_dir="/app/httpd24"
httpd_conf_dir="/etc/httpd24"
php_dir="/app/php"

# 安裝程序版本,和源碼壓縮包名
# 註意如果要修改,必須修改下載地址或者自行提供源碼包

mariadb_version="mariadb-10.2.15-linux-x86_64"
mariadb_package="${mariadb_version}.tar.gz"
httpd_version="httpd-2.4.33"
httpd_package="${httpd_version}.tar.bz2"
apr_version="apr-1.6.3"
apr_package="${apr_version}.tar.bz2"
apr_util_version="apr-util-1.6.1"
apr_util_package="${apr_util_version}.tar.bz2"
php_version="php-7.2.7"
php_package="${php_version}.tar.bz2"


# 選擇應用程序 "yes" or "no"

wdpress="yes"
pma="yes"

# 是否清理源碼包 "yes" or "no"

cleantemp="yes"

############################################################################################
# 準備開發環境
echo -e "\e[1;31mPrepare the development environment....\e[0m"

# 開發包組
yum groupinstall "development tools" "Server Platform Development" -y

# 編譯 httpd-2.4需要
yum install pcre-devel openssl-devel expat-devel -y

# 編譯 php 需要
yum install libxml2-devel bzip2-devel libmcrypt-devel -y

# 下載源程序包
echo -e "\e[1;31mdownload packages....\e[0m"

wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.33.tar.bz2
wget http://mirrors.hust.edu.cn/apache//apr/apr-1.6.3.tar.bz2
wget http://mirrors.hust.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
wget http://mirrors.sohu.com/php/php-7.2.7.tar.bz2

echo -e "\e[1;31munpack the packages....\e[0m"
tar -xf ${httpd_package}
tar -xf ${apr_package}
tar -xf ${apr_util_package}
tar -xf ${php_package}

############################################################################################
# 二進制安裝mariadb-10.2.15,程序包需另行下載
echo -e "\e[1;31minstall mariadb....\e[0m"

# 創建賬號
groupadd -r -g 306 mysql
useradd -r -u 306 -g 306 -d /data/mysqldb mysql

# 解壓縮
tar -xf mariadb-10.2.15-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -sv mariadb-10.2.15-linux-x86_64 mysql
cd mysql
chown -R mysql:mysql ./*

# 數據目錄和啟動腳本
mkdir /data/mysqldb -p
chown -R mysql:mysql /data/mysqldb
./scripts/mysql_install_db --datadir=/data/mysqldb --user=mysql
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on

# 配置文件
mkdir /etc/mysql
cp support-files/my-huge.cnf /etc/mysql/my.cnf
sed -i '27a \datadir =\/data\/mysqldb' /etc/mysql/my.cnf
sed -i '27a \innodb_file_per_table = on' /etc/mysql/my.cnf
sed -i '27a \skip_name_resolve = on' /etc/mysql/my.cnf

mkdir /var/log/mariadb -p
touch /var/log/mariadb/mariadb.log
chown mysql:mysql /var/log/mariadb/mariadb.log

# 添加環境變量
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh

# 啟動服務
echo -e "\e[1;31mstarting mariadb....\e[0m"
service mysqld start

####################################################################
# 源碼編譯http-2.4.33
echo -e "\e[1;31mstart install httpd....\e[0m"

groupadd -r -g 80 apache
useradd -r -u 80 -g 80 apache

# 下載並解壓
cd
cp -a ${apr_version} ${httpd_version}/srclib/apr
cp -a ${apr_util_version} ${httpd_version}/srclib/apr-util

# 編譯
cd ${httpd_version}
./configure --prefix=${httpd_dir} --sysconfdir=${httpd_conf_dir} --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-includedapr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork

make -j 4 && make install

# 添加環境變量
echo -e "PATH=${httpd_dir}/bin/:\$PATH" > /etc/profile.d/apache.sh
. /etc/profile.d/apache.sh

# 修改配置文件中的user和group,默認是daemon
sed -i '/^User/s/.*/User apache/' ${httpd_conf_dir}/httpd.conf
sed -i '/^Group/s/.*/Group apache/' ${httpd_conf_dir}/httpd.conf

# 啟動服務
echo -e "\e[1;31mstarting httpd....\e[0m"
apachectl start

#####################################################################
# 源碼編譯php
echo -e "\e[1;31mstart install php....\e[0m"
cd
cd ${php_version}/

if [ "$phpmod" = "module" ];then

######################
# 編譯php module

./configure --prefix=${php_dir} --enable-mysqlnd --with-mysqli=mysqlnd --with-openssl --with-pdo-mysql=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --with-apxs2=${httpd_dir}/bin/apxs --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --disable-fileinfo

make -j 4 && make install

# 開始編輯配置文件
echo -e "\e[1;31mModify configuration....\e[0m"

cp php.ini-production /etc/php.ini
sed -i '/IfModule mime_module/a \AddType application\/x-httpd-php .php' ${httpd_conf_dir}/httpd.conf
sed -i '/IfModule mime_module/a \AddType application\/x-httpd-php-sourcei .phps' ${httpd_conf_dir}/httpd.conf
sed -i 's/index.html/index.php index.html/' ${httpd_conf_dir}/httpd.conf


######################
elif [ "$phpmod" = "fpm" ];then

# 編譯php-fpm
./configure --prefix=${php_dir} --enable-mysqlnd  --with-mysqli=mysqlnd  --with-openssl --with-pdo-mysql=mysqlnd  --enable-mbstring  --with-freetype-dir  --with-jpeg-dir  --with-png-dir  --with-zlib  --with-libxml-dir=/usr  --enable-xml --enable-sockets  --enable-fpm  --with-config-file-path=/etc  --with-config-file-scan-dir=/etc/php.d  --disable-fileinfo 

make -j 4 && make install

# 安裝後配置
echo -e "\e[1;31mModify configuration....\e[0m"

cp php.ini-production /etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
 
cd ${php_dir}/etc
cp php-fpm.conf.default php-fpm.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf

# 修改php-fpm程序的用戶和組,可選項,不修改也可以
sed -i '/^user =/s/.*/user = apache/' php-fpm.d/www.conf
sed -i '/^group =/s/.*/group = apache/' php-fpm.d/www.conf

# 修改/etc/php.d/*.ini --> /etc/php.d/*.so
# php7.2.7 如果不修改此項,啟動會有一堆報錯
rename ini so /etc/php.d/*


service php-fpm start

# 配置httpd支持php
sed -i '/IfModule mime_module/a \AddType application\/x-httpd-php .php' ${httpd_conf_dir}/httpd.conf
sed -i '/IfModule mime_module/a \AddType application\/x-httpd-php-sourcei .phps' ${httpd_conf_dir}/httpd.conf
sed -i 's/index.html/index.php index.html/' ${httpd_conf_dir}/httpd.conf
sed -i 's/#\(LoadModule proxy_module modules\/mod_proxy.so\)/\1/' ${httpd_conf_dir}/httpd.conf
sed -i 's/#\(LoadModule proxy_fcgi_module modules\/mod_proxy_fcgi.so\)/\1/' ${httpd_conf_dir}/httpd.conf
echo 'ProxyRequests Off' >> ${httpd_conf_dir}/httpd.conf
echo -e "ProxyPassMatch  ^/(.*\.php)$ fcgi://127.0.0.1:9000${httpd_dir}/htdocs/\$1" >> ${httpd_conf_dir}/httpd.conf


fi


############################################################################################
# 安裝wordpress
cd
if [ "$wdpress" = "yes" ];then

# 獲取安裝包並解壓
echo -e "\e[1;31mstart install wordpress....\e[0m"
wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.zip
unzip wordpress-4.9.4-zh_CN.zip
cp -a wordpress/* ${httpd_dir}/htdocs/
chown -R apache:apache ${httpd_dir}/htdocs/*

# 添加數據庫授權賬號,創建數據庫
mysql -e "grant all on *.* to 'wpuser'@'localhost' identified by 'centos';"
mysql -e "create database wordpress;"

# 準備配置文件
cp ${httpd_dir}/htdocs/wp-config-sample.php ${httpd_dir}/htdocs/wp-config.php
sed -i 's/database_name_here/wordpress/' ${httpd_dir}/htdocs/wp-config.php
sed -i 's/username_here/wpuser/' ${httpd_dir}/htdocs/wp-config.php
sed -i 's/password_here/centos/' ${httpd_dir}/htdocs/wp-config.php

fi

############################################################################################
# 安裝pma
if [ "$pma" = "yes" ];then
echo -e "\e[1;31mstart install phpmyadmin....\e[0m"
yum install php-mbstring -y

# 設置root賬號密碼
#mysql -e "SET PASSWORD FOR root@'localhost'=PASSWORD('centos');"


# 獲取安裝包並解壓
wget https://files.phpmyadmin.net/phpMyAdmin/4.8.2/phpMyAdmin-4.8.2-all-languages.zip
unzip phpMyAdmin-4.8.2-all-languages.zip
cp -a phpMyAdmin-4.8.2-all-languages ${httpd_dir}/htdocs/pma
cp ${httpd_dir}/htdocs/pma/config.sample.inc.php ${httpd_dir}/htdocs/pma/config.inc.php

fi

############################################################################################
# php7.0 以上自帶加速,所以無需安裝xcache
# 清理緩存安裝文件
cd
if [ "$cleantemp" = "yes" ];then
        rm -rf ${mariadb_package} 
        rm -rf ${httpd_package}  ${httpd_version}
        rm -rf ${apr_util_package}  ${apr_util_version}
        rm -rf ${apr_package}  ${apr_version}
        rm -rf ${php_package}  ${php_version}
        rm -rf wordpress-4.9.4-zh_CN.zip  wordpress
        rm -rf phpMyAdmin-4.8.2-all-languages.zip  phpMyAdmin-4.8.2-all-languages

fi


############################################################################################
# 重啟服務
echo -e "\e[1;31mrestarting httpd....\e[0m"
apachectl restart
echo -e "\e[1;31mjob done....\e[0m"




第24章,LAMP 一鍵安裝腳本