插件窝 干货文章 一键安装:CentOS上快速搭建web服务器的最新方法

一键安装:CentOS上快速搭建web服务器的最新方法

安装 install 一键 bash 743    来源:    2025-04-12

CentOS上快速搭建Web服务器的一键安装方法

最新推荐方法:使用自动安装脚本

以下是在CentOS 7/8上快速搭建Web服务器(LAMP/LEMP)的最新一键安装方法:

1. LAMP (Apache + MySQL/MariaDB + PHP) 一键安装

curl -s https://raw.githubusercontent.com/teddysun/lamp/master/lamp.sh | bash

或者:

wget --no-check-certificate https://lamp.sh/lamp.sh && chmod +x lamp.sh && ./lamp.sh

2. LEMP (Nginx + MySQL/MariaDB + PHP) 一键安装

curl -s https://raw.githubusercontent.com/teddysun/lamp/master/lemp.sh | bash

或者:

wget --no-check-certificate https://lamp.sh/lemp.sh && chmod +x lemp.sh && ./lemp.sh

替代方案:使用Web管理面板

1. 宝塔面板 (推荐)

# CentOS 7+ 安装命令
yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh

2. Webmin/Virtualmin

# 安装Webmin
cat <<EOF > /etc/yum.repos.d/webmin.repo
[Webmin]
name=Webmin Distribution Neutral
baseurl=https://download.webmin.com/download/yum
enabled=1
gpgcheck=1
gpgkey=https://download.webmin.com/download/yum/RPM-GPG-KEY-webmin
EOF

yum install webmin -y

传统分步安装方法(备用)

如果一键脚本不适用,可以使用以下分步命令:

1. 安装Apache

yum install httpd -y
systemctl start httpd
systemctl enable httpd

2. 安装MySQL/MariaDB

# MariaDB (推荐)
yum install mariadb-server mariadb -y
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation

3. 安装PHP

# CentOS 7
yum install epel-release yum-utils -y
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
yum-config-manager --enable remi-php74
yum install php php-mysqlnd php-gd php-xml php-mbstring -y

# CentOS 8
dnf install php php-mysqlnd php-gd php-xml php-mbstring -y

验证安装

# 创建测试页面
echo "<?php phpinfo(); ?>" > /var/www/html/info.php

# 重启服务
systemctl restart httpd

然后在浏览器中访问:http://你的服务器IP/info.php

安全建议

安装完成后,请执行以下安全措施: 1. 配置防火墙: bash firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --reload 2. 删除测试页面: bash rm -f /var/www/html/info.php 3. 定期更新系统: bash yum update -y

这些方法提供了从最简单到最灵活的不同选择,您可以根据自己的需求选择最适合的方式。