1. 程式人生 > 實用技巧 >lnmp架構之一鍵搭建wordpress部落格網站指令碼(適用於centos7和centos8)

lnmp架構之一鍵搭建wordpress部落格網站指令碼(適用於centos7和centos8)

#!/bin/sh
set -e
. /etc/init.d/functions
#定義變數
PACKAGE="
gcc
make
pcre-devel
openssl-devel
expat-devel
bzip2
wget
openssl
bzip2-devel
unzip
perl-Data-Dumper
libaio-devel
ncurses-compat-libs
ncurses-libs
libaio
ncurses-c++-libs
libxml2-devel
libmcrypt-devel
sqlite-devel
oniguruma-devel
"
ML=/root
PHPPAGE=php-7.4
.12.tar.bz2 cudirectory=/root php_file=/apps/php74 phpversion=php-7.4.12 COLOR="echo -e \\E[1;32m" COLOR1="echo -e \\E[1;31m" END="\\E[0m" nginx_page=nginx-1.18.0.tar.gz nginx_ml=/apps/nginx mysql_ml=/data/mysql #php特定包,需事先放在root家目錄下 yum -y install oniguruma5php-6.9.5+rev1-4.el8.remi.x86_64.rpm && action "
oniguruma5php-6.9.5安裝完成" || { action "oniguruma5php-6.9.5安裝失敗,請檢查包是否存在!" false;exit; } yum -y install oniguruma5php-devel-6.9.5+rev1-4.el8.remi.x86_64.rpm && action "oniguruma5php-devel-6.9.5安裝完成" for PACK in $PACKAGE;do rpm -q $PACK &> /dev/null || yum -y -q install $PACK done #安裝httpd install_nginx() { #建立使用者
id nginx &> /dev/null || useradd -s /sbin/nologin nginx #下載原始碼包 ${COLOR}"正在下載原始碼包,請稍等!"${END} cd /usr/local/src/ [ -e $nginx_page ] || wget http://nginx.org/download/$nginx_page &> /dev/null tar xf $nginx_page #建立安裝目並修改許可權 [ -d ${nginx_ml}/run ] || mkdir -p ${nginx_ml}/run chown -R nginx.nginx ${nginx_ml} #編譯 ${COLOR}"正在編譯安裝,請稍等!"${END} cd nginx-1.18.0/ ./configure --prefix=${nginx_ml} \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module #安裝 make -j `lscpu | awk '/^CPU\(s\)/{print $2}'` && make install #修改配置檔案 [ -e ${nginx_ml}/conf/nginx.conf ] && sed -i 's@^#pid.*@pid /apps/nginx/run/nginx.pid;@' ${nginx_ml}/conf/nginx.conf #修改配置 [ -e ${nginx_ml}/conf/nginx.conf ] && sed -i '/#gzip on;/a\ include \/apps\/nginx\/conf\.d\/\*\.conf;' ${nginx_ml}/conf/nginx.conf #建立配置檔案 cat > /usr/lib/systemd/system/nginx.service <<EOF [Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/apps/nginx/run/nginx.pid ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP \$MAINPID ExecStop=/bin/kill -s TERM \$MAINPID [Install] WantedBy=multi-user.target EOF #配置nginx支援fastcgi mkdir -p ${nginx_ml}/conf.d cat > ${nginx_ml}/conf.d/test.conf <<EOF server { listen 80; server_name www.magedu.org; server_tokens off; location / { root /data/nginx/wordpress; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { root /data/nginx/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; include fastcgi_params; fastcgi_hide_header X-Powered-By; } location ~ ^/(ping|status)$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param PATH_TRANSLATED \$document_root\$fastcgi_script_name; } } EOF #命令建立 ln -s ${nginx_ml}/sbin/nginx /usr/sbin/nginx #啟動服務 systemctl daemon-reload systemctl enable --now nginx &> /dev/null && action "nginx安裝啟動完成!" || action "nginx啟動失敗,請檢查配置檔案!" false } #安裝資料庫 install_mysql() { #判斷是否在原始碼包目錄 [[ `pwd` == /root ]] || cd #建立mysql使用者 id mysql &> /dev/null || { useradd -r -d $mysql_ml -s /sbin/nologin mysql ; action "建立使用者成功"; } #建立mysql的家目錄資料夾 mkdir -pv $mysql_ml && chown mysql:mysql $mysql_ml #解壓二進位制程式 tar xf mariadb-10.2.31-linux-systemd-x86_64.tar.gz -C /usr/local #將原檔案建立為軟連結 ln -sv /usr/local/mariadb-10.2.31-linux-systemd-x86_64 /usr/local/mysql chown -R root:root /usr/local/mysql/ #環境變數 echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh source /etc/profile.d/mysql.sh #準備配置檔案 cp -b /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf cat > /etc/my.cnf <<EOF [mysqld] datadir = /data/mysql innodb_file_per_table = on skip_name_resolve = on socket=/data/mysql/mysql.sock log-error=/data/mysql/mysql.log pid-file=/data/mysql/mysql.pid [client] port=3306 socket=/data/mysql/mysql.sock EOF #建立資料庫檔案 /usr/local/mysql/scripts/mysql_install_db --datadir=/data/mysql --user=mysql #服務啟動檔案 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on service mysqld start #建立wordpress和discuz資料庫 /usr/local/mysql/bin/mysql -e "create database wordpress;" /usr/local/mysql/bin/mysql -e "create database discuz;" /usr/local/mysql/bin/mysql -e "grant all on wordpress.* to wordpress@'10.0.0.%' identified by '123456';" /usr/local/mysql/bin/mysql -e "grant all on discuz.* to discuz@'10.0.0.%' identified by '123456';" } #安裝php install_php() { . /etc/init.d/functions #判斷是否在包檔案目錄 [[ `pwd` == /root ]] || cd #解壓檔案 tar -jxvf $PHPPAGE &> /dev/null && action "php原始碼包解壓完成" [ -d $php_file ] || mkdir -p $php_file #編譯 cd ${cudirectory}/$phpversion ./configure \ --prefix=$php_file \ --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 | awk '/^CPU\(s\)/{print $2}'` && make install #準備環境變數 echo "PATH=${php_file}/bin:$PATH" > /etc/profile.d/php.sh source /etc/profile.d/php.sh #準備配置檔案 cp $cudirectory/${phpversion}/php.ini-production /etc/php.ini cp ${php_file}/etc/php-fpm.conf.default ${php_file}/etc/php-fpm.conf cp ${php_file}/etc/php-fpm.d/www.conf.default ${php_file}/etc/php-fpm.d/www.conf #準備服務啟動檔案 cp $cudirectory/${phpversion}/sapi/fpm/php-fpm.service /usr/lib/systemd/system/ #修改程序所有者 sed -i 's/^user.*/user = nginx/' ${php_file}/etc/php-fpm.d/www.conf sed -i 's/^group.*/group = nginx/' ${php_file}/etc/php-fpm.d/www.conf #支援status和ping頁面 sed -i '/;pm.status_path = \/status/a\pm.status_path = \/status' ${php_file}/etc/php-fpm.d/www.con sed -i '/;ping.path = \/ping/a\ping.path = \/ping' ${php_file}/etc/php-fpm.d/www.con #設定支援opcache加速 [ -d /etc/php.d ] || mkdir -p /etc/php.d/ cat > /etc/php.d/opcache.ini <<EOF [opcache] zend_extension=opcache.so opcache.enable=1 EOF #啟動服務 systemctl daemon-reload systemctl enable --now php-fpm.service && action "php-fpm服務啟動成功" || action "php-fpm服務啟動失敗,請檢查配置檔案" false } #安裝mysql if [[ `systemctl status mysql | awk '/Active/{print $2}'` == active ]] ;then action "資料庫已安裝過,跳過" else install_mysql && action "資料庫安裝完成" || action "資料庫安裝失敗,請檢查配置檔案" false fi #安裝apache if [[ `systemctl status nginx | awk '/Active/{print $2}'` == active ]] ;then action "nginx已安裝過,跳過" else install_nginx && action "nginx安裝完成" || action "nginx安裝失敗,請檢查配置檔案" false fi #安裝php if [[ `systemctl status php-fpm | awk '/Active/{print $2}'` == active ]] ;then action "apache已安裝過,跳過" else install_php action "php安裝完成" || action "php安裝失敗,請檢查配置檔案" false fi #配置wordpress install_wordpress() { [[ `pwd` == /root ]] || cd rpm -q unzip || yum -y install unzip &> /dev/null [ -f /root/wordpress-5.4.2-zh_CN.zip ] && unzip wordpress-5.4.2-zh_CN.zip || action "wordpress原始碼包不存在" mv /root/wordpress /data/nginx setfacl -R -m u:nginx:rwx /data/nginx/wordpress/ } install_wordpress && action "wordpress配置完成" || action "wordpress配置失敗,請檢查檔案" false