一键安装LAMP并部署wordpres
HTTPDIR=/apps/httpd
PHPDIR=/apps/php-fpm
MARIADBDIR=/apps/mariadb
MYSQLDIR=/apps/mysql
APPSDIR=/apps
WEBDIR=/data/web
function httpd() {
VERSION=2.4.46
APR=1.7.0
APR_UTIL=1.6.1
yum -y install wget gcc make pcre-devel openssl-devel expat-devel
id apache || useradd -r -s /sbin/nologin apache
if [ -d "$HTTPDIR" ]; then
echo directory exists
else
mkdir -p $HTTPDIR
fi
if [ -d "$WEBDIR" ]; then
echo directory exists
else
mkdir -p $WEBDIR
chown -R apache.apache $WEBDIR
fi
cd /usr/local/src
if [ -f httpd-$VERSION.tar.bz2 ]; then
echo file exists
else
wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-$VERSION.tar.bz2
fi
if [ -f "apr-$APR.tar.bz2" ]; then
echo file exists
else
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-$APR.tar.bz2
fi
if [ -f "apr-util-$APR_UTIL.tar.bz2" ]; then
echo file exists
else
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2
fi
tar xf httpd-$VERSION.tar.bz2
tar xf apr-$APR.tar.bz2
tar xf apr-util-$APR_UTIL.tar.bz2
mv -f apr-$APR httpd-$VERSION/srclib/apr
mv -f apr-util-$APR_UTIL httpd-$VERSION/srclib/apr-util
cd httpd-$VERSION
./configure --prefix=$HTTPDIR --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=event
make -j `lscpu |grep "^CPU(s)"|awk '{print $NF}'` && make install
sed -i 's/^User.*/User apache/' $HTTPDIR/conf/httpd.conf
sed -i 's/^Group.*/Group apache/' $HTTPDIR/conf/httpd.conf
grep "AddType application/x-httpd-php .php" $HTTPDIR/conf/httpd.conf || (
sed -i "s@#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so@LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so@" $HTTPDIR/conf/httpd.conf
sed -i "s@#LoadModule proxy_module modules/mod_proxy.so@LoadModule proxy_module modules/mod_proxy.so@" $HTTPDIR/conf/httpd.conf
cat >> $HTTPDIR/conf/httpd.conf <<EOF
AddType application/x-httpd-php .php
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000$WEBDIR/$1
ProxyPassMatch ^/(fpm_status|ping) fcgi://127.0.0.1:9000
EOF
)
sed -i "s/DirectoryIndex index.html/DirectoryIndex index.php index.html/" $HTTPDIR/conf/httpd.conf
ln -s $HTTPDIR/bin/* /usr/bin
echo 'MANDATORY_MANPATH /apps/httpd/man' >> /etc/man_db.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/httpd/bin/apachectl start
ExecReload=/apps/httpd/bin/apachectl graceful
ExecStop=/apps/httpd/bin/apachectl stop
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now httpd
echo "httpd-$VERSION install succeed"
}
function php-fpm() {
VERSION=7.4.12
mkdir -p $PHPDIR
id apache || useradd -r -s /sbin/nologin apache
sed -i "s@enabled=0@enabled=1@g" /etc/yum.repos.d/CentOS-Base.repo
yum -y install gcc libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel php-mysqlnd
if [ -d "$PHPDIR" ]; then
echo $PHPDI exists!
else
mkdir $PHPDI
fi
if [ -f "php-$VERSION.tar.xz" ]; then
echo "php-$VERSION.tar.xz exists!"
else
wget https://www.php.net/distributions/php-$VERSION.tar.xz
fi
tar -xf php-$VERSION.tar.xz
cd php-$VERSION
./configure \
--prefix=$PHPDIR \
--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
make -j `lscpu |grep "^CPU(s)"|awk '{print $NF}'` && make install
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
cd $PHPDIR/etc
cp php-fpm.conf.default php-fpm.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf
sed -i "s/^user = nobody/user = apache/" www.conf
sed -i "s/^group = nobody/group = apache/" www.conf
sed -i "s@^;pm.status_path = /status@pm.status_path = /fpm_status@" www.conf
sed -i "s@^;ping.path = /ping@ping.path = /ping@" www.conf
if [ -f "/etc/php.d/opcache.ini" ]; then
echo opacche is start!
else
cat >> /etc/php.d/opcache.ini <<EOF
[opcache]
zend_extension=opcache.so
opcache.enable=1
EOF
fi
systemctl daemon-reload
systemctl start php-fpm
}
function mariadb() {
MARIADB=mariadb-10.2.15-linux-x86_64
APPSDIR=/apps
DATA_DIR=/data/db
yum install libaio ncurses-compat-libs -y
id mysql || useradd -r -s /sbin/nologin mysql
if [ -f "$MARIADB.tar.gz" ]; then
true
else
echo file not exists!!
fi
mkdir -p $DATA_DIR
chown -R mysql.mysql $DATA_DIR
echo unzip ing.....
tar -xf $MARIADB.tar.gz -C $APPSDIR
ln -sv $APPSDIR/$MARIADB $MARIADBDIR
chown -R root.root $MARIADBDIR
cd $MARIADBDIR
cp support-files/my-large.cnf /etc/my.cnf
sed -i "s@^socket = /tmp/mysql.sock@socke = $DATA_DIR/mysql.sock@" /etc/my.cnf
grep "/datadir =/data/mysql" /etc/my.cnf || (
cat >> /etc/my.cnf<<EOF
[mysqld]
datadir =/data/mysql
skip_name_resolve = ON
EOF
)
exit 10
./scripts/mysql_install_db --user=mysql --basedir=$MARIADBDIR --datadir=$DATA_DIR
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
service mysqld start
}
function mysql(){
mysql=mysql-5.7.30-linux-glibc2.12-x86_64
mysqldatadir=/data/mysql
yum -y install libaio numactl-libs ncurses-compat-libs
cd ~
if [ -f "$mysql.tar.gz" ]; then
if [ -d "$APPSDIR" ]; then
echo directory exists
else
mkdir -p $APPSDIR
fi
echo unziping ....
tar -xf $mysql.tar.gz -C $APPSDIR
cd $APPSDIR
ln -s $mysql $MYSQLDIR
chown -R root.root $MYSQLDIR
else
echo file not exists
fi
id mysql || useradd -r -s /sbin/nologn mysql
echo 'PATH=/apps/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
cp /etc/my.cnf{,.bak}
if [ -d "$mysqldatadir" ]; then
echo $mysqldatadir exists please delete!
exit 2
else
mkdir -p $mysqldatadir
chown -R mysql.mysql $mysqldatadir
fi
cat > /etc/my.cnf<<EOF
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
cd $MYSQLDIR/bin
mysqld --initialize --user=mysql --datadir=/data/mysql
pass=`awk '/temporary password/{print $NF}' /data/mysql/mysql.log |tee -a pass.file`
cp $MYSQLDIR/support-files/mysql.server /etc/init.d/mysqld
sed -i "s@^basedir=@basedir=$MYSQLDIR@" /etc/init.d/mysqld
sed -i "s@^datadir=@datadir=$mysqldatadir@" /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
echo "PASS:$pass"
}
function wordpres(){
cd ~
wordpres=https://cn.wordpress.org/latest-zh_CN.tar.gz
if [ -f "latest-zh_CN.tar.gz" ]; then
echo wordpress exists
else
wget $wordpres
fi
if [ -d "$WEBDIR" ]; then
echo $WEBDIR exists
else
mkdir -p $WEBDIR
fi
tar -xf latest-zh_CN.tar.gz
cp -r wordpress/* $WEBDIR/
chown -R apache.apache $WEBDIR
systemctl restart httpd php-fpm mysql
}
while true;do
echo "1)一键安装LAMP"
echo "2)安装httpd"
echo "3)安装php-fpm"
echo "4)安装mysql"
echo "5)安装WordPress"
echo "6)退出"
read -p "请输入你的选择:" USERCHOOSE
mkdir $APPSDIR
case "$USERCHOOSE" in
1)
echo "一键安装LAMP"
httpd
php-fpm
mysql
echo "LAMP install succeed"
continue
;;
2)
echo "安装httpd"
httpd
echo "httpd install succeed"
continue
;;
3)
echo "安装php-fpm"
php-fpm
echo "php-fpm install succeed"
continue
;;
4)
echo "安装mysql"
mysql
echo "mysql install succeed"
continue
;;
5)
echo "安装WordPress"
wordpres
continue
;;
6)
echo "退出"
exit 1
;;
*)
echo "输入有误!"
continue
;;
esac
done