组织了一个简易快捷的LNMP环境搭建脚本,可以用来快速测试服务器,避免手动安装要输入指令还有改配置麻烦
在系统已经更新的情况下(没更新会自动更新),基本5分钟左右即可完成,如果不想自己编辑文件可以在线一键安装
wget https://www.hl05.com/hluse/hluse_lnmp.tar.gz -O hluse_lnmp.tar.gz && tar zxf hluse_lnmp.tar.gz && bash ./hluse_lnmp.sh
5分钟左右安装完成并且安装好PHP指针#!/bin/bash
version=v1.01
echo "==============================================================================================================="
echo -e "\e[1;$[RANDOM%7+31]m
___ ___ ___ ___ ___
/\__\ /\__\ /\__\ /\ \ /\ \
/:/ / /:/ / /:/ / /::\ \ /::\ \
/:/__/ /:/ / /:/ / /:/\ \ \ /:/\:\ \
/::\ \ ___ /:/ / /:/ / ___ _\:\~\ \ \ /::\~\:\ \
/:/\:\ /\__\ /:/__/ /:/__/ /\__\ /\ \:\ \ \__\ /:/\:\ \:\__\
\/__\:\/:/ / \:\ \ \:\ \ /:/ / \:\ \:\ \/__/ \:\~\:\ \/__/
\::/ / \:\ \ \:\ /:/ / \:\ \:\__\ \:\ \:\__\
/:/ / \:\ \ \:\/:/ / \:\/:/ / \:\ \/__/
/:/ / \:\__\ \::/ / \::/ / \:\__\
\/__/ \/__/ \/__/ \/__/ \/__/ \e[0m"
echo -e "--- 最简安装LNMP环境+PHP探针"
echo -e "--- version: $version"
echo -e "--- https://www.hl05.com"
echo "==============================================================================================================="
# 定义颜色代码
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # 恢复默认颜色
# 检查root权限
if [ "$(id -u)" != "0" ]; then
echo -e "${RED}错误:该脚本需要以root权限运行!${NC}" >&2
exit 1
fi
# 检测操作系统
if grep -qiE "ubuntu|debian" /etc/os-release; then
SYSTEM="debian"
elif grep -qiE "centos|rhel|almalinux|rocky|euler" /etc/os-release; then
SYSTEM="centos"
elif grep -qi "fedora" /etc/os-release; then
SYSTEM="fedora"
elif grep -qi "arch" /etc/os-release; then
SYSTEM="arch"
elif grep -qi "opensuse" /etc/os-release; then
SYSTEM="opensuse"
else
echo -e "${RED}错误:不支持的操作系统!${NC}" >&2
exit 1
fi
# 安装基础依赖
install_deps() {
echo -e "${GREEN}正在安装基础依赖...${NC}"
if [ "$SYSTEM" = "debian" ]; then
apt install -y curl wget sudo
elif [ "$SYSTEM" = "fedora" ]; then
dnf install -y curl wget sudo
elif [ "$SYSTEM" = "arch" ]; then
pacman -S --noconfirm curl wget sudo
elif [ "$SYSTEM" = "opensuse" ]; then
zypper install -y curl wget sudo
else
yum install -y curl wget sudo
fi
}
# 更新系统
update_system() {
echo -e "${GREEN}正在更新系统...${NC}"
if [ "$SYSTEM" = "debian" ]; then
apt update && apt upgrade -y
else
yum update -y && yum upgrade -y
fi
}
# 安装Nginx
install_nginx() {
echo -e "${GREEN}正在安装Nginx...${NC}"
if [ "$SYSTEM" = "debian" ]; then
apt install -y nginx
systemctl start nginx
systemctl enable nginx
elif [ "$SYSTEM" = "fedora" ]; then
dnf install -y nginx
systemctl start nginx
systemctl enable nginx
elif [ "$SYSTEM" = "arch" ]; then
pacman -S --noconfirm nginx
systemctl start nginx
systemctl enable nginx
elif [ "$SYSTEM" = "opensuse" ]; then
zypper install -y nginx
systemctl start nginx
systemctl enable nginx
else
yum install -y nginx
systemctl start nginx
systemctl enable nginx
fi
}
# 安装MySQL
install_mysql() {
echo -e "${GREEN}正在安装MySQL...${NC}"
if [ "$SYSTEM" = "debian" ]; then
apt install -y mysql-server
systemctl start mysql
systemctl enable mysql
elif [ "$SYSTEM" = "fedora" ]; then
dnf install -y mysql-server
systemctl start mysqld
systemctl enable mysqld
elif [ "$SYSTEM" = "arch" ]; then
pacman -S --noconfirm mysql
systemctl start mysqld
systemctl enable mysqld
elif [ "$SYSTEM" = "opensuse" ]; then
zypper install -y mysql-server
systemctl start mysql
systemctl enable mysql
else
yum install -y mysql-server
systemctl start mysqld
systemctl enable mysqld
fi
echo -e "${YELLOW}请手动执行以下命令进行MySQL安全设置:${NC}"
echo -e "${YELLOW}sudo mysql_secure_installation${NC}"
}
# 安装PHP(CentOS使用默认源)
install_php() {
echo -e "${GREEN}正在安装PHP...${NC}"
if [ "$SYSTEM" = "debian" ]; then
apt install -y php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml
# 动态获取PHP-FPM服务名称
PHP_FPM_SERVICE=$(systemctl list-unit-files | grep -oP 'php\d\.\d-fpm\.service' | head -n 1 | sed 's/\.service//')
if [ -z "$PHP_FPM_SERVICE" ]; then
PHP_FPM_SERVICE="php-fpm" # 如果未找到特定版本,则使用默认名称
fi
elif [ "$SYSTEM" = "fedora" ]; then
dnf install -y php php-fpm php-mysqlnd php-common php-cli php-curl php-gd php-mbstring php-xml
PHP_FPM_SERVICE="php-fpm"
elif [ "$SYSTEM" = "arch" ]; then
pacman -S --noconfirm php php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml
PHP_FPM_SERVICE="php-fpm"
elif [ "$SYSTEM" = "opensuse" ]; then
zypper install -y php php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml
PHP_FPM_SERVICE="php-fpm"
else
# CentOS/RHEL使用默认源安装PHP
#yum install -y epel-release # 确保EPEL仓库已启用
yum install -y php php-fpm php-mysqlnd php-common php-cli php-curl php-gd php-mbstring php-xml
PHP_FPM_SERVICE="php-fpm" # CentOS系统通常使用php-fpm
fi
# 确保PHP-FPM配置文件正确
if [ "$SYSTEM" = "centos" ]; then
sed -i 's/listen = \/run\/php-fpm\/www.sock/listen = 127.0.0.1:9000/' /etc/php-fpm.d/www.conf
elif [ "$SYSTEM" = "fedora" ]; then
sed -i 's/listen = \/run\/php-fpm\/www.sock/listen = 127.0.0.1:9000/' /etc/php-fpm.d/www.conf
elif [ "$SYSTEM" = "arch" ]; then
sed -i 's/listen = \/run\/php-fpm\/php-fpm.sock/listen = 127.0.0.1:9000/' /etc/php/php-fpm.conf
elif [ "$SYSTEM" = "opensuse" ]; then
sed -i 's/listen = \/run\/php-fpm\/php-fpm.sock/listen = 127.0.0.1:9000/' /etc/php7/fpm/php-fpm.conf
fi
systemctl start $PHP_FPM_SERVICE
systemctl enable $PHP_FPM_SERVICE
}
# 配置Nginx
configure_nginx() {
echo -e "${GREEN}正在配置Nginx...${NC}"
CONFIG_FILE="/etc/nginx/sites-available/default"
if [ "$SYSTEM" = "centos" ]; then
CONFIG_FILE="/etc/nginx/conf.d/default.conf"
elif [ "$SYSTEM" = "fedora" ]; then
CONFIG_FILE="/etc/nginx/nginx.conf"
elif [ "$SYSTEM" = "arch" ]; then
CONFIG_FILE="/etc/nginx/nginx.conf"
elif [ "$SYSTEM" = "opensuse" ]; then
CONFIG_FILE="/etc/nginx/nginx.conf"
fi
# 检查配置文件是否存在,若不存在则创建默认配置
if [ ! -f "$CONFIG_FILE" ]; then
echo -e "${YELLOW}配置文件 $CONFIG_FILE 不存在,正在创建默认配置...${NC}"
mkdir -p $(dirname "$CONFIG_FILE")
else
# 备份原始配置
cp "$CONFIG_FILE" "$CONFIG_FILE.bak"
fi
cat > "$CONFIG_FILE" <<EOF
server {
listen 80;
server_name localhost; # 修改为更通用的 server_name
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files \$uri \$uri/ =404;
}
location ~ \.php\$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
}
}
EOF
# 针对Debian系统,修改PHP-FPM监听方式为Unix套接字
if [ "$SYSTEM" = "debian" ]; then
sed -i 's/fastcgi_pass 127.0.0.1:9000;/fastcgi_pass unix:\/run\/php\/php-fpm.sock;/' "$CONFIG_FILE"
fi
nginx -t && systemctl restart nginx
}
# 创建测试文件
create_testfile() {
echo -e "${GREEN}正在创建测试文件...${NC}"
mkdir -p /var/www/html
cat > /var/www/html/phpinfo.php <<EOF
<?php
phpinfo();
?>
EOF
# 下载PHP探针文件并重命名为index.php
wget -O /var/www/html/index.php https://api.inn-studio.com/download?id=xprober
# 设置权限
if [ "$SYSTEM" = "debian" ]; then
chown -R www-data:www-data /var/www/html
else
chown -R nginx:nginx /var/www/html
fi
chmod -R 755 /var/www/html
}
# 配置防火墙
configure_firewall() {
echo -e "${GREEN}正在配置防火墙...${NC}"
if [ "$SYSTEM" = "debian" ]; then
if ufw status | grep -q inactive; then
ufw allow 80
ufw enable
fi
elif [ "$SYSTEM" = "fedora" ]; then
if ! firewall-cmd --list-ports | grep -qw 80/tcp; then
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
fi
elif [ "$SYSTEM" = "arch" ]; then
if ! ufw status | grep -q inactive; then
ufw allow 80
ufw enable
fi
elif [ "$SYSTEM" = "opensuse" ]; then
if ! firewall-cmd --list-ports | grep -qw 80/tcp; then
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
fi
else
if ! firewall-cmd --list-ports | grep -qw 80/tcp; then
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
fi
fi
}
# 主执行流程
main() {
install_deps
update_system
install_nginx
install_mysql
install_php
configure_nginx
create_testfile
configure_firewall
# 重启服务以确保配置生效
echo -e "${GREEN}正在重启服务以确保配置生效...${NC}"
if [ "$SYSTEM" = "debian" ]; then
systemctl restart nginx
systemctl restart $PHP_FPM_SERVICE
systemctl restart mysql
else
systemctl restart nginx
systemctl restart $PHP_FPM_SERVICE
systemctl restart mysqld
fi
IP_ADDRESS=$(curl -s icanhazip.com)
echo -e "${GREEN}LNMP环境安装完成!${NC}"
echo -e "请访问:${YELLOW}http://${IP_ADDRESS}/index.php${NC}"
echo -e "不要忘记执行MySQL安全设置:${YELLOW}mysql_secure_installation${NC}"
}
main
运行完即可访问PHP指针,如果想使用可以配置MYSQL:mysql_secure_installation
各个版本取决于系统默认的版本,这种LNMP安装方法兼容性还是不错的,阿里云上的所有linux系统几乎测试完毕,绝大部分是一口气完成。
扫描二维码,手机查看
声明:部分数据/图片来源互联网,不代表欢乐你我,真实性请妥善甄别。