#!/bin/bash
# ubuntu20 shell 脚本自动编译web安装网站环境 nginx199 php7/8 mariadb1011 以 systemd 方式设置开机启动
# mysql 重置 root 密码,创建 或删除 mysql 子库和子用户
downdir="/datadisk/eisc/download" ; sudo mkdir -p $downdir ; sudo chmod 777 -R $downdir
installdir="/datadisk/eisc/server" ; sudo mkdir -p $installdir
# 安装路径不能将所有目录都为 777 , 如: mysql 的 /etc/my.cnf 不能为 777 否则mysql 无法启动
www="/datadisk/eisc/www"
wwwconf="/datadisk/eisc/www/wwwconf"
# 设置web 网站路径 和 nginx conf 子站点路径
echo "
欢迎使用自动安装 网站web 环境shell 脚本, nginx199 php-fpm mariadb10.11
当前需要更新 apt upgrade , 如果遇到弹窗选择包,请回车跳过。不要按 Esc 退出. 建议选择 ubuntu20.04
安装完成重启服务器后,启动web服务命令: sudo nginx199 ; sudo php-fpm743 ; sudo /etc/init.d/mariadb1011 restart
数据库默认登陆: mysql -u root -peisc.cn
server 源码下载路径:$downdir
server 安装路径:$installdir
www 网站路径:$www
www nginx conf 子站点路径:$wwwconf
C 2022.12.04 小绿叶技术博客 eisc.cn
"
sleep 3;
if [ ! -e /etc/init.d/mariadb1011 ]
then
sudo apt update ; sudo apt upgrade -y
fi
down_guanfang_url(){
downNginxUrl="http://nginx.org/download/nginx-1.9.9.tar.gz"
downPHPurl="https://www.php.net/distributions"
downMariadbUrl="https://archive.mariadb.org//mariadb-10.11.0/bintar-linux-systemd-x86_64/mariadb-10.11.0-linux-systemd-x86_64.tar.gz"
# 手动下载数据库:https://mariadb.org/download/
}
down_eisc_url(){
url="http://work.eisc.cn/ruanjian"
downNginxUrl="$url/ubuntu/server/nginx-1.9.9.tar.gz"
downPHPurl="$url/ubuntu/server/php"
downMariadbUrl="$url/ubuntu/server/mariadb-10.11.0-linux-systemd-x86_64.tar.gz"
}
down_select(){
read -p "选择下载安装包地址: 1 官方下载 2 小绿叶技术博客下载. 请输入: " selectURL
case $selectURL in
"1") echo "当前选择官方下载..." ; down_guanfang_url ;;
"2") echo "当前选择小绿叶技术博客下载..." ; down_eisc_url ;;
*) echo "输入错误,请重新执行脚本! " ; exit ;;
esac
echo $downNginxUrl
echo $downPHPurl
echo $downMariadbUrl
}
down_select
nginx_install(){
cd $downdir
if [ ! -e /bin/nginx199 ]
then
echo "开始安装nginx 1.9.9"
sudo apt install -y build-essential libtool \
libpcre3 libpcre3-dev \
zlib1g-dev openssl libgd-dev \
geoip-database libgeoip-dev
# 安装gcc c++ ; pcre ; zlib ssl GD ; GeoIP 依赖库
if [ ! -e $downdir/nginx-1.9.9.tar.gz ]
then
wget $downNginxUrl
fi
if [ ! -e $downdir/nginx-1.9.9/configure ]
then
tar -xzvf nginx-1.9.9.tar.gz
fi
cd nginx-1.9.9
sudo mkdir -p $installdir/nginx/nginx1.9.9 ; sudo chmod 777 -R mkdir -p $installdir/nginx/nginx1.9.9
./configure --prefix=$installdir/nginx/nginx1.9.9
sed -i "s/-Werror/ /g" objs/Makefile
NR=`cat -n ./src/os/unix/ngx_user.c | grep cd.current_salt | awk -F" " '{print $1}'`
sed -i "$NR d" ./src/os/unix/ngx_user.c
sudo make && sudo make install
sudo ln -s $installdir/nginx/nginx1.9.9/sbin/nginx /bin/nginx199
sudo killall nginx199 ; sudo nginx199
# 重新启动nginx
sudo nginx199 ; nginx199 -V
else
echo "[ok]nginx 1.9.9 已经安装"
fi
}
php_aptupdate(){
sudo apt install -y gcc make openssl curl libssl-dev libxml2-dev libzip-dev libcurl4-openssl-dev \
libpng-dev libjpeg-dev libwebp-dev libonig-dev libsqlite3-dev libsodium-dev libargon2-dev \
libkrb5-dev libbz2-dev libxslt-dev php-mbstring \
build-essential libtool libpcre3 libpcre3-dev zlib1g-dev libgd-dev geoip-database libgeoip-dev
# 安装包: No package 'krb5-gssapi' found ; Please reinstall the BZip2 distribution ; No package 'libxslt' found
# 报错解决参考:http://t.zoukankan.com/architectforest-p-15714248.html
sudo groupadd www ; sudo useradd -g www www -s /bin/false
# sudo groupadd www-data ; sudo useradd -g www-data www-data -s /bin/false
# 重建用户,并且不允许用户登陆系统
echo "
linux nginx + php-fpm 安装参考:
https://www.php.net/manual/zh/install.unix.nginx.php
"
}
php_release721(){
phpreleaseMl="php-7.2.1"
phpreleaseInstall="php721"
phpreleaseBin="php-fpm721"
downPHPurl="$downPHPurl/$phpreleaseMl.tar.gz"
echo "php 下载地址重新定义为:$downPHPurl"
}
php_release7433(){
phpreleaseMl="php-7.4.33"
phpreleaseInstall="php743"
phpreleaseBin="php-fpm743"
downPHPurl="$downPHPurl/$phpreleaseMl.tar.gz"
echo "php 下载地址重新定义为:$downPHPurl"
}
php_release811(){
phpreleaseMl="php-8.1.13"
phpreleaseInstall="php811"
phpreleaseBin="php-fpm811"
downPHPurl="$downPHPurl/$phpreleaseMl.tar.gz"
echo "php 下载地址重新定义为:$downPHPurl"
}
php_install(){
cd $downdir
if [ ! -e /bin/$phpreleaseBin ]
then
echo "开始安装 $phpreleaseInstall "
php_aptupdate
if [ ! -e $downdir/$phpreleaseMl.tar.gz ]
then
wget $downPHPurl
fi
if [ ! -e $downdir/$phpreleasedown/configure ]
then
tar -xzvf $phpreleaseMl.tar.gz
fi
cd $phpreleaseMl
sudo mkdir -p $installdir/php/$phpreleaseInstall ; sudo chmod 777 -R $installdir/php/$phpreleaseInstall
./configure --prefix=$installdir/php/$phpreleaseInstall --with-config-file-path=$installdir/php/$phpreleaseInstall/etc \
--enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
--with-iconv-dir --with-freetype --with-jpeg --with-zlib \
--enable-zip \
--with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl \
--enable-ftp --enable-gd --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --with-gettext --disable-fileinfo --enable-opcache --with-webp
# --with-openssl
# 第一行 指定安装路径,和 php.ini 路径为 $installdir/php/php72/etc
# 第二行 开启php 的 php-fpm web功能。启动用户名和组 和 重要 开启php 的mysql connect() 函数 功能。
# 第三行 压缩解码工具 否则 php WordPress 无法无法正常启动 报错: is not a valid libtool object
# 后面行,暂时没有研究. --with-openssl ubuntu22 编译不过
sudo make ; sudo make install
#----------------------- 说明 -------------------#
# 安装说明:https://www.php.net/manual/zh/install.php
#--with-fpm-user - 设置 FPM 运行的用户身份(默认 - nobody)。
#--with-fpm-group - 设置 FPM 运行时的用户组(默认 - nobody)。
#--with-fpm-systemd - 启用 systemd 集成 (默认 - no)。
#--with-fpm-acl - 使用 POSIX 访问控制列表 (默认 - no)。
#--with-fpm-apparmor - 激活 AppArmor 集成 (默认 - no)。
#--with-fpm-selinux - 激活 SELinux 集成(默认 - no)。
#---------------------- php config ---------------#
sudo cp $downdir/$phpreleaseMl/sapi/fpm/php-fpm.service.in /etc/systemd/system/php-fpm.service
sudo cp $downdir/$phpreleaseMl/php.ini-development $installdir/php/$phpreleaseInstall/etc/php.ini
sudo cp $installdir/php/$phpreleaseInstall/etc/php-fpm.conf.default $installdir/php/$phpreleaseInstall/etc/php-fpm.conf
sudo cp $installdir/php/$phpreleaseInstall/etc/php-fpm.d/www.conf.default $installdir/php/$phpreleaseInstall/etc/php-fpm.d/www.conf
#---------- 更改用户
NR=`cat -n $installdir/php/$phpreleaseInstall/etc/php-fpm.d/www.conf | grep -w user | grep nobody | awk -F" " '{print $1}'`
sudo sed -i "$NR s/nobody/www/g" $installdir/php/$phpreleaseInstall/etc/php-fpm.d/www.conf
NR=`cat -n $installdir/php/$phpreleaseInstall/etc/php-fpm.d/www.conf | grep -w group | grep -v listen | grep nobody | awk -F" " '{print $1}'`
sudo sed -i "$NR s/nobody/www/g" $installdir/php/$phpreleaseInstall/etc/php-fpm.d/www.conf
NR=`cat -n /etc/systemd/system/php-fpm.service | grep ProtectSystem | awk -F" " '{print $1}'`
sudo sed -i "$NR s/full/false/g" /etc/systemd/system/php-fpm.service
#---------- 解决 No input file specified
NR=`cat -n $installdir/php/$phpreleaseInstall/etc/php.ini | grep fix_pathinfo | grep -v provides| awk -F" " '{print $1}'`
sudo sed -i "$NR a cgi.fix_pathinfo=1" $installdir/php/$phpreleaseInstall/etc/php.ini
sudo sed -i "$NR d" $installdir/php/$phpreleaseInstall/etc/php.ini
NR=`cat -n $installdir/php/$phpreleaseInstall/etc/php.ini | grep force_redirect | grep 1| awk -F" " '{print $1}'`
sudo sed -i "$NR a cgi.force_redirect=0" $installdir/php/$phpreleaseInstall/etc/php.ini
sudo sed -i "$NR d" $installdir/php/$phpreleaseInstall/etc/php.ini
#-----------
NR=`cat -n $installdir/php/$phpreleaseInstall/etc/php-fpm.d/www.conf | grep "127.0.0.1:9000" | awk -F" " '{print $1}'`
# sudo sed -i "$NR a listen = /var/run/$phpreleaseBin.sock" $installdir/php/$phpreleaseInstall/etc/php-fpm.d/www.conf
sudo sed -i "$NR a listen = 127.0.0.1:9000" $installdir/php/$phpreleaseInstall/etc/php-fpm.d/www.conf
sudo sed -i "$NR d" $installdir/php/$phpreleaseInstall/etc/php-fpm.d/www.conf
sudo cp $installdir/php/$phpreleaseInstall/sbin/php-fpm /bin/$phpreleaseBin
sudo sudo chmod +x /bin/$phpreleaseBin ; sudo $phpreleaseBin
# 重载配置,并且启动php
echo "php 安装完成,启动与停止php : sudo killall $phpreleaseBin ; sudo sudo chmod +x /bin/$phpreleaseBin ; sudo $phpreleaseBin "
#sudo sed -i "778 s/;//g" /etc/php/7.4/fpm/php.ini
#sudo sed -i "778 s/1/0/g" /etc/php/7.4/fpm/php.ini
#sudo sed -i "798 s/;//g" /etc/php/7.4/fpm/php.ini
# 解决 No input file specified
else
echo "php 已经安装"
fi
}
php_select(){
echo "请输入您要安装php 的版本: 提供版本: 1. php7.2.1 2. php7.4.33 3. php8.1.13 任意按键取消安装php 并且往下执行... "
read -p "请输入版本编号:" selectxh
case $selectxh in
"1") echo "当前选择php版本 $phpreleaseMl 启动文件为: /bin/$phpreleaseBin " ; php_release721 ; php_install ;;
"2") echo "当前选择php版本 $phpreleaseMl 启动文件为: /bin/$phpreleaseBin " ; php_release7433 ; php_install ;;
"3") echo "当前选择php版本 $phpreleaseMl 启动文件为: /bin/$phpreleaseBin " ; php_release811 ; php_install ;;
*) echo "[ok]退出安装 php "
esac
}
nginx_web_config(){
sudo mkdir -p $wwwconf $www ; sudo chmod 777 -R $www
setconf=`cat $installdir/nginx/nginx1.9.9/conf/nginx.conf | grep "$wwwconf" `
if [ ${#setconf} -gt 10 ]
then
echo "[ok]nginx 已经配置, 子站点目录: $wwwconf/www.conf"
else
NR=`cat -n $installdir/nginx/nginx1.9.9/conf/nginx.conf | grep http | grep "{" | awk -F" " '{print $1}'`
sudo sed -i "$NR a include $wwwconf/*.conf; " $installdir/nginx/nginx1.9.9/conf/nginx.conf
echo "<?php phpinfo(); ?>" > $www/www/p.php
fi
sudo nginx199 -s stop ; sudo nginx199
# 修改php 后重新启动nginx
}
create_www_conf(){
echo "案例: eisc.cn 114.114.114.114 62000"
read -p " 请输入三个参数:" peizhi
buff=( $peizhi )
yuming=${buff[0]}
# 前端域名,增加到nginx 消息头部
ip=${buff[1]}
# 后端服务器ip
portmin=${buff[2]}
portmin=$[portmin-1]
# 设置最小端口,后面网站按照顺序自动加1
cd $wwwconf; sudo chmod 777 -R $wwwconf $wwwconf/*
if [ ! -e eiscwwwconf.tar.gz ]
then
sudo tar -czvf eiscwwwconf.tar.gz *
sudo rm -rf `ls | grep -v eiscwwwconf.tar.gz`
fi
sudo rm -rf `ls | grep -v eiscwwwconf.tar.gz`
file=(`ls $www | grep -vE "ssl|wwwconf|wwwroot|*.sh"`)
for i in ${file[*]}
do
((a++))
b=$[portmin+a]
portlist[$c]=$b
((c++))
# 打印一次变量,接收一个元素存入数组: portlist
if [ ! -e $www/$i/cgi-bin ]
then
sudo mkdir $www/$i/cgi-bin
fi
echo " $i 创建web网站 的端口: $b 前端域名 + $i.$yuming + 后端ip port + $ip:$b + c++ cgi 解析目录: $i/cgi-bin"
echo " $i 创建web网站 的端口: $b 前端域名 + $i.$yuming + 后端ip port + $ip:$b + c++ cgi 解析目录: $i/cgi-bin" >> log_www_conf.txt
#--------- 生成前端转发 子站点文件 -----------#
sudo mkdir -p $wwwconf/nginxzhuanfa ; sudo chmod 777 $wwwconf/nginxzhuanfa
echo "
server{
listen 80;
server_name $i.$yuming;
location / {
proxy_pass http://$ip:$b;
}
}
" > $wwwconf/nginxzhuanfa/$i-$b.conf
if [ "$i" = "www" ]
then
echo "
server{
listen 80;
server_name $i.$yuming $yuming;
location / {
proxy_pass http://$ip:$b;
}
}
" > $wwwconf/nginxzhuanfa/$i-$b.conf
fi
#---------- 后端转发子站点 ----------#
echo "
server {
listen $b;
root $www/$i;
index index.php index.html index.htm index;
add_header 'yuming is ' '$i.$yuming';
add_header 'ipport is ' '$ip:$b';
location ~ \.php {
# fastcgi_pass unix:/var/run/php-fpm811.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
location /cgi-bin {
fastcgi_pass unix:/var/run/fcgiwrap.socket;
include fastcgi.conf;
}
}
" > $wwwconf/$b.$i.conf
done
echo "[ok]创建网站日志存放在: $wwwconf/log_www_conf.txt"
echo "[ok]前端转发 nginx 子文件目录: $wwwconf/nginxzhuanfa"
sudo nginx199 -s reload
}
cgi_install_config(){
cd $downdir
dir="$installdir/aspnet" ; sudo mkdir -p $dir; sudo chmod 777 -R $dir ;
mkdir -p $dir/conf $dir/www/www
sudo apt install -y build-essential libfcgi-dev autoconf libtool automake libtool spawn-fcgi fcgiwrap
cgiport=10000
psnet=`sudo netstat -ntlp | grep -w $cgiport `
if [ ${#psnet} -lt 1 ]
then
spawn-fcgi -f /usr/sbin/fcgiwrap -a 127.0.0.1 -p $cgiport -F 32 -P /tmp/fastcgi-c.pid -u nobody -g nobody
# 启动
fi
echo "[ok]访问: 127.0.0.1/p.php 查看 效果, 子站点目录:$wwwconf/www.conf "
#--- 安装cgicc web模块 ---#
if [ ! -e cgicc-3.2.19.tar.gz ]
then
wget http://ftp.gnu.org/gnu/cgicc/cgicc-3.2.19.tar.gz
fi
if [ ! -e $installdir/cgicc/bin/cgicc-config ]
then
sudo tar -xzvf cgicc-3.2.19.tar.gz ; cd cgicc-3.2.19
sudo mkdir -p $installdir/cgicc ; sudo chmod 777 $installdir/cgicc
./configure --prefix=$installdir/cgicc
sudo make ; sudo make install
sudo cp -r $installdir/cgicc/lib/* /usr/lib64/
fi
# g++ -o get get.cpp -lcgicc
# 使用 cgicc 编译c++ 方法: g++ 编译c++ 程序; gcc 编译c语言; -o 生成可执行文件 get 被编译的文件:get.cpp 指定库 cgicc
}
nginx_config_if(){
echo "
#----------- 配置子站点nginx ------------#
请输入 前端转发服务器域名,后端服务器公网ip 和 第一个网站端口。有多个网站,端口自动计数加1 。
如果没有域名 和前端服务器,随意输入三个参数,但端口一定为 65536 以内。脚本可以重复执行,重新配置
"
read -p "是否进行配置: y 需要配置, 任意按键并回车跳过, 请输入: " ifa
if [ "$ifa" = "y" ]
then
echo "正在进行配置 子站点..."
create_www_conf ; cgi_install_config
# 配置子站点
else
echo "[ok]已经跳过nginx配置子站点!"
fi
}
mariadb_install(){
cd $downdir
if [ ! -e /etc/init.d/mariadb1011 ]
then
echo "开始安装 mariadb-10.11"
sudo apt install -y libncurses5
sudo groupadd mysql ; sudo useradd -g mysql mysql -s /bin/false
# 创建mysql用户组和用户,不允许登陆系统
if [ ! -e $downdir/mariadb-10.11.0-linux-systemd-x86_64.tar.gz ]
then
wget $downMariadbUrl
fi
if [ ! -e $installdir/mysql/mariadb1011/README.md ]
then
sudo mkdir -p $installdir/mysql ; sudo chmod 777 -R $installdir/mysql
sudo tar -xzvf mariadb-10.11.0-linux-systemd-x86_64.tar.gz -C $installdir/mysql/
sudo mv $installdir/mysql/mariadb-10.11.0-linux-systemd-x86_64 $installdir/mysql/mariadb1011
fi
# sudo chown -R mysql mariadb-10.11 ; cd mariadb-10.11
# 更改文件属性为mysql组,mysql 用户
cd $installdir/mysql/mariadb1011
sudo mkdir -p $installdir/sqldata ; sudo chmod 777 -R $installdir/sqldata
sudo chown -R mysql:mysql $installdir/sqldata ; sudo chmod 777 -R $installdir/sqldata
sudo $installdir/mysql/mariadb1011/scripts/mysql_install_db --user=mysql --basedir=$installdir/mysql/mariadb1011 --datadir=$installdir/sqldata
# 初始化mariadb
sudo cp $installdir/mysql/mariadb1011/support-files/wsrep.cnf /etc/my.cnf
sudo chmod 777 /etc/my.cnf
echo "
[mysqld]
port = 3306
socket = /tmp/mysql.sock
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
#添加以下配置
basedir=$installdir/mysql/mariadb1011
datadir=$installdir/sqldata
" > /etc/my.cnf
sudo chmod 644 /etc/my.cnf
# my.cnf 权限为 777 不安全,所以mysql无法启动。
sudo cp $installdir/mysql/mariadb1011/support-files/mysql.server /etc/init.d/mysql
sudo cp $installdir/mysql/mariadb1011/bin/mysqld /etc/init.d/mysqld
sudo ln -s $installdir/mysql/mariadb1011/support-files/mysql.server /etc/init.d/mariadb1011
sudo chmod +x /etc/init.d/mysqld
sudo chmod 777 /etc/profile
echo "
#MARIADB
export MARIADB_HOME=$installdir/mysql/mariadb1011
export PATH=$PATH:${MARIADB_HOME}/bin
" >> /etc/profile
# 设置环境变量,等于:添加执行文件的路径。 先定义 MARIADB_HOME 变量的路径,再引用
sudo chmod 644 /etc/profile
source /etc/profile ; bash
#---------- 重置 mysql root 密码也是该流程 ---------#
sudo killall mariadbd mysqld_safe
runmysql=`sudo netstat -nltp | grep 3306 | grep -w tcp | grep mariadb`
if [ ${#runmysql} -lt 1 ]
then
sudo /etc/init.d/mariadb1011 start
fi
# 重新启动mysql 服务, mysql 启动时候才能进行 mysqladmin 命令 重置密码
sleep 1;
sudo $installdir/mysql/mariadb1011/bin/mysqladmin -u root password eisc.cn
echo "数据库root 密码为: eisc.cn 使用命令更改密码: "
echo "sudo $installdir/mysql/mariadb1011/bin/mysqladmin -u root password eisc.cn"
sudo mkdir -p $installdir/back/sql/ ; sudo chmod 777 $installdir/back/sql/
else
echo "[ok] mariadb1011 已经安装"
fi
}
mysql_data(){
#------ 修改mysql data 数据目录 -----#
sudo chmod 777 /eisc/
mkdir /eisc/data/
sudo chown -R mysql:mysql /eisc/data/
sudo chmod 777 /usr/local/mariadb-10.9.2/data/
sudo cp -r /usr/local/mariadb-10.9.2/data/* /eisc/data/
sudo /usr/local/mariadb-10.9.2/support-files/mysql.server stop
sudo /usr/local/mariadb-10.9.2/support-files/mysql.server start
}
mysql_database(){
#!/bin/bash
# shell 自动创建数据库 用户和密码
sudo apt install libncurses*
# 解决mysql 命令报错,缺少库:Couldn't find any package by glob 'libncurses.so.5'
sudo apt install mysql-client -y
# 安装连接数据库工具
sudo mkdir /var/run/mysqld/
sudo ln -s /tmp/mysql.sock /var/run/mysqld/mysqld.sock
# 解决 mysql 报错,无法进入mysql 。 mariadb 的启动sock 不一样
runmysql=`sudo netstat -nltp | grep 3306 | grep -w tcp | grep mariadb`
if [ ${#runmysql} -lt 1 ]
then
sudo /etc/init.d/mariadb1011 start
fi
sudo $installdir/mysql/mariadb1011/bin/mysqladmin -u root password eisc.cn
# 启动数据库,重置数据库 root 用户密码为: eisc.cn
echo "#---------------- 数据库管理工具 ------------#
参考输入案例: create eisc.cn www www 000000 localhost
1 创建 or 删除,输入: create 或 drop
2 数据库 root 用户密码
3 子数据库名
4 子库用户名
5 子库密码
6 开放数据库方式:本地/远程 输入: localhost 或 %
用空格隔开,必须按照顺序输入6个参数!"
read -p "请输入:" in_buff
buff=( abcd $in_buff);
echo "你输入的信息为: ${buff[*]}";
case ${buff[1]} in
"create")
# mysql -uroot -p${buff[2]} -e "create database ${buff[3]} character set utf8 collate utf8_bin;"
mysql -uroot -p${buff[2]} -e "create database ${buff[3]} character set utf8;"
mysql -uroot -p${buff[2]} -e "grant all on ${buff[4]}.* to '${buff[4]}'@'${buff[6]}' identified by '${buff[5]}'"
mysql -uroot -p${buff[2]} -e "show databases;SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS ListUsername FROM mysql.user where User='${buff[4]}';"
;;
"drop")
mysql -uroot -p${buff[2]} -e "drop database ${buff[3]}"
mysql -uroot -p${buff[2]} -e "drop user '${buff[4]}'@'${buff[6]}'"
mysql -uroot -p${buff[2]} -e "show databases;SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS ListUsername FROM mysql.user where User='${buff[4]}';"
;;
esac
}
mysql_contor(){
runmysql=`sudo netstat -nltp | grep 3306 | grep -w tcp | grep mariadb`
if [ ${#runmysql} -lt 1 ]
then
sudo /etc/init.d/mariadb1011 start
fi
# 启动数据库
echo "#---- 数据库 root 密码始终被重置为 eisc.cn ----#
如果需要手动重置为自己想要的密码,命令行手动执行命令:
sudo $installdir/mysql/mariadb1011/bin/mysqladmin -u root password eisc.cn
将 eisc.cn 改为自己的密码
"
read -p "是否进行数据库管理工作,创建数据库和用户,输入: y/n ? " ins
if [ "$ins" = "y" ]
then
echo "开始管理数据库 ..."
mysql_database
else
echo "[ok]退出数据库管理!"
fi
}
kaiji_web_service(){
# ubuntu 以 systemd 服务方式,自动启动服务软件
systemdDir="/usr/lib/systemd/system"
webname="eisc_web_start.service"
cd $systemdDir ; sudo touch $webname ; sudo chmod 777 $webname
#----- 写入 start.service 服务 -----#
echo "
[Unit]
Description=start web
[Service]
Type=forking
ExecStart=$installdir/shell/start.sh
ExecReload=/bin/kill -SIGHUP \$MAINPID
ExecStop=/bin/kill -SIGINT \$MAINPID
[Install]
WantedBy=multi-user.target
" > $webname
sudo apt install dos2unix -y
sudo dos2unix $webname
# 转为 unix 格式,否则可能出现字符乱码
sudo chmod 644 $webname
sudo systemctl daemon-reload
sudo systemctl disable $webname
sudo systemctl enable $webname
cd
}
kaiji_start_web_sh(){
read -p "是否继续进行配置 systemd 开机启动? y/n: " kaijisz
if [ "$kaijisz" = "y" ]
then
kaiji_web_service
# 启动这个函数
sudo mkdir -p $installdir/shell ; sudo chmod 777 -R $installdir/shell
sudo chmod +x $installdir/shell/start.sh $installdir/nginx/nginx1.9.9/sbin/nginx $installdir/php/php743/sbin/php-fpm
sudo chown root:root $installdir/shell/start.sh ; sudo chmod 777 $installdir/shell/start.sh
# 将脚本授权给 root 用户
echo "#!/bin/bash
# bash 解释器 一定要在第一行 开头写,否则下面脚本不执行,开机启动不成功。 由于是 service服务,因此此脚本是 root 用户执行
$installdir/nginx/nginx1.9.9/sbin/nginx &
$installdir/php/php743/sbin/php-fpm &
/etc/init.d/mariadb1011 start &
/bin/spawn-fcgi -f /usr/sbin/fcgiwrap -a 127.0.0.1 -p 10000 -F 32 -P /tmp/fastcgi-c.pid &
touch \$HOME/log_start_web.txt ; chmod 777 \$HOME/log_start_web.txt
echo \"服务已经自动自动,时间 \`date \`\" > \$HOME/log_start_web.txt
" > $installdir/shell/start.sh
echo "#----------------- 查看写入的开机启动脚本 -------------------#"
cat $installdir/shell/start.sh
else
echo "[ok]目前不配置systemd "
fi
}
main(){
php_select
nginx_install
nginx_web_config
nginx_config_if
# 包含创建 www 子站点 和 c++ cgi
mariadb_install
# 数据库初始化 ,会中断退出脚本,所以放到最后安装
mysql_contor
kaiji_start_web_sh
}
main
# ubuntu20/21/22 一键部署 web 环境: wget eisc.cn/file/ubuntu/shell/server/web.sh ; chmod +x web.sh ; ./web.sh