#!/bin/bash
MYSQL_PASSWD=‘P@ssw0rd‘
SRC_DIR=‘/usr/local/src‘
APPS_DIR=‘/apps‘
WP_PASSWD=‘P@ssw0rd‘
MYSQL_FILE=‘mysql-5.7.30-el7-x86_64.tar.gz‘
HTTPD_FILE=‘httpd-2.4.46.tar.gz‘
APR_FILE=‘apr-1.7.0.tar.gz‘
APR_UNTIL_FILE=‘apr-util-1.6.1.tar.gz‘
PHP_FILE=‘php-7.4.10.tar.xz‘
WP_FILE=‘latest-zh_CN.tar.gz‘
MYSQL_DIR=${MYSQL_FILE%.tar*}
HTTPD_DIR=${HTTPD_FILE%.tar*}
APR_DIR=${APR_FILE%.tar*}
APR_UNTIL_DIR=${APR_UNTIL_FILE%.tar*}
PHP_DIR=${PHP_FILE%.tar*}
install_mysql(){
echo ‘正在安装依赖包‘
yum -y install numactl-libs libaio &> /dev/null && echo ‘安装依赖包完成‘
echo ‘正在下载$MYSQL_FILE这个包‘
[ -d $APPS_DIR ] || mkdir $APPS_DIR
wget -P $SRC_DIR https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/$MYSQL_FILE || { echo "mysql下载失败" ; exit ; }
echo "下载成功,解压并安装MySQL"
tar xf $SRC_DIR/$MYSQL_FILE -C $APPS_DIR/ &> /dev/null
id mysql &> /dev/null || useradd -s /sbin/nologin -r mysql
ln -s /apps/mysql-5.7.30-el7-x86_64/bin/* /usr/bin/ &> /dev/null && echo ‘软连接创建成功‘
# echo "PATH=$APPS_DIR/$MYSQL_DIR/bin/:$PATH" > /etc/profile.d/lamp.sh
# source /etc/profile.d/lamp.sh
[ -d /data ] || mkdir /data
cat > /etc/my.cnf <<EOF
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
mysqld --initialize --user=mysql --datadir=/data/mysql && echo "MySQL 数据库文件是 /data/mysql"
cp $APPS_DIR/$MYSQL_DIR/support-files/mysql.server /etc/init.d/mysqld
sed -ri -e "/^basedir=/c basedir=$APPS_DIR/$MYSQL_DIR" -e "/^datadir=/c datadir=/data/mysql" -e "/^mysqld_pid_file_path=/c mysqld_pid_file_path=/data/mysql/mysql.pid" /etc/init.d/mysqld
systemctl daemon-reload
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start &> /dev/null
[ $? -eq 0 ] && echo ‘数据库启动成功‘ || { echo "数据库启动失败,退出!";exit; }
OLDPASSWD=`awk ‘/A temporary/{print $NF}‘ /data/mysql/mysql.log`
echo "初始密码为 $OLDPASSWD"
mysqladmin -uroot -p$OLDPASSWD password $MYSQL_PASSWD &> /dev/null && echo "MySQL密码为$MYSQL_PASSWD"
}
install_httpd(){
echo "正在安装依赖包"
yum -q -y install gcc make openssl openssl-devel expat-devel && echo ‘依赖包安装完成‘
echo "正在下载$HTTPD_FILE、$APR_FILE、$APR_UNTIL_FILE这三个包"
wget -P $SRC_DIR https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/$HTTPD_FILE || { echo "http下载失败";exit; }
wget -P $SRC_DIR https://mirrors.tuna.tsinghua.edu.cn/apache/apr/$APR_FILE || { echo "apr下载失败";exit; }
wget -P $SRC_DIR https://mirrors.tuna.tsinghua.edu.cn/apache/apr/$APR_UNTIL_FILE || { echo "apr-until下载失败";exit; }
echo "开始安装httpd"
cd $SRC_DIR
tar xf $HTTPD_FILE
tar xf $APR_FILE
tar xf $APR_UNTIL_FILE
mv $APR_DIR $HTTPD_DIR/srclib/apr
mv $APR_UNTIL_DIR $HTTPD_DIR/srclib/apr-util
cd $HTTPD_DIR
id apache &> /dev/null || { useradd -s /sbin/nologin -r apache; }
./configure --prefix=/apps/httpd24 --enable-ssl --enable-so --enable-cgi --enable-rewirte --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event &> /dev/null && echo ‘./configure 执行成功‘ || { echo ‘./configure 失败并退出‘;exit; }
echo ‘正在make && make install时间长出去抽根烟吧‘
make &> /dev/null && make install &> /dev/null && echo ‘make install 成功‘ || { echo ‘make install 失败并退出‘;exit; }
# echo ‘PATH=/apps/httpd24/bin/:$PATH‘ >> /etc/profile.d/lamp.sh
# source /etc/profile.d/lamp.sh
ln -s /apps/httpd24/bin/* /usr/bin/ &> /dev/null && echo ‘软连接创建成功‘
sed -ri -e ‘s/^User.*/user apache/g‘ -e ‘s/Group.*/group apache/g‘ /apps/httpd24/conf/httpd.conf
cat > /lib/systemd/system/httpd.service << EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
ExecStart=/apps/httpd24/bin/apachectl start
ExecReload=/apps/httpd24/bin/apachectl graceful
ExecStop=/apps/httpd24/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
echo "http安装完成"
systemctl daemon-reload
systemctl enable --now httpd && echo ‘httpd启动成功‘||{ echo "httpd启动失败";exit; }
}
install_php(){
echo "安装php依赖"
yum -q -y install gcc make sqlite-devel libxml2-devel bzip2-devel libmcrypt-devel libsqlite3x-devel oniguruma-devel
wget -P $SRC_DIR http://mirrors.sohu.com/php/$PHP_FILE || { echo "php下载失败";exit; }
echo "开始安装PHP"
cd $SRC_DIR
tar xf $PHP_FILE
cd $PHP_DIR
./configure --prefix=/apps/php74 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo &> /dev/null && echo ‘php ./configure执行成功‘ || { echo ‘php ./configure执行失败‘;exit; }
make &> /dev/null && make install &> /dev/null && echo ‘php make install执行成功‘ || { echo ‘php make install执行失败‘;exit; }
[ $? -eq 0 ] || { echo "php编译安装失败";exit; }
cp php.ini-production /etc/php.ini
[ -d /etc/php.d ] || mkdir /etc/php.d
cat > /etc/php.d/opcache.ini <<EOF
[opcache]
zend_extension=opcache.so
opcache.enable=1
EOF
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
cd /apps/php74/etc
cp php-fpm.conf.default php-fpm.conf
cd php-fpm.d/
cp www.conf.default www.conf
id apache &> /dev/null || useradd -s /sbin/nologin -r apache
sed -i.bak -e ‘s/^user.*/user = apache/‘ -e ‘s/^group.*/group = apache/‘ /apps/php74/etc/php-fpm.d/www.conf
[ -d /data/www ] || { mkdir /data/www -p ; chown -R apache.apache /data/www ; }
sed -ri -e ‘/^DocumentRoot/c DocumentRoot "/data/www"‘ -e ‘s@/apps/httpd24/htdocs@/data/www@g‘ -e ‘/#LoadModule proxy_module/a LoadModule proxy_module modules/mod_proxy.so‘ -e ‘/#LoadModule proxy_fcgi_module/a LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so‘ -e ‘s@DirectoryIndex index.html@DirectoryIndex index.php index.html@g‘ /apps/httpd24/conf/httpd.conf
echo ‘ProxyRequests off‘ >> /apps/httpd24/conf/httpd.conf
echo ‘ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/www/$1‘ >> /apps/httpd24/conf/httpd.conf
echo ‘ProxyPassMatch ^/(fpm_status|ping)$ fcgi://127.0.0.1:9000/$1‘ >> /apps/httpd24/conf/httpd.conf
systemctl daemon-reload
systemctl restart httpd && echo ‘httpd重启成功‘
systemctl restart php-fpm && echo ‘php-fpm重启成功‘ || { echo "PHP安装失败";exit; }
}
install_wordpress(){
#下面这个地址下载很慢可以提前下载好放到/usr/local/src/下,注释掉下面这行
wget -P $SRC_DIR http://cn.wp101.net/latest-zh_CN.tar.gz || { echo "下载word press失败";exit; }
echo "开始安装wordpress"
cd $SRC_DIR
tar xf $WP_FILE
mv wordpress/* /data/www/
chown -R apache.apache /data/www/wp-content/
cd /data/www/
mv wp-config-sample.php wp-config.php
mysql -uroot -p"$MYSQL_PASSWD" -e "create database wordpress;grant all on wordpress.* to wordpress@‘127.0.0.1‘ identified by ‘$WP_PASSWD‘" &>/dev/null
sed -i.bak -e ‘s/database_name_here/wordpress/‘ -e ‘s/username_here/wordpress/‘ -e ‘s/password_here/‘‘‘$WP_PASSWD‘‘‘/‘ -e ‘s/localhost/127.0.0.1/‘ wp-config.php && echo "wordpress 安装完成"
}
install_mysql
install_httpd
install_php
install_wordpress