菜单

编译安装LNMP环境

2018年11月17日 - linux
#!/bin/bash
######################################
# author 小学课本的小明               #
# website:  https://www.yfdou.com     #
# One click Install LNMP environment #
######################################
 
######start root to run######
ROOT_UID=0
E_NOTROOT=67
if [ "$UID" -ne "$ROOT_UID" ]
  then
    echo "Must be root to run this script"
    exit $E_NOROOT
fi
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
ulimit -SHn 65535
#######end root to run#######

#########Source package suffix description#############
YUM_SOURCE=https://mirrors.163.com/.help/CentOS7-Base-163.repo
SOURCE_DIR=/root/software            ##Source package suffix directory
INSTALL_DIR=/app                     ##Installation  directory
MYSQL_DATA=/mysql/data               ##For instance  Database location      
MYSQL_USER=mysql                     ##For instance  MYSQL USER
FPM_USER=www                         ##For instance  PHP-FPM USER
NGX_USER=$FPM_USER                   ##For instance  NGINX USER
MYSQL=mysql-5.7.10                   ##For instance  mysql-5.7.10.tar.gz
PHP=php-7.1.5                        ##For instance  php-7.1.5.tar.bz2
NGINX=nginx-1.12.0                   ##For instance  nginx-1.12.0.tar.gz
BOOST=boost_1_59_0                   ##For instance  boost_1_59_0.tar.bz2
LIBICONV=libiconv-1.14               ##For instance  libiconv-1.14.tar.gz
LIBMCRYPT=libmcrypt-2.5.8            ##For instance  libmcrypt-2.5.8.tar.gz
MHASH=mhash-0.9.9.9                  ##For instance  mhash-0.9.9.9.tar.bz2
MCRYPT=mcrypt-2.6.8                  ##For instance  mcrypt-2.6.8.tar.gz
PCRE=pcre-8.39                       ##For instance  pcre-8.39.tar.bz2
FREETYPE=freetype-2.7                ##For instance  freetype-2.7.tar.bz2
OPENSSL=openssl-1.0.2l               ##For instance  openssl-1.0.2l.tar.gz
NGX_STICKY=nginx-sticky              ##For instance  nginx-sticky.tar.gz
NGX_CACHE_PURGE=ngx_cache_purge-2.3  ##For instance  ngx_cache_purge-2.3.tar.gz
#########end Source package suffix description##########

# update user set authentication_string=PASSWORD('123456') where User='root';
# alter user 'root'@'localhost' identified by '123456';
# flush privileges;
# GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

##start functions##
. /etc/init.d/functions
###end functions###
 
#check the results of the command execution
 
function check_ok(){
  if [ $? -eq 0 ]
  then
    continue
  else
    echo "please check error"
    exit
  fi
}

function check_DIR(){
 if [ ! -d "${SOURCE_DIR}" ] 
   then
     action "${SOURCE_DIR} directory does not exist" /bin/false
	 exit
 fi

 if [ ! -d "${INSTALL_DIR}" ] 
   then
     action "${INSTALL_DIR} directory does not exist" /bin/false
	 action "Being created ${INSTALL_DIR} directory" /bin/true
     mkdir -p $INSTALL_DIR
 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 $YUM_SOURCE &>/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 gcc* glib* gcc-c++ make re2c curl curl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel libmcrypt libmcrypt-devel zlib zlib-devel openssl* openssl-devel freetype freetype-devel gd gd-devel perl perl-devel ncurses ncurses-devel bison bison-devel libtool gettext gettext-devel cmake bzip2 bzip2-devel pcre pcre-devel -y &>/dev/null
}
 
