首页 > 系统相关 >shell脚本-lnmp一键部署

shell脚本-lnmp一键部署

时间:2023-07-16 16:22:42浏览次数:43  
标签:shell -- 0.0 一键 lnmp echo usr mysql php

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  /usr/lib/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   

测试一下

标签:shell,--,0.0,一键,lnmp,echo,usr,mysql,php
From: https://www.cnblogs.com/xuxuxuxuxu/p/17558015.html

相关文章

  • 利用Python的JSON以及Base64模块实现二进制文件传输及反向Shell
    控制端代码:importsocketimportoptparseimportsysimportthreadingimportjsonimportbase64classMyTCPServer:def__init__(self)->None:self.port=self.get_param()try:self.s_socket=socket.socket(socket.AF_INET,......
  • linux下使用VI/VIM编辑器完成文本编辑工作(finalshell远程操控linux)
    VI是命令行下对文本进行编辑的最佳选择VIM是VI的加强版,兼容VI的所有指令,不仅能编辑文本,还具有shell程序编辑的功能,可以使用不同颜色的文体来辨别语法的正确性1.vi/vim编辑器的工作模式命令模式:此模式下敲出来的内容理解为命令,以命令驱动功能,此模型下,不能自由编辑文本输入模式......
  • shell脚本基础
    1.shell基础1.1简介shell介绍shell应用场景:系统管理,文件操作等1.2解释器解释器是一种命令解释器,主要作用是对命令进行运行和解释,将需要执行的操作传递给操作系统内核并执行指定解释器#!/usr/bin/python3不指定,默认就是这个#!/bin/bash#这是一个shell脚本#byliech......
  • shell位置变量
    1.变量  env查看当前用户系统预设环境变量  set查看系统预设全部变量  read可以从标准输入中读取变量值:  read-p"Pleaseinputanumber:"变量名  unset变量名删除变量  export变量名=值直接定义环境变量2.位置参数:特殊的shell变量,用于传递参数  ......
  • 服务器/虚拟机 关机或重启之后报:Entering emergency mode. Exit the shell to continu
    具体现象多次重启之后还是不行,提示让进入紧急模式或退出排查原因根据提示输入journalctl,查看系统的日志信息可以看到有一些明显的报错信息可以看到这些报错都与xfs文件系统相关XFS(dm-0)这里给出了具体信息mount:mount/dev/mapper/centos-rooton/susrootfailed......
  • Linux shell 获取脚本文件后面的参数的方法
    原文:https://blog.csdn.net/tcjy1000/article/details/124558257环境Linux主机:Ubuntu20.04.4LTS获取shell脚本参数Linuxshell可以通过$0$1$2等获取shell脚本本身及后面的参数获取路径错误示例:#!/bin/bashecho"--createLinuxrootfsbegin--"echo'$1'rootfs_path......
  • 一键下载部署Office
    公司电脑重装了下系统,原来Office不能用了,用正版的话花钱太多,WPS很多功能还不能用,就在网上搜索有没有免费的。一查还真有,安装步骤已经安装完成,现在推荐给各位。下载地址:https://otp.landian.vip/zh-cn/download.htmlfficeToolPlus基于Office部署工具(ODT)打造,可以很轻松......
  • Xshell连接Fedora虚拟机
    先安装Xshell7,然后新建会话,远程连接虚拟机内的Fedora。然后会被拒绝:Couldnotconnecttoxxx:Connectionfailed.照着这篇文章的做法,$sudovim/etc/ssh/sshd_config找到#Authentication:一项,改成LoginGraceTime120PermitRootLoginyesStrictModesyes然后......
  • shell多线程/实例
    1.Shell实现多进程  使用&和wait配合实现shell多进程并行  参考连接:https://blog.csdn.net/yuefei169/article/details/83340480  (1)改串行执行为并行执行方式:将前台执行命令放在后台执行(串行命令后加&符号).(生产少使用该方式)  (2)使用元祖模拟队列来控制进程数......
  • LNMP 和 LAMP 对比 (仅供参考)
    Nginx性能稳定、功能丰富、运维简单、处理静态文件速度快且消耗系统资源极少。Apache是LAMP架构最核心的WebServer,开源、稳定、模块丰富是Apache的优势。但Apache的缺点是有些臃肿,内存和CPU开销大,性能上有损耗,不如一些轻量级的Web服务器 至于安装哪个好呢? 要省......