Shell script: one-click installation of LAMP and LNMP scripts

Source: Internet
Author: User
Tags mcrypt aliyun

Shell script: one-click installation of LAMP and LNMP scripts

After three days of writing and testing, I finally completed the script with the longest Code. There were a total of 253 lines. LAMP was installed on two machines, and LNMP passed smoothly. The script can be used as a production environment.

Notes for the script:
1. The script is applicable to CentOS 6.5 or above and is clean. If other environments have been installed, You need to modify the script content;
2: The script is applicable to apache 2.4.x, mysql5.5.x, and php5.5.x;
3: mysql, php, apache, and nginx installation packages must be downloaded to the/usr/local/src directory in advance. If the package name is incorrect, you need to change the package name in the script;

The script installation version is as follows. If you need another name, modify the software installation package name and the corresponding directory name;
Httpd-2.4.7.tar.gz running mysql-5.6.25.tar.gz running nginx-1.6.2.tar.gz, php-5.6.8.tar.gz
4: no judgment is added to the script. The set environment is clean;

The script content is as follows:

#! /Bin/bash
# Written by mofansheng @ 2015-11-06
# One key install LAMP or LNMP scripts; apply to apache 2.4.x, mysql 5.6.x, nginx 1.6.x;
 
./Etc/init. d/functions
 
# Check the results of the command execution
 
Function check_ OK (){
If [$? -Eq 0]
Then
Continue
Else
Echo "please check error"
Exit
Fi
}
 
Function yum_update (){
# Set yum repos
Echo "= update yum repos, it will take serval mintinues ="
Yum install wget-y
Mv/etc/yum. repos. d/CentOS-Base.repo/etc/yum. repos. d/CentOS-Base.repo.bak
Wget-O/etc/yum. repos. d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &>/dev/null
Wget-O/etc/yum. repos. d/epel. repo http://mirrors.aliyun.com/repo/epel-6.repo &>/dev/null
Yum clean all &>/dev/null
Yum makecache &>/dev/null
Check_ OK
Action "yum repos update is OK"/bin/true
}
 
Function yum_depend (){
# Install dependencies packages
Yum install wget gcc-c ++ make re2c curl-devel libxml2 libxml2-devel libjpeg-devel libpng-devel libmcrypt ready-devel zlib-devel openssl-devel freetype- devel gd-devel perl-devel ncurses-devel bison-devel libtool gettext-devel cmake bzip2 bzip2-devel pcre-devel-y
}
 
Function install_mysql (){
Echo "mysql5.6.25 will be installed, please be patient"
Cd/usr/local/src
Tar-zxf mysql-5.6.25.tar.gz
Cd mysql-5.6.25
Cmake-DCMAKE_INSTALL_PREFIX =/usr/local/mysql-DMYSQL_DATADIR =/data/mysql-DMYSQL_UNIX_ADDR =/usr/local/mysql. sock-DSYSCONFDIR =/etc-cores = 1-cores = 1-cores = 1-cores = 1-cores = 1-DWITH_READLINE = 1-DMYSQL_TCP_PORT = 3306-DENABLED_LOCAL_INFILE = 1-cores = 1-DEXTRA_CHARSETS = all-DDEFAULT_CHARSET = utf8-DDEFAULT_COLLATION = utf8_general_ci
Check_ OK
Make & make install
Check_ OK

Useradd-M-s/sbin/nologin mysql
Mkdir-p/data/mysql
Chown-R mysql: mysql/data/mysql/
Chown-R mysql: mysql/usr/local/mysql/
Check_ OK
Cd/usr/local/mysql/scripts/
./Mysql_install_db -- basedir =/usr/local/mysql/-- datadir =/data/mysql/-- user = mysql
Check_ OK
/Bin/cp/usr/local/mysql/my. cnf/etc/my. cnf
Sed-I '/^ \ [mysqld \] $/a \ user = mysql \ ndatadir =/data/mysql \ ndefault_storage_engine = InnoDB \ n'/etc/my. cnf
Check_ OK

Cp/usr/local/mysql/support-files/mysql. server/etc/init. d/mysqld
Sed-I's # ^ datadir = # datadir =/data/mysql # '/etc/init. d/mysqld
Sed-I's # ^ basedir = # basedir =/usr/local/mysql # '/etc/init. d/mysqld
Service mysqld start
Chkconfig -- add mysqld
Chkconfig mysqld on
Check_ OK

Iptables-a input-m state -- state NEW-m tcp-p tcp -- dport 3306-j ACCEPT
/Etc/init. d/iptables save
Check_ OK
Echo "export PATH = $ PATH:/usr/local/mysql/bin">/etc/profile
Source/etc/profile
Check_ OK
}
 