function install_mysql(){
 echo "$MYSQL will be installed,please be patient"
 cd $SOURCE_DIR
 tar jxf $BOOST.tar.bz2
 tar -zxf $MYSQL.tar.gz
 cd $MYSQL
 cmake -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR/mysql -DMYSQL_DATADIR=$MYSQL_DATA -DMYSQL_UNIX_ADDR=$INSTALL_DIR/mysql/mysql.sock -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DDOWNLOAD_BOOST=1 -DWITH_BOOST=$SOURCE_DIR/$BOOST
 #sh BUILD/autorun.sh
 #./configure --prefix=$INSTALL_DIR/mysql --with-unix-socket-path=$INSTALL_DIR/mysql/tmp/mysql.sock --localstatedir=$MYSQL_DATA --enable-assembler --enable-thread-safe-client --with-mysqld-user=$MYSQL_USER --with-big-tables --without-debug --with-pthread --with-extra-charsets=complex --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition,innobase --with-plugin-PLUGIN --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static
 check_ok
 make && make install
 check_ok
  
 useradd -M -s /sbin/nologin $MYSQL_USER
 mkdir -p $MYSQL_DATA
 chown -R $MYSQL_USER:$MYSQL_USER $MYSQL_DATA
 chown -R $MYSQL_USER:$MYSQL_USER $INSTALL_DIR/mysql
 check_ok
 
 #cd $INSTALL_DIR/mysql/scripts/
 #./mysql_install_db --basedir=$INSTALL_DIR/mysql/ --datadir=$MYSQL_DATA --user=$MYSQL_USER
 
 cd $INSTALL_DIR/mysql
 ./bin/mysqld --initialize --basedir=$INSTALL_DIR/mysql/ --datadir=$MYSQL_DATA --user=$MYSQL_USER
 check_ok
 /bin/cp $SOURCE_DIR/$MYSQL/support-files/my-default.cnf /etc/my.cnf
 sed -i '/^\[mysqld\]$/a\user = '"$MYSQL_USER"'\ndatadir = '"${INSTALL_DIR}"'/mysql/data\ndefault_storage_engine = InnoDB\n' /etc/my.cnf
 check_ok
  
 cp $SOURCE_DIR/$MYSQL/support-files/mysql.server /etc/init.d/mysqld
 cp $SOURCE_DIR/$MYSQL/support-files/mysql.server $INSTALL_DIR/mysql/bin/mysql.server
 sed -i 's#^datadir=#datadir='"${MYSQL_DATA}"'#' /etc/init.d/mysqld
 sed -i 's#^basedir=#basedir='"${INSTALL_DIR}"'/mysql#' /etc/init.d/mysqld
 chmod +x /etc/init.d/mysqld
 chmod +x $INSTALL_DIR/mysql/bin/mysql.server
 service mysqld start
 chkconfig --add mysqld
 chkconfig mysqld on
 check_ok
 
 cat > /etc/my.cnf<<EOF
[client]
port		= 3306
socket		= /tmp/mysql.sock

[mysqld]
#skip-grant-tables
port		= 3306
socket		= /tmp/mysql.sock
datadir = ${MYSQL_DATA}
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M

#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535

log-bin=mysql-bin
binlog_format=mixed
server-id	= 1
expire_logs_days = 10

default_storage_engine = InnoDB
#innodb_file_per_table = 1
#innodb_data_home_dir = ${MYSQL_DATA}
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = ${MYSQL_DATA}
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
EOF
 service mysqld restart
 check_ok
 
 #firewall-cmd --zone=public --add-port=3306/tcp --permanent
 #firewall-cmd --reload
 #iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
 #service iptables save
 #service iptables restart
 #check_ok
 
 
 echo "export PATH=\$PATH:"${INSTALL_DIR}"/mysql/bin" >>/etc/profile
 source /etc/profile
 check_ok
}
 
