LNMP表明的就是:Linux系統下Nginx+MySQL+PHP這種網站服務器架構。LNMP是如今大多數大型公司在用的web環境,nginx小巧高效的web服務器和反向代理服務器,相對於apach佔用資源更少,抗併發力更強,並且處理靜態文件速度很是快。Mysql是一個小型關係型數據庫管理系統。PHP是一種 HTML 內嵌式的語言,是一種在服務器端執行的嵌入HTML文檔的腳本語言。這四種軟件均爲免費軟件,組合到一塊兒,成爲一個免費、高效的網站服務系統。接近過年,筆者在家閒着沒事寫了個LNMP的Shell安裝腳本,筆者如今仍是個在學校學習的菜鳥,有能夠優化的地方,但願你們能提出來,感謝。php
#!/bin/bash clear cat << EOF Welcome to Ike LNMP install system!!!(^*^) 1) Install nginx 2) Install php 3) Install mysql 4) Install nginx and php 5) Install lnmp(linux nginx,mysql,php) EOF echo -n "Please you enter (1-5):" read enter clear #install pcre downloadpcre='wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz' tarpcre1='tar -zvxf pcre-8.41.tar.gz' cdpcredir='cd pcre-8.41' install2='./configure' install3='make' install4='make install' cplibpcre='cp /usr/local/lib/libpcre.so.1 /lib/libpcre.so.1' addgroup='groupadd www' adduser='useradd -s /bin/false www' adddir='mkdir /usr/local/nginx' httpdstop='service httpd stop' addlogdir='mkdir /data0/logs/nginx' dirmod='chown www:www /data0/logs/nginx -R' #install nginx downloadnginx='wget http://nginx.org/download/nginx-1.8.1.tar.gz' sec='tar -zvxf nginx-1.8.1.tar.gz' then0='cd nginx-1.8.1' then1='./configure --user=www --group=www --prefix=/usr/local/nginx' nginxinstall='make' nginxinstall2='make install' setnginx1='sed -i "s/\/scripts/$document_root/g" /usr/local/nginx/conf/nginx.conf' startnginx1='/usr/local/nginx/sbin/nginx' installnginx(){ $zlib $downloadpcre $tarpcre1 $cdpcredir $install2 $install3 $install4 $cplibpcre $addgroup $adduser $adddir $addlogdir $httpdstop $dirmod $downloadnginx $sec $then0 $then1 $nginxinstall $nginxinstall2 $setnginx1 $startnginx1 } installmysql() { rm -rf /etc/my.cnf touch /etc/my.cnf cat >> /etc/my.cnf << EOF [mysqld] character-set-server = utf8mb4 default_storage_engine = innodb log_timestamps = SYSTEM socket=/tmp/mysql.sock basedir=/usr/local/mysql datadir=/usr/local/mysql/data max_connections = 1000 max_connect_errors = 1000 table_open_cache = 1024 max_allowed_packet = 128M open_files_limit = 65535 explicit_defaults_for_timestamp=true [mysqld_safe] pid-file=/usr/local/mysql/data/fzjh.pid [log] log-error=/usr/local/mysql/log/error.log EOF if [ $(uname -p) == x86_64 ] then wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz tar -zvxf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql rm -rf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz else wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-i686.tar.gz tar -zvxf mysql-5.7.20-linux-glibc2.12-i686.tar.gz mv mysql-5.7.20-linux-glibc2.12-i686 /usr/local/mysql rm -rf mysql-5.7.20-linux-glibc2.12-i686.tar.gz fi addmysqluser='useradd mysql -s /sbin/nologin' createdatadir='mkdir /usr/local/mysql/data' installmysql='/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data' startmysql='/usr/local/mysql/support-files/mysql.server restart' $addmysqluser $createdatadir $installmysql $startmysql dbpasswd=$(cat /root/.mysql_secret | grep -v '#') /usr/local/mysql/bin/mysql -uroot -p$dbpasswd << EOF alter user 'root'@'localhost' identified by '123456' EOF } #installphp downloadphp='wget http://au1.php.net/get/php-7.1.10.tar.gz/from/this/mirror -O php-7.1.10.tar.gz' tarphp='tar -zvxf php-7.1.10.tar.gz' gophpdir='cd php-7.1.10' maketest='make' makeinstall='make install' set1='mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf ' set2='mv /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf' set3='mv php.ini-development /usr/local/php/bin/php.ini' chajian(){ if [ $(rpm -qa | grep libxml2-devel | wc -l) != 0 ];then echo libxml2 libxml2-devel is installed else yum install libxml2 libxml2-devel -y fi if [ $(rpm -qa | grep curl-devel | wc -l) != 0 ];then echo curl-devel is installed else yum install -y curl-devel fi if [ $(rpm -qa | grep wget | wc -l) != 0 ] then if [ $(rpm -qa | grep gcc-c++ | wc -l) != 0 ];then echo "wget and gcc gcc-c++ is installed" else yum install -y gcc gcc-c++ fi else yum install -y wget fi if [ $(rpm -qa | grep zlib-devel | wc -l) != 0 ];then echo "zlib-devel is install" else yum install -y zlib-devel fi } installphp(){ $downloadphp $tarphp $gophpdir if [ enter == 5 ] then ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear --with-zlib --enable-pdo --with-pdo-mysql --with-mysql--prefix=/usr/local/php else ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear --with-zlib --enable-pdo --with-pdo-mysql --with-mysql--prefix=/usr/local/php fi $maketest $makeinstall $set1 $set2 $set3 } case $enter in 1) chajian installnginx clear echo 'nginx file:/usr/local/nginx' echo 'html file:/usr/local/nginx/html' ;; 2) chajian installphp clear echo 'php file:/usr/local/php' ;; 3) chajian installmysql clear echo 'mysql file:=/usr/local/mysql' echo 'you mysql_user:root' echo you mysql_password:$dbpasswd ;; 4) chajian installnginx installphp clear echo 'nginx file:/usr/local/nginx' echo 'html file:/usr/local/nginx/html' echo 'php file:/usr/local/php' ;; 5) chajian installnginx installmysql installphp clear echo 'nginx file:/usr/local/nginx' echo 'html file:/usr/local/nginx/html' echo 'php file:/usr/local/php' echo 'mysql file:=/usr/local/mysql' echo 'you mysql_user:root' echo you mysql_password:$dbpasswd esac
接下來來解釋下腳本代碼html
clear cat << EOF Welcome to Ahui install system!!!(^*^) 1) Install nginx 2) Install php 3) Install mysql 4) Install nginx and php 5) Install lnmp(linux nginx,mysql,php) EOF echo -n "Please you enter (1-5):" read enter
首先用eof將咱們想要顯示的界面給cat打印出來,而後用read讀取一個輸入的變量給enter這個變量。mysql
#install pcre downloadpcre='wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz' tarpcre1='tar -zvxf pcre-8.41.tar.gz' cdpcredir='cd pcre-8.41' install2='./configure' install3='make' install4='make install' cplibpcre='cp /usr/local/lib/libpcre.so.1 /lib/libpcre.so.1' addgroup='groupadd www' adduser='useradd -s /bin/false www' adddir='mkdir /usr/local/nginx' httpdstop='service httpd stop' addlogdir='mkdir /data0/logs/nginx' dirmod='chown www:www /data0/logs/nginx -R' #install nginx downloadnginx='wget http://nginx.org/download/nginx-1.8.1.tar.gz' sec='tar -zvxf nginx-1.8.1.tar.gz' then0='cd nginx-1.8.1' then1='./configure --user=www --group=www --prefix=/usr/local/nginx' nginxinstall='make' nginxinstall2='make install' setnginx1='sed -i "s/\/scripts/$document_root/g" /usr/local/nginx/conf/nginx.conf' startnginx1='/usr/local/nginx/sbin/nginx' installnginx(){ $zlib $downloadpcre $tarpcre1 $cdpcredir $install2 $install3 $install4 $cplibpcre $addgroup $adduser $adddir $addlogdir $httpdstop $dirmod $downloadnginx $sec $then0 $then1 $nginxinstall $nginxinstall2 $setnginx1 $startnginx1 }
咱們將安裝nginx的須要的pcre先編譯安裝了,定義好變量名,直接用$加變量名,使用這些命令就好了,而後再安裝nginx。linux
installmysql() { rm -rf /etc/my.cnf touch /etc/my.cnf cat >> /etc/my.cnf << EOF [mysqld] character-set-server = utf8mb4 default_storage_engine = innodb log_timestamps = SYSTEM socket=/tmp/mysql.sock basedir=/usr/local/mysql datadir=/usr/local/mysql/data max_connections = 1000 max_connect_errors = 1000 table_open_cache = 1024 max_allowed_packet = 128M open_files_limit = 65535 explicit_defaults_for_timestamp=true [mysqld_safe] pid-file=/usr/local/mysql/data/fzjh.pid [log] log-error=/usr/local/mysql/log/error.log EOF if [ $(uname -p) == x86_64 ] then wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz tar -zvxf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql rm -rf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz else wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-i686.tar.gz tar -zvxf mysql-5.7.20-linux-glibc2.12-i686.tar.gz mv mysql-5.7.20-linux-glibc2.12-i686 /usr/local/mysql rm -rf mysql-5.7.20-linux-glibc2.12-i686.tar.gz fi addmysqluser='useradd mysql -s /sbin/nologin' createdatadir='mkdir /usr/local/mysql/data' installmysql='/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data' startmysql='/usr/local/mysql/support-files/mysql.server restart' $addmysqluser $createdatadir $installmysql $startmysql dbpasswd=$(cat /root/.mysql_secret | grep -v '#') }
而後是mysql,首先刪除掉系統內的my.cnf,用touch加EOF將咱們本身的my.cnf建立好,而後使用uname -p 來檢測咱們的系統是多少位的,在下載相應的mysql安裝包。nginx
chajian(){ if [ $(rpm -qa | grep libxml2-devel | wc -l) != 0 ];then echo libxml2 libxml2-devel is installed else yum install libxml2 libxml2-devel -y fi if [ $(rpm -qa | grep curl-devel | wc -l) != 0 ];then echo curl-devel is installed else yum install -y curl-devel fi if [ $(rpm -qa | grep wget | wc -l) != 0 ] then if [ $(rpm -qa | grep gcc-c++ | wc -l) != 0 ];then echo "wget and gcc gcc-c++ is installed" else yum install -y gcc gcc-c++ fi else yum install -y wget fi if [ $(rpm -qa | grep zlib-devel | wc -l) != 0 ];then echo "zlib-devel is install" else yum install -y zlib-devel fi
PS:這些是用if語句檢測一些要依賴的包c++
#installphp downloadphp='wget http://au1.php.net/get/php-7.1.10.tar.gz/from/this/mirror -O php-7.1.10.tar.gz' tarphp='tar -zvxf php-7.1.10.tar.gz' gophpdir='cd php-7.1.10' maketest='make' makeinstall='make install' set1='mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf ' set2='mv /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf' set3='mv php.ini-development /usr/local/php/bin/php.ini' installphp(){ $downloadphp $tarphp $gophpdir if [ enter == 5 ] then ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear --with-zlib --enable-pdo --with-pdo-mysql --with-mysql--prefix=/usr/local/php else ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear --with-zlib --enable-pdo --with-pdo-mysql --with-mysql--prefix=/usr/local/php fi $maketest $makeinstall $set1 $set2 $set3 }
此爲php的安裝方法,和上面幾個差很少一個樣。筆者以爲入股安裝lnmp必須使用pod-mysql來使mysql和php能夠連接在一塊兒。web
case $enter in 1) chajian installnginx clear echo 'nginx file:/usr/local/nginx' echo 'html file:/usr/local/nginx/html' ;; 2) chajian installphp clear echo 'php file:/usr/local/php' ;; 3) chajian installmysql clear echo 'mysql file:=/usr/local/mysql' echo 'you mysql_user:root' echo you mysql_password:$dbpasswd ;; 4) chajian installnginx installphp clear echo 'nginx file:/usr/local/nginx' echo 'html file:/usr/local/nginx/html' echo 'php file:/usr/local/php' ;; 5) chajian installnginx installmysql installphp clear echo 'nginx file:/usr/local/nginx' echo 'html file:/usr/local/nginx/html' echo 'php file:/usr/local/php' echo 'mysql file:=/usr/local/mysql' echo 'you mysql_user:root' echo you mysql_password:$dbpasswd esac
最後使用case多選來經過enter變量輸入的值來,根據case來進行相對應的安裝。
最後感謝各位的閱讀,但願各位有能夠優化的地方提出來,互相學習。sql