Function install_apache (){
Echo "apache2.4.7 will be installed, please be patient"
Cd/usr/local/src
Wget http://mirrors.cnnic.cn/apache/apr/apr-1.5.2.tar.gz
Wget http://mirrors.cnnic.cn/apache/apr/apr-util-1.5.4.tar.gz
Check_ OK
Tar zxf apr-1.5.2.tar.gz
Cd apr-1.5.2
./Configure -- prefix =/usr/local/apr
Check_ OK
Make & make install
Check_ OK

Cd/usr/local/src
Tar zxf apr-util-1.5.4.tar.gz
Cd apr-util-1.5.4
./Configure -- prefix =/usr/local/apr-util -- with-apr =/usr/local/apr
Check_ OK
Make & make install
Check_ OK

Cd/usr/local/src
Tar zxf httpd-2.4.7.tar.gz
/Bin/cp-r apr-1.5.2/usr/local/src/httpd-2.4.7/srclib/apr
/Bin/cp-r apr-util-1.5.4/usr/local/src/httpd-2.4.7/srclib/apr-util
Cd httpd-2.4.7
. /Configure -- prefix =/usr/local/apache2 -- with-apr =/usr/local/apr -- with-apr-util =/usr/local/apr-util/-- -pcre -- enable-mod-shared = most -- enable-so -- with-encoded ded-apr
Check_ OK
Make & make install
Check_ OK

Echo "export PATH = $ PATH:/usr/local/apache2/bin">/etc/profile
Source/etc/profile
Check_ OK

Iptables-a input-m state -- state NEW-m tcp-p tcp -- dport 80-j ACCEPT
/Etc/init. d/iptables save
Check_ OK
/Usr/local/apache2/bin/apachectl
Check_ OK
}
 
Function install_php (){
Echo "php5.6.8 will be installed, please be patient"
Cd/usr/local/src
Tar zxf php-5.6.8.tar.gz
Cd php-5.6.8
. /Configure -- prefix =/usr/local/php -- with-apxs2 =/usr/local/apache2/bin/apxs -- with-config-file-path =/usr/local/php/ etc -- with-mysql =/usr/local/mysql -- with-libxml-dir -- with-gd -- with-jpeg-dir -- with-png-dir -- with-freetype-dir -- with-iconv-dir -- with-zlib-dir -- with-bz2 -- with-openssl -- with-mcrypt -- enable-soap -- enable-gd-native-ttf -- enable-mbstring -- enable -sockets -- enable-exif -- disable-ipv6
Check_ OK
Make & make install
Check_ OK

Cp/usr/local/src/php-5.6.8/php. ini-production/usr/local/php/etc/php. ini
Sed-I's # ^; date. timezone = # date. timezone = Asia/Shanghai # '/usr/local/php/etc/php. ini
Check_ OK

}
 
Function set_lamp (){
Sed-I '/AddType application \/x-gzip. gz. tgz/a \ AddType application/x-httpd-php. php \ n'/usr/local/apache2/conf/httpd. conf
Sed-I 'ssf-index.html#index.html index. php # '/usr/local/apache2/conf/httpd. conf
Sed-I '/# ServerName www.example.com: 80/a \ ServerName localhost: 80 \ n'/usr/local/apache2/conf/httpd. conf
Check_ OK
Cat>/usr/local/apache2/htdocs/test. php <EOF
<? Php
Echo "PHP is OK \ n ";
?>
EOF
 
/Usr/local/apache2/bin/apachectl graceful
Check_ OK
Curl localhost/test. php
Check_ OK
Action "LAMP is install success"/bin/true
}
 