function install_phpfpm(){
 echo "$PHP will be installed,please be patient"
 cd $SOURCE_DIR
 ##install libiconv##
 tar zxvf $LIBICONV.tar.gz
 cd $LIBICONV
 ./configure --prefix=/usr/local/libiconv
 make && make install
 cd ..
 ##install libmcrypt##
 tar zxvf $LIBMCRYPT.tar.gz
 cd $LIBMCRYPT
 ./configure
 make && make install
 cd ..
 ##install mhash##
 tar jxvf $MHASH.tar.bz2
 cd $MHASH
 ./configure
 make && make install
 cd ..
 ##install mcrypt##
 tar zxvf $MCRYPT.tar.gz
 echo "/usr/local/lib/" >> /etc/ld.so.conf && ldconfig
 cd $MCRYPT
 ./configure
 make && make install
 cd ..
 ##install freetype##
 tar jxvf $FREETYPE.tar.bz2
 cd $FREETYPE
 ./configure
 make && make install
 cd ..

 useradd -s /sbin/nologin $FPM_USER
 #tar zxf $PHP.tar.gz
 tar jxf $PHP.tar.bz2
 cd $PHP
 ./configure --prefix=$INSTALL_DIR/php-fpm --with-config-file-path=$INSTALL_DIR/php-fpm/etc --enable-fpm --with-fpm-user=$FPM_USER --with-fpm-group=$FPM_USER --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=/usr/local/libiconv --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-exif --with-pear --with-curl --enable-bcmath --enable-mbstring --with-gettext --with-xmlrpc --with-openssl --enable-short-tags --enable-sockets --enable-zend-multibyte --enable-mbstring --enable-static --enable-sigchild --enable-pcntl --enable-pdo --enable-zip --enable-opcache --enable-maintainer-zts --enable-sysvsem --enable-inline-optimization --enable-mbregex 
 check_ok
 make && make install
 check_ok
  
 cp $SOURCE_DIR/$PHP/php.ini-production $INSTALL_DIR/php-fpm/etc/php.ini
 sed -i 's#^;date.timezone =#date.timezone=Asia/Shanghai#' $INSTALL_DIR/php-fpm/etc/php.ini
 cd $INSTALL_DIR/php-fpm/etc/
 mv php-fpm.conf.default php-fpm.conf
 cp $INSTALL_DIR/php-fpm/etc/php-fpm.d/www.conf.default $INSTALL_DIR/php-fpm/etc/php-fpm.d/www.conf
 check_ok
  
 cp $SOURCE_DIR/$PHP/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
 service php-fpm start
 check_ok
}
 
function install_nginx(){
 echo "$NGINX will be installed,please be patient"
 cd $SOURCE_DIR
 ##install pcre##
 tar jxvf $PCRE.tar.bz2
 cd $PCRE
 ./configure
 make && make install
 cd ..
 
 tar zxf $OPENSSL.tar.gz
 cd $OPENSSL
 ./config
 make && make install
 cd ../
 
 tar zxf $NGX_STICKY.tar.gz
 tar zxf $NGX_CACHE_PURGE.tar.gz
 useradd -s /sbin/nologin $NGX_USER
 tar zxf $NGINX.tar.gz
 cd $NGINX
 ./configure --prefix=$INSTALL_DIR/nginx --user=$NGX_USER --group=$NGX_USER --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=$SOURCE_DIR/$NGX_STICKY --with-openssl=$SOURCE_DIR/$OPENSSL --with-pcre=$SOURCE_DIR/$PCRE --add-module=$SOURCE_DIR/$NGX_CACHE_PURGE
 check_ok
 make && make install
 check_ok
 
cat > /etc/init.d/nginx << EOF
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    \$all
# Required-Stop:     \$all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

# Author:   lijunye
# website:  https://lijunye.cn

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "\$NETWORKING" = "no" ] && exit 0

nginx="${INSTALL_DIR}/nginx/sbin/nginx"
prog=\$(basename \$nginx)

sysconfig="/etc/sysconfig/\$prog"
lockfile="/var/lock/subsys/nginx"
pidfile="${INSTALL_DIR}/nginx/logs/nginx.pid"

NGINX_CONF_FILE="${INSTALL_DIR}/nginx/conf/nginx.conf"

[ -f \$sysconfig ] && . \$sysconfig


