此指令碼以在任何一臺有網路的伺服器上自動配置LNMP
使用方法:將inst_lnmp程式碼儲存一個檔案,例如inst_lnmp.sh 上傳到伺服器上執行。
此指令碼在CentOS 6.0 X864環境下通過。
參考文章 http://os.51cto.com/art/201110/295713.htm
- #!/bin/bash
- #set -x
- #date: 2013-01-06
- #Description: 一鍵安裝LNMP環境
- #Version: 0.1
- #Author: Wolf
- #定義命令搜尋路徑
- PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
- export PATH
- src_dir=/usr/src
- nginx_dir=/usr/local/nginx
- mysql_dir=/usr/local/mysql
- php_dir=/usr/local/php
- libmcrypt_dir=/usr/local/libmcypt
- #關閉SELiunx
- echo "Disabled SELinux"
- if [ -s /etc/selinux/config ]
- then
- sed -i `s/SELINUX=enforcing/SELINUX=disabled/g` /etc/selinux/config
- fi
- /usr/sbin/setenforce 0
- # Check if user is root
- if [ "$UID" -ne 0 ]
- then
- printf "Error: You must be root to run this script! "
- exit 1
- fi
- useradd -s /sbin/nologin mysql
- #檢查需要的軟體包是否存在,如果沒有就下載。
- download_src(){
- cd $src_dir
- if [ -s libiconv-1.13.1.tar.gz ]
- then
- echo "libiconv-1.13.1.tar.gz found"
- else
- echo "libiconv-1.13.1.tar.gz not found. download new...."
- wget http://mozbuildtools.googlecode.com/files/libiconv-1.13.1.tar.gz
- fi
- if [ -s mhash-0.9.9.9.tar.gz ]
- then
- echo "mhash-0.9.9.9.tar.gz found"
- else
- echo "mhash-0.9.9.9.tar.gz not found. download new....."
- wget http://acelnmp.googlecode.com/files/mhash-0.9.9.9.tar.gz
- fi
- if [ -s mysql-5.5.25a.tar.gz ]
- then
- echo "mysql-5.5.25a.tar.gz found"
- else
- echo "mysql-5.5.25a.tar.gz not found. download new....."
- wget http://mysql.ntu.edu.tw/Downloads/MySQL-5.5/mysql-5.5.25a.tar.gz
- fi
- if [ -s php-5.3.13.tar.gz ]
- then
- echo "php-5.3.13.tar.gz found"
- else
- echo "php-5.3.13.tar.gz not found. download new....."
- wget http://cn2.php.net/distributions/php-5.3.13.tar.gz
- fi
- if [ -s google-perftools-1.6.tar.gz ]
- then
- echo "google-perftools-1.6.tar.gz found"
- else
- echo "google-perftools-1.6.tar.gz not found. download new....."
- wget http://gperftools.googlecode.com/files/google-perftools-1.6.tar.gz
- fi
- if [ -s cmake-2.8.4.tar.gz ]
- then
- echo "cmake-2.8.4.tar.gz found......"
- else
- echo "cmake-2.8.4.tar.gz not found. download new....."
- wget http://www.cmake.org/files/v2.8/cmake-2.8.4.tar.gz
- fi
- if [ -s nginx-1.3.10.tar.gz ]
- then
- echo "nginx-1.3.10.tar.gz found."
- else
- echo "nginx-1.3.10.tar.gz not found. download new...."
- wget http://www.nginx.org/download/nginx-1.3.10.tar.gz
- fi
- if [ -s libmcrypt-2.5.7.tar.gz ]
- then
- echo "libmcrypt-2.5.7.tar.gz found"
- else
- echo "libmcrypt-2.5.7.tar.gz not found. download new..."
- wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/Production/libmcrypt-2.5.7.tar.gz
- fi
- if [ -s memcache-2.2.5.tgz ]
- then
- echo "memcache-2.2.5.tgz found"
- else
- echo "memcache-2.2.5.tgz not found. download new......"
- wget http://vps.googlecode.com/files/memcache-2.2.5.tgz
- fi
- if [ -s eaccelerator-0.9.5.3.tar.bz2 ]
- then
- echo "eaccelerator-0.9.5.3.tar.bz2 found"
- else
- echo "eaccelerator-0.9.5.3.tar.bz2 not found. download new.."
- wget http://autosetup1.googlecode.com/files/eaccelerator-0.9.5.3.tar.bz2
- fi
- if [ -s ImageMagick.tar.gz ]
- then
- echo "ImageMagick.tar.gz found"
- else
- echo "ImageMagick.tar.gz not found. download nrw."
- wget http://www.p_w_picpathmagick.org/download/ImageMagick.tar.gz
- fi
- if [ -s imagick-2.3.0.tgz ]
- then
- echo "imagick-2.3.0.tgz found"
- else
- echo "imagick-2.3.0.tgz not found. download new"
- wget http://lnmpp.googlecode.com/files/imagick-2.3.0.tgz
- fi
- }
- #初始化伺服器
- init_server(){
- for src in dialog ntp vim-enhanced vixie-cron gcc gcc-c++ gcc-g77 flex bison autoconf automake glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel libtool* zlib-devel libxml2-devel libjpeg-devel libpng-devel libtiff-devel fontconfig-devel freetype-devel libXpm-devel gettext-devel curl curl-devel pam-devel e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel net-snmp net-snmp-devel nss_ldap openldap-clients openldap-servers libtidy libtidy-devel wget libc-client libc-client-devel pcre pcre-devel
- do
- yum -y install $src
- done
- ln -s /usr/lib64/libc-client.so* /usr/lib/
- ln -s /usr/lib64/libldap* /usr/lib
- }
- #先檢測mysql是否已經安裝,如果已經安裝了就不再安裝,如果沒安裝就安裝mysql
- inst_mysql(){
- tar zxvf cmake-2.8.4.tar.gz
- cd $src_dir/cmake-2.8.4
- ./configure && make && make install
- if ( -d $mysql_dir )
- then
- echo "MySQL is installed"
- else
- echo "MySQL not install. install run..."
- cd $src_dir
- tar zxvf mysql-5.5.25a.tar.gz
- cd mysql-5.5.25a
- cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_SSL=yes -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=on
- gmake && make install
- fi
- if ( -f /ect/my.cnf )
- then
- echo "MySQL config file my.cnf found. backup my.cnf to my.cnf.bak"
- mv /etc/my.cnf /etc/my.cnf.bak
- /bin/cp support-files/my-small.cnf /etc/my.cnf
- /bin/cp support-files/mysql.server /etc/rc.d/init.d/mysqld
- chmod 755 /etc/rc.d/init.d/mysqld
- fi
- /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=root --defaults-file=/etc/my.cnf
- }
- inst_libmcypt(){
- if ( -d $libmcrypt_dir )
- then
- echo "libmcrypt is installed."
- else
- cd $src_dir
- tar zxvf libmcrypt-2.5.7.tar.gz
- cd libmcrypt-2.5.7
- ./configure --prefix=/usr/local/libmcypt && make && make install
- /sbin/ldconfig
- cd libltdl/
- ./configure --enable-ltdl-install
- make
- make install
- fi
- cd $src_dir
- tar zxf mhash-0.9.9.9.tar.gz
- cd mhash-0.9.9.9/
- ./configure
- make
- make install
- ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
- ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
- ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
- ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
- ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
- }
- #先檢測PHP是否已經編譯安裝,如果安裝了就不再進行安裝,如果沒安裝就安裝PHP
- inst_php(){
- cd $src_dir
- tar zxf libiconv-1.13.1.tar.gz
- cd libiconv-1.13.1/
- ./configure --prefix=/usr/local
- make
- make install
- cd $src_dir
- if ( -d $php_dir)
- then
- echo "PHP is installed"
- else
- echo "PHP not install. install run...."
- cd $src_dir
- tar zxvf php-5.3.13.tar.gz
- cd php-5.3.13
- ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/lib --with-mysql=/usr/local/mysql/bin/mysql_config --with-enable-sysvsem --enable-fpm --with-openssl --with-zlib --with-bz2 --with-curl --with-libxml-dir=/usr --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --enable-gd-native-ttf --enable-gd-jis-conv --without-iconv --with-ldap --with-mcrypt=/usr/local/libmcypt --with-zlib-dir --with-snmp --enable-zip --with-curlwrappers --with-imap --with-kerberos --with-imap-ssl --with-freetype-dir --with-mysql=/usr/local/mysql
- make && make install
- cp php.ini-development /usr/local/php/lib/php.ini
- cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
- fi
- }
- #安裝PHP擴充套件
- inst_php_ex(){
- cd $src_dir
- tar zxf memcache-2.2.5.tgz
- cd memcache-2.2.5/
- ${php_dir}/bin/phpize
- ./configure --with-php-config=${php_dir}/bin/php-config
- make
- make install
- cd ../
- tar jxf eaccelerator-0.9.5.3.tar.bz2
- cd eaccelerator-0.9.5.3
- ${php_dir}/bin/phpize
- ./configure --enable-eaccelerator=shared --with-eaccelerator-shared-memory --with-php-config=${php_dir}/bin/php-config
- make
- make install
- cd $src_dir
- tar zxf ImageMagick.tar.gz
- cd ImageMagick-6.8.1-9/
- ./configure
- make
- make install
- cd ../
- tar zxf imagick-2.3.0.tgz
- cd imagick-2.3.0/
- ${php_dir}/bin/phpize
- ./configure --with-php-config=${php_dir}/bin/php-config
- make
- make install
- cd ..
- echo "php extension installed successfully!"
- }
- #安裝nginx
- inst_nginx(){
- cd $src_dir
- tar zxf google-perftools-1.6.tar.gz
- cd google-perftools*
- ./configure
- make
- make install
- cd $src_dir
- if ( -d $nginx_dir )
- then
- echo "Nginx is installed"
- else
- cd $src_dir
- tar zxvf nginx-1.3.10.tar.gz
- cd nginx-1.3.10
- ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_stub_status_module
- make && make install
- fi
- }
- #生成一個PHP啟動指令碼,可以用service phpd start | restart | stop | show 進行啟動、重啟、關閉、檢視執行狀態
- phpd(){
- cat <<EOF >>phpd
- #!/bin/bash
- #Author: wolf
- #Date: 2013-01-03
- #
- #chkconfig: - 85 15
- #processname: php
- php=/usr/local/php/sbin/php-fpm
- conf=/usr/local/php/etc/php-fpm.conf
- case $1 in
- start)
- echo -n "Starting php"
- $php
- echo " done"
- ;;
- stop)
- echo -n "Stopping php"
- killall -9 php-fpm
- echo " done"
- ;;
- restart)
- $0 stop
- $0 start
- ;;
- show)
- ps -aux|grep php
- ;;
- *)
- echo -n "Usage: $0 {start|restart|reload|stop|test|show}"
- ;;
- esac
- EOF
- if [ -f phpd ]
- then
- /bin/cp phpd /etc/rc.d/init.d/phpd
- chmod +x /etc/rc.d/init.d/phpd
- fi
- }
- #生成nginx啟動指令碼,可以用service nginxd start | restart | stop | reload | test | show 進行重啟、關閉、啟動>、測試配置檔案、檢視狀態和重新載入。
- nginxd(){
- cat <<EOF >>nginxd
- #!/bin/bash
- #Author: wolf
- #Date: 2013-01-03
- #
- #chkconfig: - 85 15
- #description: Nginx is a World Wide Web server.
- #processname: nginx
- nginx=/usr/local/nginx/sbin/nginx
- conf=/usr/local/nginx/conf/nginx.conf
- case $1 in
- start)
- echo -n "Starting Nginx"
- $nginx -c $conf
- echo " done"
- ;;
- stop)
- echo -n "Stopping Nginx"
- killall -9 nginx
- echo " done"
- ;;
- test)
- $nginx -t -c $conf
- ;;
- reload)
- echo -n "Reloading Nginx"
- ps auxww | grep nginx | grep master | awk `{print $2}` | xargs kill -HUP
- echo " done"
- ;;
- restart)
- $0 stop
- $0 start
- ;;
- show)
- ps -aux|grep nginx
- ;;
- *)
- echo -n "Usage: $0 {start|restart|reload|stop|test|show}"
- ;;
- esac
- EOF
- if [ -s nginxd ]
- then
- /bin/cp nginxd /etc/rc.d/init.d/nginxd
- chmod +x /etc/rc.d/init.d/nginxd
- fi
- }
- cat <<EOF
- ############################################
- 1 install Nginx
- 2 install PHP
- 3 install MySQL
- 4 install LNMP
- ############################################
- ############################################
- 請選擇你要安裝的服務(1 | 2 | 3 | 4)
- EOF
- echo "請輸入一個數字: "
- read num
- case $num in
- 1)
- echo "Nginx installing........... have a rest"
- init_server
- download_src
- inst_nginx
- ;;
- 2)
- echo "PHP installing....... have a rest"
- download_src
- init_server
- inst_mysql
- inst_libmcypt
- inst_php
- inst_php_ex
- ;;
- 3)
- echo "MySQL installing...... have a rest"
- init_server
- download_src
- inst_mysql
- ;;
- 4)
- echo "LNMP installing...... have a rest"
- download_src
- init_server
- inst_mysql
- inst_libmcypt
- inst_php
- inst_php_ex
- inst_nginx
- ;;
- *)
- echo "輸入錯誤 " echo "選擇你要安裝的服務(1 | 2 | 3 | 4)"
- ;;
- esac
- #檢查安裝是否成功
- case $num in
- 4)
- if [ -s /usr/local/nginx ] && [ -s /usr/local/php ] && [ -s /usr/local/mysql ]
- then
- echo "LNMP is install completed"
- echo "Nginx basedir: /usr/local/nginx"
- echo "PHP basedir: /usr/local/php"
- echo "MySQL basedir: /usr/local/mysql"
- echo "MySQL datadir: /usr/local/mysql/data"
- else
- echo "LNMP is install fail"
- fi
- ;;
- 3)
- if [ -s /usr/local/mysql ]
- then
- echo "Nginx is install completed"
- echo "MySQL basedir: /usr/local/mysql"
- echo "MySQL datadir: /usr/local/mysql/data"
- phpd
- nginxd
- else
- echo "Nginx is install fail"
- fi
- ;;
- 2)
- if [ -s /usr/local/php ]
- then
- echo "PHP is install completed"
- echo "PHP basedir: /usr/local/php"
- phpd
- else
- echo "PHP is install fail"
- fi
- ;;
- 1)
- if [ -s /usr/local/nginx ]
- then
- echo "Nginx is install completed"
- echo "Nginx basedir: /usr/local/nginx "
- nginxd
- else
- echo "Nginx is install fail"
- fi
- ;;
- esac