shell脚本-lnmp一键部署

shell脚本-lnmp一键部署

创建文件lnmp.sh

vim lnmp.sh
#!/bin/bash
#描述:LNMP网站架构部署脚本
cat <<EOF
欢迎使用LNMP架构服务搭建
请将安装包放入/opt目录下
1.安装nginx服务
2.安装mysql
3.安装php
4.一键安装LNMP架构
EOF
read -p "请输入你的选择:" choice

function Nginx(){
echo    -e "\033[34m 2.安装Nginx \033[0m"
echo    "安装Nginx依赖包"
nginx_gz=nginx-1.9.5.tar.gz
nginx=nginx-1.9.5
cd /opt
yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel  pcre pcre-devel make automake   &>/dev/null
echo    "创建Nginx运行用户"
groupadd www
useradd -g www www -s /sbin/nologin
tar xf $nginx_gz
cd $nginx
./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --user=www --group=www &>/dev/null
if [ $? -eq 0 ];then
    echo    "Nginx预编译完成,开始安装"
else
    echo    "Nginx预编译失败,请检查相关依赖包是否安装"
    exit 4
fi
make && make install &>/dev/null
# 添加系统服务
cat > /etc/systemd/system/nginx.service <<EOF
[Unit]
Description=Nginx HTTP Server
After=network.target

[Service]
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
Restart=on-failure
Type=forking

[Install]
WantedBy=multi-user.target
EOF

chmod  755  /etc/systemd/system/nginx.service
/usr/local/nginx/sbin/nginx
netstat -anput | grep nginx &>/dev/null
if [ $? -eq 0 ];then
    echo    "Nginx启动成功"
else
    echo    "Nginx启动失败"
    exit 5
fi
}

function MySQL(){
echo    -e "\033[35m 3.安装MySQL \033[0m"
cd /opt
boost_bz2=boost_1_59_0.tar.bz2
mysql_gz=mysql-5.7.26.tar.gz
mysql=mysql-5.7.26
echo    "卸载原有MySQL及相关依赖"
yum -y remove mysql* mariadb* &>/dev/null
echo    "创建MySQL运行用户"
groupadd mysql
useradd -M -s /sbin/nologin -r -g mysql mysql
mkdir -p /data/mysql/{data,log}
chown -R mysql:mysql /data/mysql/
echo    "安装MySQL依赖包"
yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel bzip2 &>/dev/null
tar jxf $boost_bz2 -C /opt
tar xf $mysql_gz
cd $mysql
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -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=/opt/boost_1_59_0 &>/dev/null
if [ $? -eq 0 ];then
    echo    "MySQL预编译完成,正在安装"
else
    echo    "MySQL预编译失败,请检查依赖包是否全部安装"
    exit 6
fi
make && make install &>/dev/null
chown -R mysql:mysql /usr/local/mysql/
cat >>/etc/my.cnf<< eof
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql/data
port=3306
socket=/usr/local/mysql/mysql.sock
symbolic-links=0
character-set-server=utf8
log-error=/data/mysql/log/mysqld.log
pid-file=/usr/local/mysql/mysqld.pid
eof
cp support-files/mysql.server /etc/init.d/mysql.server
chmod +x /etc/init.d/mysql.server
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
/etc/init.d/mysql.server start
netstat -anplt | grep mysql
if [ $? -eq 0 ];then
    echo    "MySQL启动成功"
else
    echo    "MySQL启动失败"
    exit 7
fi
ln -s /usr/local/mysql/bin/* /usr/local/bin/
}

function Php(){
echo    -e "\033[36m 4.安装PHP \033[0m"
libmcrypt_gz=libmcrypt-2.5.8.tar.gz
libmcrypt=libmcrypt-2.5.8
php_gz=php-5.6.40.tar.gz
php=php-5.6.40
cd /opt
echo    "安装PHP依赖包"
yum -y install gcc autoconf  freetype gd libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel freetype-devel libjpeg-devel bzip2 bzip2-devel openssl openssl-devel
tar xf $libmcrypt_gz
cd $libmcrypt
./configure --prefix=/usr/local/libmcrypt && make && make install &>/dev/null
cd /opt
tar xf $php_gz
cd $php
./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --enable-mbstring --with-curl --with-gd --enable-fpm --with-config-file-path --with-openssl --enable-fpm --enable-sockets  --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --with-mhash  --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/usr/local/php5.6/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  &>/dev/null
make && make install &>/dev/null
cp php.ini-production /usr/local/php5.6/php.ini
cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
/etc/init.d/php-fpm start
netstat -anput | grep php-fpm
if [ $? -eq 0 ];then
    echo    "PHP启动成功"
else
    echo    "PHP启动失败"
    exit 8
fi
}

function conf(){ 
echo    -e "\033[33m 5.LNMP架构配置 \033[0m"
echo    "修改Nginx配置文件"
rm -rf /usr/local/nginx/conf/nginx.conf
cat >> /usr/local/nginx/conf/nginx.conf << eof
user  www;
worker_processes  2;
error_log  logs/error.log;
pid        logs/nginx.pid;
events {
        use epoll;
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    #gzip  on;
    ferver {
m       listen       80;
        server_name  localhost;
        charset utf-8;
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        location ~ \.php$ {
            root  html;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
eof
cat >> /usr/local/nginx/html/index.php << eof
<?php
        phpinfo();
?>
eof
cat >> /usr/local/nginx/html/mysql.php << eof
<?php
$link=mysql_connect('127.0.0.1','lnmp','123456');
if ($link)echo "connection success......";
mysql_close();
?>
eof
echo    "在MySQL中创建测试用户"
mysql -uroot -e "grant all on *.* to 'lnmp'@'%' identified by '123456';"
mysql -uroot -e "flush privileges;"
echo    "重启服务(NML)"
/usr/local/nginx/sbin/nginx -s reload
/etc/init.d/mysql.server restart
/etc/init.d/php-fpm restart
cd /opt
}

#########################调用函数#####################
case $choice in
        1)
                Nginx
                ;;
        2)
                MySQL
                ;;
        3)
                Php
                ;;
        4)
                Nginx
                sleep 2
                MySQL
                sleep 2
                Php
                sleep 2
                conf
                ;;
        *)
                echo "请输入正确选项"
esac

启动lnmp.sh

bash lnmp.sh

查看网络状态

[root@localhost init.d]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      1394/php-fpm: maste 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      924/nginx: master p 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      918/sshd            
tcp6       0      0 :::3306                 :::*                    LISTEN      1356/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      918/sshd            
udp        0      0 0.0.0.0:68              0.0.0.0:*                           718/dhclient        
udp        0      0 127.0.0.1:323           0.0.0.0:*                           661/chronyd         
udp6       0      0 ::1:323                 :::*                                661/chronyd   

测试一下

posted @ 2023-07-16 16:10  FouroFour  阅读(232)  评论(0编辑  收藏  举报