start() {
    [ -x \$nginx ] || exit 5
    [ -f \$NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting \$prog: "
    daemon \$nginx -c \$NGINX_CONF_FILE
    retval=\$?
    echo
    [ \$retval -eq 0 ] && touch \$lockfile
    return \$retval
}

stop() {
    echo -n $"Stopping \$prog: "
    killproc -p \$pidfile \$prog
    retval=\$?
    echo
    [ \$retval -eq 0 ] && rm -f \$lockfile
    return \$retval
}

restart() {
    configtest_q || return 6
    stop
    start
}

reload() {
    configtest_q || return 6
    echo -n $"Reloading \$prog: "
    killproc -p \$pidfile \$prog -HUP
    echo
}

configtest() {
    \$nginx -t -c \$NGINX_CONF_FILE
}

configtest_q() {
    \$nginx -t -q -c \$NGINX_CONF_FILE
}

rh_status() {
    status \$prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

# Upgrade the binary with no downtime.
upgrade() {
    local oldbin_pidfile="\${pidfile}.oldbin"

    configtest_q || return 6
    echo -n $"Upgrading \$prog: "
    killproc -p \$pidfile \$prog -USR2
    retval=\$?
    sleep 1
    if [[ -f \${oldbin_pidfile} && -f \${pidfile} ]];  then
        killproc -p \$oldbin_pidfile \$prog -QUIT
        success $"\$prog online upgrade"
        echo 
        return 0
    else
        failure $"\$prog online upgrade"
        echo
        return 1
    fi
}

# Tell nginx to reopen logs
reopen_logs() {
    configtest_q || return 6
    echo -n $"Reopening \$prog logs: "
    killproc -p \$pidfile \$prog -USR1
    retval=\$?
    echo
    return \$retval
}

case "\$1" in
    start)
        rh_status_q && exit 0
        \$1
        ;;
    stop)
        rh_status_q || exit 0
        \$1
        ;;
    restart|configtest|reopen_logs)
        \$1
        ;;
    force-reload|upgrade) 
        rh_status_q || exit 7
        upgrade
        ;;
    reload)
        rh_status_q || exit 7
        \$1
        ;;
    status|status_q)
        rh_\$1
        ;;
    condrestart|try-restart)
        rh_status_q || exit 7
        restart
        ;;
    *)
        echo $"Usage: \$0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
        exit 2
esac
EOF
 
 chmod +x /etc/init.d/nginx
 chkconfig --add nginx
 chkconfig nginx on
 check_ok
 
 #firewall-cmd --zone=public --add-port=80/tcp --permanent
 #firewall-cmd --reload
 #iptables -A INPUT -p tcp --dport 80 -j ACCEPT
 #service iptables save
 #service iptables restart
 
 yum install -y libprofiler.so.0 libssl.so.6 &>/dev/null
 
 $INSTALL_DIR/nginx/sbin/nginx
 #/etc/init.d/nginx start
 #systemctl daemon-reload
 #service nginx start
 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  /app/nginx/html$fastcgi_script_name;\n\    include        fastcgi_params;\n\}\n' $INSTALL_DIR/nginx/conf/nginx.conf
 $INSTALL_DIR/nginx/sbin/nginx -s reload
 #/etc/init.d/nginx reload
 check_ok
 echo -e '<?php\n phpinfo();\n?>\n' >$INSTALL_DIR/nginx/html/index.php
 curl -I localhost/index.php
 check_ok
 action "LNMP is install success" /bin/true
}
 
function install_lnmp(){
 echo "$NGINX $MYSQL $PHP will be installed"
 echo "===update yum repos and install dependecies packages,it will take serval mintinues==="
 check_DIR
 check_ok
 yum_update
 check_ok
 yum_depend
 check_ok
 install_mysql
 check_ok
 install_phpfpm
 check_ok
 install_nginx
 check_ok
 set_lnmp
}

cat <<EOF
######INSTALLATION INFORMATION######
##Source directory ${SOURCE_DIR}
##Installation directory ${INSTALL_DIR}
##${MYSQL}.tar.gz
##${PHP}.tar.bz2
##${NGINX}.tar.gz
##${BOOST}.tar.bz2
##${LIBICONV}.tar.gz
##${LIBMCRYPT}.tar.gz
##${MHASH}.tar.bz2
##${MCRYPT}.tar.gz
##${PCRE}.tar.bz2
##${LIBUNWIND}.tar.gz
##${GPERFTOOLS}.tar.gz
##${OPENSSL}.tar.gz
##${NGX_STICKY}.tar.gz
##${NGX_CACHE_PURGE}.tar.gz
#######INSTALLATION OPTIONS######
    1:[install LNMP]
    2:[exit]
EOF
read -t 60 -p "please input the num you want:" input
case ${input} in
 1)
 install_lnmp
 ;;
 2)
 exit
 ;;
 *)
 printf "\nOperation timeout!!!\nPlease re-enter sh install_lnmp.sh\nYou must input only in {1|2}\n"
esac

 

编译安装LNMP环境》有2个想法

大学里的胖子

感谢大神的分享!!!!

回复
大学里的胖子

脚本跑得有点慢

回复

发表评论

电子邮件地址不会被公开。 必填项已用*标注

18 − 7 =