前提:确保80端口未被占用 yum源仓库正确,如果有不会配置请查看jiahttps://blog.csdn.net/2202_75975220/article/details/139335608?spm=1001.2014.3001.5501
nano install_lnmp_wordpress.sh#创建编辑脚本文件
#!/bin/bash
# 检查是否以 root 用户运行
if [ "$(id -u)" -ne 0 ]; then
echo "请以 root 用户运行此脚本"
exit 1
fi
# 用户输入配置
read -p "请输入服务器的IP地址: " SERVER_IP
read -s -p "请输入 MariaDB root 用户的密码: " MYSQL_ROOT_PASSWORD
echo
read -p "请输入要创建的 WordPress 数据库名: " MYSQL_WORDPRESS_DB
read -p "请输入 WordPress 数据库用户: " MYSQL_WORDPRESS_USER
read -s -p "请输入 WordPress 数据库用户密码: " MYSQL_WORDPRESS_PASSWORD
echo
# 安装必要的依赖
echo "安装必要的依赖..."
dnf install -y epel-release wget unzip
# 安装 Nginx
echo "安装 Nginx..."
dnf install -y nginx
# 启动并启用 Nginx 服务
systemctl enable nginx
systemctl start nginx
# 安装 MariaDB
echo "安装 MariaDB..."
dnf install -y mariadb-server
# 启动并启用 MariaDB 服务
systemctl enable mariadb
systemctl start mariadb
# 设置 MariaDB root 用户密码
echo "设置 MariaDB root 用户密码..."
mysql -u root <<EOF
ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD';
FLUSH PRIVILEGES;
EOF
# 安装 PHP 和必要的扩展
echo "安装 PHP 和必要的扩展..."
dnf install -y php php-fpm php-mysqlnd php-xml php-mbstring php-json php-gd
# 启动并启用 PHP-FPM 服务
systemctl enable php-fpm
systemctl start php-fpm
# 配置 Nginx 使用 PHP
echo "配置 Nginx 使用 PHP..."
cat > /etc/nginx/conf.d/default.conf <<EOF
server {
listen 80;
server_name $SERVER_IP;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files \$uri \$uri/ =404;
}
location ~ \.php\$ {
try_files \$uri =404;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
EOF
# 重新加载 Nginx 配置
systemctl restart nginx
# 安装 WordPress
echo "安装 WordPress..."
wget https://wordpress.org/latest.zip -P /tmp
unzip /tmp/latest.zip -d /usr/share/nginx/html/
mv /usr/share/nginx/html/wordpress/* /usr/share/nginx/html/
rm -rf /usr/share/nginx/html/wordpress /tmp/latest.zip
# 设置 WordPress 文件权限
chown -R nginx:nginx /usr/share/nginx/html
chmod -R 755 /usr/share/nginx/html
# 创建 WordPress 数据库和用户
echo "创建 WordPress 数据库和用户..."
mysql -u root -p"$MYSQL_ROOT_PASSWORD" <<EOF
CREATE DATABASE $MYSQL_WORDPRESS_DB;
CREATE USER '$MYSQL_WORDPRESS_USER'@'localhost' IDENTIFIED BY '$MYSQL_WORDPRESS_PASSWORD';
GRANT ALL PRIVILEGES ON $MYSQL_WORDPRESS_DB.* TO '$MYSQL_WORDPRESS_USER'@'localhost';
FLUSH PRIVILEGES;
EOF
# 创建 wp-config.php 文件
echo "创建 wp-config.php 文件..."
cat > /usr/share/nginx/html/wp-config.php <<EOF
<?php
define( 'DB_NAME', '$MYSQL_WORDPRESS_DB' );
define( 'DB_USER', '$MYSQL_WORDPRESS_USER' );
define( 'DB_PASSWORD', '$MYSQL_WORDPRESS_PASSWORD' );
define( 'DB_HOST', 'localhost' );
define( 'DB_CHARSET', 'utf8mb4' );
define( 'DB_COLLATE', '' );
define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
define('NONCE_KEY', 'put your unique phrase here');
define('AUTH_SALT', 'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT', 'put your unique phrase here');
define('NONCE_SALT', 'put your unique phrase here');
\$table_prefix = 'wp_';
define( 'WP_DEBUG', false );
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}
require_once ABSPATH . 'wp-settings.php';
EOF
# 设置 wp-config.php 文件权限
chown nginx:nginx /usr/share/nginx/html/wp-config.php
chmod 644 /usr/share/nginx/html/wp-config.php
# 提示用户进行 WordPress 安装
echo "LNMP 环境部署完成,WordPress 文件已下载并配置。"
echo "请通过浏览器访问 http://$SERVER_IP 以完成 WordPress 的安装。"
echo "MariaDB root 密码: $MYSQL_ROOT_PASSWORD"
echo "MariaDB WordPress 数据库: $MYSQL_WORDPRESS_DB"
echo "MariaDB WordPress 用户: $MYSQL_WORDPRESS_USER"
echo "MariaDB WordPress 用户密码: $MYSQL_WORDPRESS_PASSWORD"
chmod +x install_lnmp_wordpress.sh#修改权限
sudo bash install_lnmp_wordpress.sh
它会询问你的ip,数据库密码等,根据自己情况设置。

1703

被折叠的 条评论
为什么被折叠?



