read -p "您的服务器核心数" N
#判断软件包是否存在
if [ ! -f /opt/nginx-1.12.0.tar.gz ];then
echo "/opt/目录中没有软件包nginx-1.12.0.tar.gz"
exit 1
fi
if [ ! -f /opt/mysql-boost-5.7.20.tar.gz ];then
echo " /opt 目录中没有mysql-boost-5.7.20.tar.gz"
exit 1
fi
if [ ! -f /opt/php-7.1.10.tar.bz2 ];then
echo " /opt 目录中没有mysql-boost-5.7.20.tar.gz"
exit 1
fi
yum clean all &> /dev/null
yum makecache &> /dev/null
if [ $? -ne 0 ];then
echo "请配置好yum源"
exit 1
fi
#安装nginx 依赖包
yum -y install pcre-devel zlib-devel gcc gcc-c++ make &>/dev/null
#安装mysql 环境依赖包
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake &>/dev/null
#安装php依赖包
yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel &>/dev/null
#判断依赖包是否安装
PAGE="pcre-devel zlib-devel gcc gcc-c++ make ncurses ncurses-devel bison cmake gd \
libjpeg libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel"
b=0
a=0
while [ $b -eq 0 ]
do
for i in `echo $PAGE`
do
rpm -qa | grep $i &> /dev/null
if [ $? -ne 0 ];then
echo "$i 没有安装"
a=1
fi
done
if [ $a -eq 1 ];then
exit 1
else
echo "依赖包安装完成"
b=1
fi
done
#关闭防火墙和selinux
systemctl stop firewalld
echo "防火墙关闭"
systemctl disable firewalld &>/dev/null
setenforce 0 &>/dev/null
sed -i '7c SELINUX=disabled' /etc/selinux/config
echo "selinux 关闭"
#############安装nginx 服务#############
#创建运行用户
id nginx &> /dev/null
if [ $? -ne 0 ];then
useradd -M -s /sbin/nologin nginx
fi
#编译安装
cd /opt
tar zxf nginx-1.12.0.tar.gz -C /opt/
cd nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module &>/dev/null
echo "开始编译nginx"
make -j $N &>/dev/null
if [ $? -eq 0 ];then
echo "nginx 编译完成"
else
echo "echo nginx 编译失败"
exit 1
fi
make install &>/dev/null
echo "nginx 安装完成"
#优化路径
ln -sf /usr/local/nginx/sbin/nginx /usr/local/sbin/
#添加 Nginx 系统服务 (使用免交互,要注意有变量,不可以让变量被替换
cat >/lib/systemd/system/nginx.service <<'EOF'
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
chmod 754 /lib/systemd/system/nginx.service
systemctl daemon-reload
systemctl restart nginx.service &> /dev/null
sleep 3
netstat -natp | grep :80 &> /dev/null
if [ $? -eq 0 ];then
echo "nginx 服务已启动"
else
echo "nginx 未启动"
exit 1
fi
systemctl enable nginx.service &> /dev/null
########### 安装mysql ########
#创建运行用户
id mysql &>/dev/null
if [ $? -ne 0 ]; then
useradd -M -s /sbin/nologin mysql
fi
#编译安装
cd /opt
tar zxf mysql-boost-5.7.20.tar.gz
cd /opt/mysql-5.7.20/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 &> /dev/null
echo "开始编译 mysql"
make -j $N &>/dev/null
if [ $? -eq 0 ] ;then
echo "mysql 编译完成"
else
echo "mysql 编译失败"
exit 1
fi
make install &>/dev/null
echo "mysql 安装成功"
#修改 mysql 配置文件
cat > /etc/my.cnf <<EOF
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
EOF
#修改Mysql安装目录和配置文件属主属组
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
#设置路径环境变量
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
sleep 1
source /etc/profile
sleep 2
#初始化数据库
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data &>/dev/null
#添加mysqld 系统服务
/usr/bin/cp -f /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl restart mysqld.service &>/dev/null
sleep 3
netstat -natp | grep :3306 &> /dev/null
if [ $? -eq 0 ];then
echo "mysql 启动成功"
else
echo "mysql 未启动"
exit 1
fi
systemctl enable mysqld &> /dev/null
#修改mysql 登录密码
source /etc/profile
yum -y install expect &> /dev/null
/usr/bin/expect <<-EOF
spawn mysqladmin -u root -p password "abc123"
expect "Enter password:"
send "\n"
expect eof
EOF
#授权远程登录
mysql -u root -p"abc123" -e "grant all privileges on *.* to 'root'@'%' identified by 'abc123';" &>/dev/null
###### 安装 php ####
#编译按装
cd /opt
tar jxf php-7.1.10.tar.bz2
cd php-7.1.10
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip &> /dev/null
echo "php 开始编译"
make -j $N &>/dev/null
if [ $? -eq 0 ];then
echo "php 编译成功"
else
echo "php 编译失败"
exit 1
fi
make install &>/dev/null
#路径优化
ln -sf /usr/local/php/bin/* /usr/local/bin/
ln -sf /usr/local/php/sbin/* /usr/local/sbin/
#调整主配置文件:
/usr/bin/cp -f /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini.bak
sed -i '/mysqli.default_socket =/c mysqli.default_socket = /usr/local/mysql/mysql.sock' /usr/local/php/lib/php.ini.bak
sed -i '/;date.timezone =/c date.timezone = Asia/Shanghai' /usr/local/php/lib/php.ini.bak
/usr/bin/cp -fp /usr/local/php/lib/php.ini.bak /usr/local/php/lib/php.ini
#调整进程服务配置文件
cd /usr/local/php/etc/
/usr/bin/cp -pf php-fpm.conf.default php-fpm.conf.bak
sed -i '/;pid = run\/php-fpm.pid/c pid = run/php-fpm.pid' /usr/local/php/etc/php-fpm.conf.bak
/usr/bin/cp -pf /usr/local/php/etc/php-fpm.conf.bak /usr/local/php/etc/php-fpm.conf
#调整扩展配置文件
cd /usr/local/php/etc/php-fpm.d/
/usr/bin/cp -f www.conf.default www.conf
#启动php-fpm
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
cd /opt/php-7.1.10/sapi/fpm
/usr/bin/cp -f php-fpm.service /usr/lib/systemd/system/php-fpm.service
systemctl restart php-fpm.service &>/dev/null
sleep 3
netstat -natp | grep :9000 &>/dev/null
if [ $? -eq 0 ];then
echo "php-fpm 启动成功"
else
echo "php-fpm 启动失败"
exit 1
fi
#修改nginx 支持php解析
/usr/bin/cp -f /usr/local/nginx/conf/nginx.conf{,.bak}
sed -i '65,71 s/#//' /usr/local/nginx/conf/nginx.conf.bak
sed -i '69 s#/scripts#/usr/local/nginx/html#' /usr/local/nginx/conf/nginx.conf.bak
/usr/bin/cp -f /usr/local/nginx/conf/nginx.conf.bak /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -s reload &> /dev/null
#测试php
cat > /usr/local/nginx/html/index.php <<EOF
<?php
phpinfo();
?>
EOF
curl -sI http://192.168.23.103/index.php | grep "X-Powered-By: PHP/7.1.10" &> /dev/null
if [ $? -eq 0 ];then
echo "php连接成功"
else
echo "php连接失败"
exit 1
fi
#测试mysql
source /etc/profile
mysql -u root -p'abc123' -e "CREATE DATABASE bbs;" -e "GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';" -e "GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';" -e "flush privileges;" &> /dev/null
cat > /usr/local/nginx/html/index.php <<'EOF'
<?php
$link=mysqli_connect('192.168.23.103','bbsuser','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
EOF
curl -s http://192.168.23.103/index.php | grep -i "success" &> /dev/null
if [ $? -eq 0 ];then
echo "mysql 数据库连接成功"
else
echo "mysql 数据库连接失败"
exit 1
fi