Function install_phpfpm (){
Echo "php5.6.8 will be installed, please be patient"
Useradd-s/sbin/nologin php-fpm
Cd/usr/local/src
Tar zxf php-5.6.8.tar.gz
Cd php-5.6.8
. /Configure -- prefix =/usr/local/php-fpm -- with-config-file-path =/usr/local/php-fpm/etc -- enable-fpm -- with-fpm -user = php-fpm -- with-fpm-group = php-fpm -- with-mysql = mysqlnd -- with-pdo-mysql = mysqlnd -- with-mysqli = mysqlnd -- with-libxml -dir -- with-gd -- with-jpeg-dir -- with-png-dir -- with-freetype-dir -- with-iconv-dir -- with-zlib-dir -- with-mcrypt -- enable-soap -- enable-gd-native-ttf -- enable-ftp -- enable-exif -- disable-ipv6 -- with-pear -- with-curl -- enable-bcmath -- enable-mbstring -- enable -sockets -- with-gettext
Check_ OK
Make & make install
Check_ OK

Cp/usr/local/src/php-5.6.8/php. ini-production/usr/local/php-fpm/etc/php. ini
Sed-I's # ^; date. timezone = # date. timezone = Asia/Shanghai # '/usr/local/php-fpm/etc/php. ini
Cd/usr/local/php-fpm/etc/
Mv php-fpm.conf.default php-fpm.conf
Check_ OK

Cp/usr/local/src/php-5.6.8/sapi/fpm/init. d. php-fpm/etc/init. d/php-fpm
Chmod 755/etc/init. d/php-fpm
Chkconfig -- add php-fpm
Chkconfig php-fpm on
Service php-fpm start
Check_ OK
}
 
Function install_nginx (){
Echo "nginx1.6.2 will be installed, please be patient"
Cd/usr/local/src
Tar zxf nginx-1.6.2.tar.gz
Cd nginx-1.6.2
./Configure -- prefix =/usr/local/nginx -- with-pcre -- with-http_stub_status_module -- with-http_ssl_module -- with-http_gzip_static_module
Check_ OK
Make & make install
Check_ OK

/Usr/local/nginx/sbin/nginx
Check_ OK
}
 
Function set_lnmp (){
Sed-I '56a \ location ~ \. Php $ {\ n \ root html; \ n \ fastcgi_pass 127.0.0.1: 9000; \ n \ fastcgi_index index. php; \ n \ fastcgi_param SCRIPT_FILENAME/usr/local/nginx/html $ fastcgi_script_name; \ n \ include fastcgi_params; \ n \} \ n'/usr/local/nginx/conf/nginx. conf
/Usr/local/nginx/sbin/nginx-s reload
Check_ OK
Echo-e' <? Php \ n echo "nginx and PHP is OK"; \ n?> \ N'>/usr/local/nginx/html/index. php
Curl localhost/index. php
Check_ OK
Action "LNMP is install success"/bin/true
}
 
Function install_lamp (){
Echo "apache 2.4.7 mysql 5.6.24 php5.6.8 will be installed"
Echo "= update yum repos and install dependecies packages, it will take serval mintinues ="
Yum_update
Check_ OK
Yum_depend
Check_ OK
Install_mysql
Check_ OK
Install_apache
Check_ OK
Install_php
Check_ OK
Set_lamp
}
 
Function install_lnmp (){
Echo "nginx1.6.2 mysql 5.6.24 php5.6.8 will be installed"
Echo "= update yum repos and install dependecies packages, it will take serval mintinues ="
Yum_update
Check_ OK
Yum_depend
Check_ OK
Install_mysql
Check_ OK
Install_phpfpm
Check_ OK
Install_nginx
Check_ OK
Set_lnmp
}
 
Cat <EOF
1: [install LAMP]
2: [install LNMP]
3: [exit]
EOF
Read-t 10-p "please input the num you want:" input
Case $ {input} in
1)
Install_lamp
;;
2)
Install_lnmp
;;
3)
Exit
;;
*)
Printf "You must input only in {1 | 2 | 3} \ n"
Esac

You may also like the following LAMP-related content:

Install Xcache and Memcached on the LAMP platform to accelerate website operation

Build a LAMP platform environment in CentOS 7

CentOS 6.5 system installation and configuration LAMP (Apache + PHP5 + MySQL) server environment

Configure the LAMP + phpMyAdmin PHP (5.5.9) development environment in Ubuntu 14.04

Install LAMP in Ubuntu 14.10

LAMP combines NFS to build a small blog site

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.