关闭

自动编译搭建lnmp

177人阅读 评论(0) 收藏 举报
分类:
#!/bin/bash
#by liuweigan 2016/8/22
#检查当前用户是否root用户
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root!"
    exit 1
fi
Color_Text()
{
  echo -e " \e[0;$2m$1\e[0m"
}
Echo_Red()
{
  echo $(Color_Text "$1" "31")
}
Echo_Green()
{
  echo $(Color_Text "$1" "32")
}
#选择环境是否安装mysql
Stack=$1
case "${Stack}" in
    lnmp)
        InstallMysql=YES
        ;;
    web)
        InstallMysql=NO
        ;;
    *)
        Echo_Red "Usage: $0 {lnmp|web}"
        Echo_Green "WARNING:Select web will not install mysql on this server!"
        exit
        ;;
esac
#基础环境安装
yum -y install wget gcc gcc-c++ make autoconf automake libtool
#安装包下载
mkdir -p /data/soft
rm /data/soft/* -rf
cd /data/soft
wget http://exim.mirror.fr/pcre/pcre-8.34.tar.gz || { echo pcre wget error;exit; }
wget http://zlib.net/zlib-1.2.8.tar.gz || { echo zlib wget error;exit; }
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz || { echo openssl wget error;exit; }
wget http://nginx.org/download/nginx-1.8.0.tar.gz || { echo nginx wget error;exit; }
wget http://cn2.php.net/distributions/php-5.4.31.tar.gz || { echo php wget error;exit; }
wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz || { echo libmcrypt wget error;exit; }
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz || { echo libiconv wget error;exit; }
wget http://pecl.php.net/get/mongo-1.6.11.tgz || { echo mongo wget error;exit; }
wget http://pecl.php.net/get/memcache-3.0.6.tgz || { echo memcache wget error;exit; }
wget https://github.com/phpredis/phpredis/archive/2.2.4.tar.gz || { echo phpredis wget error;exit; }
wget http://pecl.php.net/get/imagick-3.1.0RC1.tgz || { echo imagick wget error;exit; }
#lnmp环境下mysql和web一个服务器
if [ $InstallMysql = YES ];then
    wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz || { echo cmake wget error;exit; }
    wget http://ftp.ntu.edu.tw/pub/MySQL/Downloads/MySQL-5.5/mysql-5.5.50.tar.gz || { echo mysql wget error;exit; }
fi
#ImageMagick下载的是最新版本7,无法编译通过,所以使用旧版本6,这里使用6.9.5-8
wget http://www.imagemagick.org/download/ImageMagick-6.9.5-8.tar.gz || { echo ImageMagick wget error;exit; }
#解压缩
tarfiles=`ls -1p | grep -v '/'`
for tarfile in ${tarfiles}
    do
        echo tar -zxvf ${tarfile}
        tar -zxvf ${tarfile}
    done
#安装pcre
echo =======================================
echo "start install pcre。。。。。。。。。。"
echo =======================================
cd /data/soft/pcre-8.34
./configure --prefix=/usr/local/pcre
make && make install
if [ $? = 0 ];then
    echo =======================================
    echo "success:pcre install ok!"
    echo =======================================
else
    echo "error:pcre install fail!"
    exit
fi
#安装zlib
echo =======================================
echo "start install zlib。。。。。。。。。。"
echo =======================================
cd /data/soft/zlib-1.2.8
./configure --prefix=/usr/local/zlib
make && make install
if [ $? = 0 ];then
    echo =======================================
    echo "success:zlib install ok!"
    echo =======================================
else
    echo "error:zlib install fail!"
    exit
fi
#安装openssl
echo =======================================
echo "start install openssl。。。。。。。。。。"
echo =======================================
cd  /data/soft/openssl-1.0.1c
./config --prefix=/usr/local/openssl
make &&make install
if [ $? = 0 ];then
    echo =======================================
    echo "success:openssl install ok!"
    echo =======================================
else
    echo "error:openssl install fail!"
    exit
fi
#安装nginx
echo =======================================
echo "start install nginx。。。。。。。。。。"
echo =======================================
mkdir -p /usr/local/webserver
cd /data/soft/nginx-1.8.0
./configure --prefix=/usr/local/webserver/nginx --user=nginx --group=nginx --sbin-path=/usr/local/webserver/nginx/nginx --conf-path=/usr/local/webserver/nginx/nginx.conf --pid-path=/usr/local/webserver/nginx/nginx.pid --with-http_ssl_module --with-pcre=/data/soft/pcre-8.34 --with-zlib=/data/soft/zlib-1.2.8 --with-openssl=/data/soft/openssl-1.0.1c
make &&make install
if [ $? = 0 ];then
    echo =======================================
    echo "success:nginx install ok!"
    echo =======================================
else
    echo "error:nginx install fail!"
    exit
fi
#nginx用户和用户组
cat /etc/group|grep -iq nginx && echo 'group nginx already exists!' || groupadd nginx
cat /etc/passwd|grep -iq nginx && echo 'user nginx already exists!' || useradd nginx -g nginx -M -s /sbin/nologin
#安装mysql
#基础环境安装
if [ $InstallMysql = YES ];then
    echo =======================================
    echo "start install mysql。。。。。。。。。。"
    echo =======================================
    yum -y install wget gcc gcc-c++ make ncurses-devel
    cd /data/soft/cmake-2.8.10.2
    ./configure
    make && make install
    if [ $? = 0 ];then
        echo =======================================
        echo "success:cmake install ok!"
        echo =======================================
    else
        echo "error:cmake install fail!"
        exit
    fi
#mysql用户和用户组
    cat /etc/group|grep -iq mysql && echo 'group mysql already exists!' || groupadd mysql
    cat /etc/passwd|grep -iq mysql && echo 'user mysql already exists!' || useradd -M -g mysql -s /sbin/nologin mysql
#mysql数据目录
    mkdir -p /wwwroot/database/mysql/data
    chown -R mysql.mysql /wwwroot/database/mysql/data
    cd /data/soft/mysql-5.5.50
    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/wwwroot/database/mysql/data -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
    make && make install
    if [ $? = 0 ];then
        echo =======================================
        echo "success:mysql install ok!"
        echo =======================================
    else
        echo "error:mysql install fail!"
        exit
    fi
fi
#php安装
#基础环境安装
yum -y install bzip2 bzip2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel libxml2 libxml2-devel openssl openssl-devel
#libiconv
cd /data/soft/libiconv-1.14
./configure --prefix=/usr/local/libiconv
if [ $? = 0 ];then
    echo =======================================
    echo "success:libiconv install ok!"
    echo =======================================
else
    echo "error:libiconv install fail!"
    exit
fi
make && make install
#libmcrypt
cd /data/soft/libmcrypt-2.5.8
./configure --prefix=/usr/local/libmcrypt
make && make install
if [ $? = 0 ];then
    echo =======================================
    echo "success:libmcrypt install ok!"
    echo =======================================
else
    echo "error:libmcrypt install fail!"
    exit
fi
#php
echo =======================================
echo "start install php。。。。。。。。。。"
echo =======================================
cd /data/soft/php-5.4.31
if [ $InstallMysql = YES ];then
    ./configure --prefix=/usr/local/webserver/php --with-libxml-dir=/usr/include/libxml2 --with-config-file-path=/usr/local/webserver/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-pear --with-curl --with-gd --with-jpeg-dir=/usr/lib64/ --with-png-dir=/usr/lib/ --with-freetype-dir=/usr/lib64/ --with-iconv=/usr/local/libiconv/ --with-mcrypt=/usr/local/libmcrypt/ --with-zlib --with-zlib-dir=/usr/local/zlib --with-bz2 --with-xmlrpc --with-openssl --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --disable-debug --enable-zip --enable-sockets --enable-soap --enable-mbstring --enable-xml --enable-ftp --enable-exif --enable-wddx --enable-bcmath --enable-shmop --enable-dba --enable-sysvsem --enable-sysvshm --enable-sysvmsg
else
    ./configure --prefix=/usr/local/webserver/php --with-libxml-dir=/usr/include/libxml2 --with-config-file-path=/usr/local/webserver/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-pear --with-curl --with-gd --with-jpeg-dir=/usr/lib64/ --with-png-dir=/usr/lib/ --with-freetype-dir=/usr/lib64/ --with-iconv=/usr/local/libiconv/ --with-mcrypt=/usr/local/libmcrypt/ --with-zlib --with-zlib-dir=/usr/local/zlib --with-bz2 --with-xmlrpc --with-openssl --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --disable-debug --enable-zip --enable-sockets --enable-soap --enable-mbstring --enable-xml --enable-ftp --enable-exif --enable-wddx --enable-bcmath --enable-shmop --enable-dba --enable-sysvsem --enable-sysvshm --enable-sysvmsg
fi
make && make install
if [ $? = 0 ];then
    echo =======================================
    echo "success:php install ok!"
    echo =======================================
else
    echo "error:php install fail!"
    exit
fi
cp php.ini-development /usr/local/webserver/php/etc/php.ini
cd /usr/local/webserver/php/etc
cp php-fpm.conf.default php-fpm.conf

#php相关扩展安装
#mongoDB扩展
echo =======================================
echo "start install mongo。。。。。。。。。。"
echo =======================================
cd /data/soft/mongo-1.6.11
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make && make install
if [ $? = 0 ];then
    echo =======================================
    echo "success:mongo install ok!"
    echo =======================================
else
    echo "error:mongo install fail!"
    exit
fi
#phpredis扩展
echo =======================================
echo "start install phpredis。。。。。。。。。。"
echo =======================================
cd /data/soft/phpredis-2.2.4
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make && make install
if [ $? = 0 ];then
    echo =======================================
    echo "success:phpredis install ok!"
    echo =======================================
else
    echo "error:phpredis install fail!"
    exit
fi
#memcache
echo =======================================
echo "start install memcache。。。。。。。。。。"
echo =======================================
cd /data/soft/memcache-3.0.6
/usr/local/webserver/php/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/webserver/php/bin/php-config --with-zlib-dir
make && make install
if [ $? = 0 ];then
    echo =======================================
    echo "success:memcache install ok!"
    echo =======================================
else
    echo "error:memcache install fail!"
    exit
fi
#imagick扩展
#此扩展需依赖ImageMagick库,先安装ImageMagick。
echo =======================================
echo "start install ImageMagick。。。。。。。。。。"
echo =======================================
cd /data/soft/ImageMagick-6.9.5-8
./configure --enable-shared --without-perl
make && make install
if [ $? = 0 ];then
    echo =======================================
    echo "success:ImageMagick install ok!"
    echo =======================================
else
    echo "error:ImageMagick install fail!"
    exit
fi
#因为新版本的ImageMagick编译出来的目录为ImageMagick-6,必须为ImageMagick才行,否则会出现错误。
ln -s /usr/local/include/ImageMagick-6 /usr/local/include/ImageMagick
cd /data/soft/imagick-3.1.0RC1
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-imagick=/usr/local/include/ImageMagick
make && make install
if [ $? = 0 ];then
    echo =======================================
    echo "success:imagick install ok!"
    echo =======================================
else
    echo "error:imagick install fail!"
    exit
fi
echo "***************************************"
echo "lnmp all install complete。。。。。。。"
echo "***************************************"
#仔细查看如下注释的部分,手动设置相关内容。
#防火墙放开80和3306端口。
#vim /etc/sysconfig/iptables
:<<!
添加如下规则,并保存:
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
!
#service iptables restart

#设置nginx
#vim /etc/init.d/nginx
:<<!
#!/bin/bash
#
#chkconfig: - 85 15
#description:nginxisaWorldWideWebserver.Itisusedtoserve
# processname: nginx

nginx=/usr/local/webserver/nginx/nginx
nginx_config=/usr/local/webserver/nginx/nginx.conf
nginx_pid=/usr/local/webserver/nginx/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginx ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginx -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
     echo -n $"Stopping $prog: "
     killproc $nginx
     RETVAL=$?
     echo
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx $nginx_pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL
!
#:wq保存此文件。
:<<!
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig --level 345 nginx on
service nginx start
!
#查看nginx是否正常启动。
#浏览器访问http://服务器IP,查看是否显示正常。

#mysql设置
:<<!
cd /usr/local/mysql
cp -f support-files/my-huge.cnf /etc/my.cnf
chown -R mysql.mysql /wwwroot/database/mysql/data/
!
#vim /etc/my.cnf
:<<!
socket       = /tmp/mysqld.sock
key_buffer_size = 384M
max_allowed_packet = 16M
innodb_data_home_dir =  /data/mysql/data  去掉#
innodb_data_file_path = ibdata1:2000M;ibdata2:10M;autoextend 去掉#
innodb_log_group_home_dir = /data/mysql/data 去掉#
innodb_buffer_pool_size = 384M 去掉#
innodb_additional_mem_pool_size = 20M 去掉#
innodb_log_file_size = 100M 去掉#
innodb_log_buffer_size = 8M 去掉#
innodb_flush_log_at_trx_commit = 1 去掉#
innodb_lock_wait_timeout = 50 去掉#
!
#:wq保存文件。
:<<!
scripts/mysql_install_db --datadir=/wwwroot/database/mysql/data/ --user=mysql
cp support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --add mysql
chkconfig mysql off
chkconfig --level 345 mysql on
service mysql start
!
#查看mysql是否正常启动。
#设置mysql登录。
:<<!
默认mysql的root是没有密码的,可直接登录。如下:
/usr/local/mysql/bin/mysql -u root
mysql> use mysql;
添加root用户并所有服务器可登录:
mysql> grant all privileges on *.* to root@'%' identified by "meiliwang2016";
删除所有密码为空的的mysql用户。
mysql> delete from user where password='';
mysql> flush privileges;
mysql> quit
测试下:
[root@mysql mysql]#/usr/local/mysql/bin/mysql -u root -p
输入密码meiliwang2016是否可正常登录mysql。
!

#php设置
#cd /usr/local/webserver/php/etc
#vim php-fpm.conf
#pid = run/php-fpm.pid   //此行在后面做开机启动时需要,否则service php-fpm restart找不到pid文件。
#:wq保存文件。

#vim php.ini
#时区修改为date.timezone =PRC
#:wq保存文件。

#启动php-fpm
#/usr/local/webserver/php/sbin/php-fpm
#检查php-fpm是否正常启动。

#修改nginx配置解析php
#vim /usr/local/webserver/nginx/nginx.conf
:<<!
location / {
            root   html;
            index  index.html index.htm index.php;
        }#此处添加index.php
location ~ \.php$ {
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}#添加此段配置。
!
#:wq保存文件。
#重启nginx
#service nginx restart

#测试php页面是否正常
#cd /usr/local/webserver/nginx/html/
:<<!
vim test.php
<?php
    phpinfo();
?>
!
#:wq保存文件。
#浏览器访问http://服务器IP/test.php,查看是否显示正常。

#php-fpm开机启动
:<<!
cp /data/soft/php-5.4.31/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig --level 345 php-fpm on
!
#测试php-fpm能否正常重启。
#service php-fpm restart

#相关扩展设置
#查看是否在如下路径存在相应的so文件。
#/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20100525/
#文件最后添加扩展。
#vim /usr/local/webserver/php/etc/php.ini
:<<!
[memcache]
extension=memcache.so

[redis]
extension=redis.so

[imagick]
extension=imagick.so

[mongo]
extension=mongo.so
!
#:wq保存文件。
#重启php-fpm。
#service php-fpm restart
#通过访问http://服务器IP/test.php查看相应的扩展是否enable。
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:63165次
    • 积分:847
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:33篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论