这几天摸索着学会了CentOS8下安装LNMP,配置起来有点花时间,就写成了个脚本。
此脚本会安装 Nginx-1.16、PHP-7.4、MySQL-8.0、phpmyadmin-5.0.4,并创建定时任务:每天1:30备份所有站点和数据库,每天2:00更新系统。

wget -O lnmp.sh https://cwlog.net/content/uploads/2020/11/4056844581.shell && sh lnmp.sh

数据目录:/var/www
站点配置目录:/var/www/conf
站点数据目录:/var/www/wwwroot
站点证书目录:/var/www/wwwssl
站点日志目录:/var/www/wwwlog
数据备份目录:/var/www/backup


创建新站点:

1、创建站点配置文件/var/www/conf/主域名.conf,内容参考 Nginx配置文件模板
2、创建站点根目录/var/www/wwwroot/主域名/,并通过wget下载或使用sftp上传网站文件
3、若开启HTTPS,上传域名证书文件至/var/www/wwwssl/主域名/主域名.pem,上传证书密钥文件至/var/www/wwwssl/主域名/主域名.key
4、执行systemctl restart nginx,若有提示信息无法启动,执行nginx查看错误信息进行修改


脚本代码如下

#!/bin/bash
echo "请确认系统版本为 CentOS 8.0 及以上"
echo "未安装 Nginx / PHP / MySQL"
echo "如符合要求请继续,否则请按 Ctrl + Z 终止安装"
echo -n "请设置MySQL密码:"
read password

echo "更新系统内核..."
yum -y update

echo "正在安装 EPEL && Remi..."
dnf -y install https://dl.Fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

echo "正在安装 Nginx-1.16..."
dnf -y module reset nginx
dnf -y module enable nginx:1.16
yum -y install nginx

echo "正在设置 Nginx..."
rm -rf /etc/nginx/nginx.conf
wget -O /etc/nginx/nginx.conf https://cwlog.net/content/uploads/2020/11/1815577153.conf
touch /etc/nginx/default.d/pathinfo.conf
echo 'location ~ .*\.php(\/.*)*$
{
    fastcgi_pass unix:/run/php-fpm/www.sock;
    # fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    set $real_script_name $fastcgi_script_name;
    if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
        set $real_script_name $1;
        set $path_info $2;
    }
    fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
    fastcgi_param SCRIPT_NAME $real_script_name;
    fastcgi_param PATH_INFO $path_info;
}' > /etc/nginx/default.d/pathinfo.conf

echo "正在启动 Nginx..."
systemctl enable nginx
systemctl start nginx

echo "正在安装 PHP-7.4"
dnf -y module reset php
dnf -y module enable php:remi-7.4
yum -y install php php-fpm php-pdo php-mysqlnd php-mbstring

echo "正在启动 PHP..."
systemctl enable php-fpm
systemctl start php-fpm

echo "正在安装 MySQL-8.0"
dnf -y module reset mysql
dnf -y module enable mysql:8.0
yum -y install mysql mysql-server

echo "正在启动 MySQL..."
systemctl enable mysqld
systemctl start mysqld

echo "正在设置 MySQL..."
yum -y install expect
echo '#!/usr/bin/expect
set timeout 60
set password [lindex $argv 0]
spawn mysql_secure_installation
expect {
"Press Y" { send "Y\r"; exp_continue}
"Press y" { send "y\r"; exp_continue}
"Please enter 0 = LOW" { send "0\r"; exp_continue}
"password" { send "$password\r"; exp_continue}
}
interact ' > mysql_secure_installation.exp 
chmod +x mysql_secure_installation.exp
./mysql_secure_installation.exp $password 
rm -rf mysql_secure_installation.exp

echo "正在创建站点目录..."
rm -rf /var/www/*
mkdir /var/www/conf
mkdir /var/www/wwwroot
mkdir /var/www/wwwssl
mkdir /var/www/wwwlog
mkdir /var/www/shell
mkdir /var/www/backup
mkdir /var/www/backup/local
mkdir /var/www/backup/local/web
mkdir /var/www/backup/local/db

echo '正在创建默认站点...'
touch /var/www/conf/default.conf
echo 'server {
    listen   80 default_server;
    listen   [::]:80;
    server_name    127.0.0.1;
    index   index.php index.html;
    root   /var/www/wwwroot/default;
    include /etc/nginx/default.d/php.conf;
    include /etc/nginx/default.d/pathinfo.conf;
}' > /var/www/conf/default.conf
mkdir /var/www/wwwroot/default
systemctl reload nginx
touch /var/www/wwwroot/default/index.html
echo '<html>
<p>LNMP is ok!</p>
<p><a href="/phpinfo.php">phpinfo</a> <a href="/phpmyadmin">phpmyadmin</a></p>
</html>' > /var/www/wwwroot/default/index.html

touch /var/www/wwwroot/default/phpinfo.php
echo '<?php phpinfo(); ?>' > /var/www/wwwroot/default/phpinfo.php

echo "正在安装 phpmyadmin..."
wget -O phpmyadmin.zip https://files.phpmyadmin.net/phpMyAdmin/5.0.4/phpMyAdmin-5.0.4-all-languages.zip
unzip phpmyadmin.zip -d /var/www/wwwroot/default
mv /var/www/wwwroot/default/phpMyAdmin-5.0.4-all-languages /var/www/wwwroot/default/phpmyadmin
rm -rf phpmyadmin.zip

echo "设置自动备份..."
touch /var/www/shell/backup.sh
echo '#!/bin/bash
nowDate=$(date +%Y%m%d_%H%M%S)
mysqldump -uroot -p'$password' --all-databases | gzip > /var/www/backup/local/db/db_${nowDate}.sql.gz
tar -zcPf /var/www/backup/local/web/web_${nowDate}.tar.gz /var/www/wwwroot/*' > /var/www/shell/backup.sh
chmod -R 777 /var/www/shell/backup.sh
echo "30 01 * * * /bin/sh /var/www/shell/backup.sh >> /tmp/tmp.txt" >> /var/spool/cron/root

echo "设置自动更新..."
echo "00 02 * * * yum -y update >> /tmp/tmp.txt" >> /var/spool/cron/root
systemctl restart crond

echo "安装完成!"
echo "您的MySQL密码是:$password"

标签: none

添加新评论