实验环境
1.shell 脚本:linux centos 7 系统
2.Python shell 脚本:window 系统
3.其他:python selenium 模块,谷歌浏览器, 谷歌浏览器驱动。
4.discuz 链接:/s/1vOwN_f56wJlIzauGrFoR1Q
提取码:mijf
实验步骤
1.上传discuz包到Linux系统/opt目录下
方法一:挂载 方法二:通过Xftp软件上传
3.编写脚本lnmp.sh
vim lnmp.sh
#!/bin/bash
#this is auto install lamp or lnmp shell!
#apache安装
apache()
{
#apache编译安装
cd /opt/httpd-2.4.29 &&./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi &&make && make install &&cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
#
#修改配置文件
#在/etc/init.d/httpd文件中第二行插入2段
sed -i "2i#chkconfig: 35 85 21 \n\
#description: Apache is a World Wide Web server." /etc/init.d/httpd
#将Listen 80 替换成 Listen $ip:80 ,$ip为变量,是本机ip地址(特别注意变量的替换方法)
sed -i "s/^Listen 80/Listen $ip:80/g" /usr/local/httpd/conf/httpd.conf
#
#优化服务
ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd ;ln -s /usr/local/httpd/bin/* /usr/local/bin/
chkconfig --add httpd
systemctl daemon-reload
systemctl start httpd
#
#检查端口和网页测试
port=`netstat -anpt | grep httpd | wc -l`
if [ $? -eq 0 ] && [ $port -ne 0 ]; then
echo -e "\033\t\t\t[44;32m test http://$ip \033[0m"
read -p "input result:(y/n) " u
if [ $u == "y" ]; then
echo -e "\033\t\t\t[32m apache install success!\033[0m"
else
echo -e "\033\t\t\t[5;31m apache install fail!\033[0m"
fi
else
#"-e" 为转义,\033为固定格式(同 \e),"/t"为制表位,同tab键,"5"为闪烁,31m为字体颜色,此处红色,"\033[0m "为固定结束格式
echo -e "\033\t\t\t[5;31m apache install fail,check!!\033[0m"
exit 0
fi
}
#
#安装nginx
nginxt()
{
#编译安装
useradd -M -s /sbin/nologin nginx
cd /opt/nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ;nginx &&nginx -t;
#检查服务启动
stat=`netstat -ntap | grep "80" | wc -l`
if [ $? -eq 0 ]&&[ $stat -ne 0 ] ;then
echo -e "\033\t\t\t[32m nginx install success!!\033[0m"
else
echo -e "\033\t\t\t[5;31m nginx install fail!!\033[0m"
exit 0
fi
}
#
#mysql安装
mysql_con()
{
#编译安装
#判断有无/usr/local/mysql目录,没有就创建
[ ! -d /usr/local/mysql ] && mkdir -p /usr/local/mysql
/usr/sbin/useradd -s /sbin/nologin mysql
cd /opt/mysql-5.5.24
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/home/mysql \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306
if [ $? -eq 0 ];then
make && make install
#
#修改配置文件
echo "export PATH=$PATH:/usr/local/mysql/bin/ " >> /etc/profile
source /etc/profile && chown -R mysql:mysql /usr/local/mysql &&cd /usr/local/mysql/support-files &&cp my-f /etc/f &&cp mysql.server /etc/init.d/mysqld &&chmod 755 /etc/init.d/mysqld &&chkconfig --add /etc/init.d/mysqld ;
chkconfig mysqld --level 35 on
else
echo -e "\e\t\t\t[5;31m set error,check ![0m"
exit 1
fi
#初始化数据库
/usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--ldata=/var/lib/mysql \
--basedir=/usr/local/mysql \
--datadir=/home/mysql
ln -s /usr/local/mysql/bin//usr/local/bin/
#
#搜索basedir=,datadir=,并替换
sed -i 's#^basedir=.$#basedir=/usr/local/mysql#' /etc/init.d/mysqld
sed -i 's#^datadir=.*$#datadir=/home/mysql#' /etc/init.d/mysqld
#mysql启动和检查
systemctl daemon-reload && systemctl start mysqld
port=`netstat -ntap | grep mysql |wc -l`
if [ $? -eq 0 ]&&[ $port -ne 0 ];then
echo -e "\033\t\t\t[32m mysql install success!! \033[0m"
else
echo -e "\033\t\t\t[5;31m mysql install fail,check!! \033[0m"
exit 0
fi
}
#
#apache-php安装
apa_php()
{
#php编译安装
cd /opt/php-5.6.11
./configure \
--prefix=/usr/local/php5 \
--with-gd \
--with-zlib \
--with-apxs2=/usr/local/httpd/bin/apxs \
--with-mysql=/usr/local/mysql \
--with-config-file-path=/usr/local/php5 \
--enable-mbstring
make && make install &&cp php.ini-development /usr/local/php5/php.ini && ln -s /usr/local/php5/bin/ /usr/local/bin/ ; ln -s /usr/local/php5/sbin/ /usr/local/sbin/ ;
useradd -M -s /sbin/nologin php
#检查httpd.conf文件中是否有php5_module,并统计
num=`cat /usr/local/httpd/conf/httpd.conf | grep "php5_module" | wc -l`
#判断是否有php5_module
if [ $num -eq 0 ] ;then
echo -e "\e\t\t\t[5;31m no php5_module, check! \e[0m"
exit 0
else
echo -e "\e\t\t\t[32m php5_module is exist! \e[0m"
fi
#
#修改httpd配置,测试php
#在第258行行尾加index.php
sed -i '258s/$/ index.php/' /usr/local/httpd/conf/httpd.conf
#在383行插入相关文件
sed -i "383iAddType application/x-httpd-php .php\n\
AddType application/x-httpd-php-source .phps" /usr/local/httpd/conf/httpd.conf
systemctl restart httpd
#
#echo文件到index.php里面
echo -e "<?php \nphpinfo();\n?>" >>/usr/local/httpd/htdocs/index.php
#输出用户测试网页链接
echo -e "\e\t\t\t[5;34m test http://$ip/index.php \e[0m"
#php连接mysql测试
read -p "if connect mysql? (y/n)? " an
if [ $an == "y" ];then
#echo文件到index.php中,注意单引号中不识别$ip
echo -e '<?php \n$link=mysql_connect(localhost);\nif($link) echo "Success!!";\nelse echo "Fail!!";\nmysql_close();\n?>' >>/usr/local/httpd/htdocs/index1.php
#将localhost替换成 $ip','root' ,注意双引号中不识别$link(要特别注意单引号和双引号之间的区别!)
sed -i "s/localhost/'$ip','root'/g" /usr/local/httpd/htdocs/index1.php
echo -e "\e\t\t\t[32m http://$ip/index.php \e[0m"
else
echo -e "\e\t\t\t[32m lamp install finish!\e[0m"
exit 0
fi
}
#
#nginx-php安装
nginx_php()
{
useradd -M -s /sbin/nologin php
cd /opt/php-5.6.11
./configure \
--prefix=/usr/local/php5 \
--with-gd \
--with-zlib \
--with-mysql=/usr/local/mysql \
--with-config-file-path=/usr/local/php5 \
--enable-mbstring \
--with-jpeg-dir \
--with-openssl \
--disable-ipv6 \
--enable-fpm
make && make install
#安装gd和修改php配置文件
cd /opt/zend-loader-php5.5-linux-x86_64/ && cp ZendGuardLoader.so /usr/local/php5/lib/php
#
echo -e "[Zend Guard Loader]\nzend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so\nzend_loader.enable=1" >>/usr/local/php5/php.ini
cd /usr/local/php5/etc/ && cp php-fpm.conf.default php-fpm.conf
#替换文件
sed -i 's#^user = .$#user = php#' php-fpm.conf
sed -i 's#^group = .$#group = php#' php-fpm.conf
sed -i 's#^pm.max_children = .$#pm.max_children = 50#' php-fpm.conf
sed -i 's#^pm.max_spare_server = .$#pm.max_children = 35#' php-fpm.conf
sed -i 's#^pm.min_spare_server = .$#pm.min_children = 5#' php-fpm.conf
sed -i 's#^pm.start_server = .$#pm.min_children = 20#' php-fpm.conf
#php服务启动和检查
/usr/local/php5/sbin/php-fpm
port=`netstat -ntap | grep ":9000" | wc -l`
if [ $? -eq 0 ]&&[ $port -ne 0 ];then
echo -e "\033\t\t\t[32m php install success! \033[0m"
else
echo -e "\033\t\t\t[5;31m php install fail! \033[0m"
exit 0
fi
}
#
#nginx和php结合
ngin_vim()
{
#修改nginx配置
mkdir -p /var/www/html/btcom
#将45行 ; 替换成index.php; (或者在 ; 前面加 index.php,这个命令不好写)
sed -i '45s/;/ index.php;/g' /usr/local/nginx/conf/nginx.conf
#在64行插入
sed -i "64ilocation ~ .php$ {\n\
root /var/www/html/btcom;\n\
fastcgi_pass 127.0.0.1:9000;\n\
fastcgi_index index.php;\n\
fastcgi_param SCRIPT_FILENAME;\n\
include fastcgi_params;\n\
}" /usr/local/nginx/conf/nginx.conf
#在68行;后插入语句,注意双引号不识别$document_root$fastcgi_script_name,才用单引号重新插入
sed -i '68s/;/ $document_root$fastcgi_script_name;/g' /usr/local/nginx/conf/nginx.conf
echo -e "<?php \nphpinfo();\n?>" >>/var/www/html/btcom/index.php
nginx -s reload
echo -e "\e[32m test http://$ip/index.php \e[0m"
#
#php连接mysql
read -p "php connect mysql (y/n)? " answer
if [ $answer == "y" ];then
#echo文件,注意但,双引号,原因同上
echo -e '<?php \n$link=mysql_connect(localhost);\nif($link) echo "Success!!";\nelse echo "Fail!!";\nmysql_close();\n?>' >>/var/www/html/btcom/index1.php
sed -i "s/localhost/'$ip','root'/g" /var/www/html/btcom/index1.php
echo -e "\e\t\t\t[32m test http://$ip/index.php \e[0m"
else
echo -e "\e\t\t\t[32m lnmp install finish!\e[0m"
exit 0
fi
}
#
#论坛安装
discuz()
{
#不登录mysql,创建数据库,授权。
mysql -e "create database bbs;grant all on bbs.* To 'bbsuser'@'%' identified BY 'admin123';flush privileges;"
#bbsuser 连接 数据库
echo -e '<?php \n$link=mysql_connect(localhost);\nif($link) echo "Success!!";\nelse echo "Fail!!";\nmysql_close();\n?>' >>/usr/local/httpd/htdocs/index1.php
sed -i "s/localhost/'$ip','bbsuser','admin123'/g" /usr/local/httpd/htdocs/index1.php
#去网页验证连接数据库是否成功
echo -e "\e\t\t\t[32m test http://$ip/index1.php \e[0m"
#进行人机交互,连接成功进行discuz安装
read -p "bbs connect mysql success (y/n)? " aw
if [ $aw == "y" ];then
unzip Discuz_X2.5_SC_UTF8.zip -d /opt && mkdir -p /usr/local/httpd/htdocs/bbs/ &&cp -r /opt/upload/ /usr/local/httpd/htdocs/bbs/ &&cd /opt/httpd/htdocs/bbs/upload/
chown -R daemon ./config/
chown -R daemon ./data/
chown -R daemon ./uc_client/
chown -R daemon ./uc_server/data/
sed -i '927s/;date.timezone =/ date.timezone = UTC/g' /usr/local/php5/php.ini
#输出完成信息,提醒用户去网页安装,给出安装链接
echo -e "\e\t\t\t[5;31m you can install discuz at http://$ip/bbs/upload/install \e[0m"
#数据库连接失败,直接退出脚本
else echo -e "\e\t\t\t[5;31m bbs connect mysql fail,check! \e[0m"
exit 0
fi
}
#
#执行package.sh脚本
./package.sh
#关闭防火墙,增强性
systemctl stop firewalld.service
setenforce 0
#过滤出本机ip
ip=`ifconfig ens33 | grep "netmask" | awk '{print $2}'`
#查看e.sh,将2个脚本连接起来
num=`cat /root/e.sh`
#2脚本已经建立连接,删除e.sh,防止占用空间
rm -rf e.sh
#lamp脚本安装
if [ $num == "lamp" ]
then
#apache 安装
apache
#mysql 安装
mysql_con
#apache和php连接
apa_php
discuz
#lnmp脚本安装
elif [ $num == "lnmp" ]
then
#nginx.mysql,php安装
nginxt &&mysql_con &&nginx_php
#修改nginx配置文件
ngin_vim
discuz
else
echo -e "\e\t\t\t[5;31m input error,check! \e[0m"
exit 1
fi
4.赋予脚本权限
chmod +x *.sh
5.执行脚本
./lnmp.sh
6.编辑python shell
from selenium import webdriver
import time
#采用谷歌浏览器
driver = webdriver.Chrome()
#进入安装链接
driver.get("http://192.168.65.185/bbs/upload/install/")
print(driver.title)
#定位“同意”元素,点击鼠标
driver.find_element_by_name("submit").click()
#定位“下一步”元素,点击鼠标
driver.find_element_by_xpath("//input[@type='submit']").click()
#定位“下一步”元素,点击鼠标
driver.find_element_by_name("submitname").click()
#定位"数据库服务器填写框", 清除原有文本
driver.find_element_by_name("dbinfo[dbhost]").clear()
#在上一步,清除文本后,输入新文本
driver.find_element_by_name("dbinfo[dbhost]").send_keys("192.168.65.185")
#定位,清除文本
driver.find_element_by_name("dbinfo[dbname]").clear()
#填写新文本
driver.find_element_by_name("dbinfo[dbname]").send_keys("bbs")
driver.find_element_by_name("dbinfo[dbuser]").clear()
driver.find_element_by_name("dbinfo[dbuser]").send_keys("bbsuser")
driver.find_element_by_name("dbinfo[dbpw]").clear()
driver.find_element_by_name("dbinfo[dbpw]").send_keys("admin123")
driver.find_element_by_name("admininfo[password]").send_keys("admin123")
driver.find_element_by_name("admininfo[password2]").send_keys("admin123")
#定位"下一步"元素,点击鼠标
driver.find_element_by_name("submitname").click()
#跳转到论坛首页
driver.get("http://192.168.65.185/bbs/upload")
print(driver.title)
#自动输入用户名,密码
driver.find_element_by_name("username").send_keys("admin")
driver.find_element_by_name("password").send_keys("admin123")
#点击自动登录
driver.find_element_by_xpath("//button[@type='submit']").click()
#点击登录
driver.find_element_by_name("cookietime").click()
7.执行python shell (快捷键 F5)
温馨提示:
1.本次脚本是在上一篇博客的基础上完成的,package.sh脚本查看上一篇博客/13760351/2352154
2.window 网页自动安装方法是采用python selenium 模块,这部分知识将在后面的博客中发布,请期待!
3.此脚本不适用于新手安装discuz论坛(脚本执行太快,出现问题,无法找到问题,解决问题),只适合作为shell和Python shell 脚本爱好者练习使用。
4.脚本安装的方法很多,这只是其中一种,若大家有更方便快捷的脚本,欢迎留言交流!
5.脚本资料下载地址:/data/2459604
6.每个人的服务器地址不一样,需要手动去python shell修改,shell脚本执行完成后,需要手动去执行python shell 脚本!