#!/bin/bash
# 更新软件包列表
sudo apt update
# 安装Nginx
sudo apt install -y nginx
# 安装MariaDB
sudo apt install -y mariadb-server
sudo mysql_secure_installation
# 安装PHP及常用扩展
sudo apt install -y php-fpm php-mysql php-common php-json php-xml php-zip php-gd php-curl
# 配置Nginx与PHP处理
sudo tee /etc/nginx/sites-available/default > /dev/null <<EOF
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files \$uri \$uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
}
EOF
# 启动Nginx和PHP-FPM服务
sudo systemctl start nginx
sudo systemctl start php7.4-fpm
sudo systemctl enable nginx
sudo systemctl enable php7.4-fpm
# 创建一个简单的PHP测试页面
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/index.php
# 重启Nginx服务以应用配置
sudo systemctl restart nginx
这段代码实现了在Ubuntu 22.04上快速部署LEMP堆栈的自动化安装和配置。代码中包含了更新软件包列表、安装Nginx、MariaDB以及PHP及其常用扩展的步骤,并配置了Nginx以处理PHP请求。最后,它创建了一个简单的PHP信息页面以便测试配置是否正确。