在阿里云轻量应用服务器上部署 Web 服务器是一个常见的需求,适合搭建博客、企业官网、小型管理系统等。下面以 CentOS 系统为例,介绍如何在阿里云轻量应用服务器上部署一个基本的 Web 服务器(Nginx + PHP + MySQL 或 Nginx + 静态网站)。
🧩 一、准备工作
1. 购买阿里云轻量应用服务器
- 登录 阿里云控制台
- 进入【轻量应用服务器】页面,选择合适的配置(推荐 CentOS 系统)
- 设置登录密码或使用密钥对
2. 开放端口
进入服务器详情页,在【防火墙】中开放以下端口:
80:HTTP 访问443:HTTPS 访问(可选)22:SSH 登录(默认已开)
🚀 二、部署 Web 服务器(以 LNMP 为例)
LNMP = Linux + Nginx + MySQL + PHP
步骤 1:连接服务器(通过 SSH)
ssh root@你的服务器公网IP
步骤 2:安装 Nginx
# 安装 EPEL 源
yum install -y epel-release
# 安装 Nginx
yum install -y nginx
# 启动 Nginx 并设置开机自启
systemctl start nginx
systemctl enable nginx
访问你的服务器 IP 地址(如 http://123.123.123.123),看到欢迎页面表示 Nginx 成功运行。
步骤 3:安装 MySQL(MariaDB)
# 安装 MariaDB
yum install -y mariadb-server mariadb
# 启动 MariaDB 并设置开机自启
systemctl start mariadb
systemctl enable mariadb
# 初始化数据库安全设置
mysql_secure_installation
按提示设置 root 密码等信息。
步骤 4:安装 PHP 及常用模块
# 安装 PHP 和常见扩展
yum install -y php php-fpm php-mysqlnd php-curl php-gd php-mbstring php-xml php-json
# 启动 PHP-FPM 并设置开机自启
systemctl start php-fpm
systemctl enable php-fpm
步骤 5:配置 Nginx 支持 PHP
编辑默认站点配置:
vim /etc/nginx/conf.d/default.conf
修改内容如下(重点是处理 .php 文件):
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
# 将 .php 请求转发给 php-fpm 处理
location ~ .php$ {
root /usr/share/nginx/html;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
重启 Nginx:
systemctl restart nginx
步骤 6:测试 PHP 是否正常工作
创建测试文件:
echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php
浏览器访问:http://你的服务器IP/info.php
如果显示 PHP 信息页面,说明配置成功。
📁 三、上传自己的网站文件
你可以将你的网站代码上传到 /usr/share/nginx/html/ 目录下,例如:
scp -r 本地网站目录/* root@你的服务器IP:/usr/share/nginx/html/
或者使用 FTP 工具(如 FileZilla)、宝塔面板等方式管理文件。
🔒 四、配置域名和 HTTPS(可选)
1. 域名解析
- 在阿里云域名控制台,将域名 A 记录解析到服务器公网 IP
2. 使用 Let’s Encrypt 免费证书(SSL)
推荐使用 acme.sh 自动申请证书:
# 安装 acme.sh
curl https://get.acme.sh | sh
# 申请证书(替换为你的域名)
~/.acme.sh/acme.sh --issue -d yourdomain.com --webroot /usr/share/nginx/html
# 安装证书
~/.acme.sh/acme.sh --installcert -d yourdomain.com
--key-file /etc/nginx/ssl/yourdomain.key
--fullchain-file /etc/nginx/ssl/yourdomain.crt
# 创建 SSL 目录
mkdir -p /etc/nginx/ssl
3. 修改 Nginx 配置启用 HTTPS
编辑配置文件:
vim /etc/nginx/conf.d/default.conf
添加 HTTPS 的 server 块:
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /etc/nginx/ssl/yourdomain.crt;
ssl_certificate_key /etc/nginx/ssl/yourdomain.key;
location / {
root /usr/share/nginx/html;
index index.php index.html;
}
location ~ .php$ {
root /usr/share/nginx/html;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 强制跳转 HTTPS(可选)
server {
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}
重启 Nginx:
systemctl restart nginx
✅ 五、其他建议
- 使用宝塔面板简化操作(官网下载安装脚本)
- 定期备份网站数据
- 配置防火墙或安全组限制访问
- 使用 CDN 提升访问速度(如阿里云 CDN)
🧾 总结
| 组件 | 作用 |
|---|---|
| Nginx | Web 服务器,负责处理 HTTP 请求 |
| PHP-FPM | 解析执行 PHP 脚本 |
| MariaDB | 数据库,存储网站数据 |
| Let’s Encrypt | 提供免费 HTTPS 证书 |
如果你只是部署一个静态网站(HTML/CSS/JS),可以只安装 Nginx,不需要 PHP 和 MySQL。
需要我帮你写一个一键安装脚本?还是你想用宝塔面板?我可以继续指导你!
云计算导航