在腾讯云轻量应用服务器上使用 CentOS 搭建网站,主要包括以下几个步骤:
🧩 一、准备工作
1. 购买腾讯云轻量应用服务器
- 登录 腾讯云控制台
- 进入【轻量应用服务器】页面
- 创建实例时选择:
- 镜像:
CentOS(推荐 CentOS 7 或 CentOS Stream) - 地域:选离你目标用户近的地区
- 系统盘:至少 40GB(建议)
- 镜像:
2. 获取公网 IP 和登录信息
- 实例创建完成后,在详情页可以看到公网 IP
- 使用 SSH 登录服务器(如使用 Windows 可用 Xshell、Putty;Mac/Linux 可直接使用终端)
ssh root@你的公网IP
🛠️ 二、安装 LAMP / LNMP 环境(以 LNMP 为例)
LNMP = Linux + Nginx + MySQL + PHP(适用于搭建 WordPress、Discuz 等常见网站)
1. 安装 EPEL 源(CentOS 所需)
yum install epel-release -y
2. 安装 Nginx
yum install nginx -y
systemctl start nginx
systemctl enable nginx
访问 http://你的公网IP 查看是否显示 Nginx 默认页面。
3. 安装 MariaDB(MySQL 的替代品)
yum install mariadb-server mariadb -y
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
设置数据库 root 密码等安全选项。
4. 安装 PHP 及相关模块
yum install php php-fpm php-mysqlnd php-curl php-gd php-mbstring php-xml -y
编辑 PHP-FPM 配置文件(可选):
vim /etc/php-fpm.d/www.conf
修改以下内容(确保和 Nginx 权限一致):
user = nginx
group = nginx
listen.owner = nobody
listen.group = nobody
启动并启用 PHP-FPM:
systemctl start php-fpm
systemctl enable php-fpm
5. 修改 Nginx 配置支持 PHP
编辑默认站点配置文件:
vim /etc/nginx/conf.d/default.conf
将 location / 改为如下内容,并添加对 .php 文件的支持:
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ .php$ {
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
🌐 三、上传网站文件或安装 CMS
方法一:手动上传静态网页
将 HTML 文件上传到 /usr/share/nginx/html/ 目录下即可。
方法二:安装 WordPress(示例)
1. 下载 WordPress
cd /tmp
wget https://cn.wordpress.org/latest-zh_CN.tar.gz
tar -zxvf latest-zh_CN.tar.gz
cp -r wordpress/* /usr/share/nginx/html/
chown -R nginx:nginx /usr/share/nginx/html/*
2. 创建 WordPress 数据库
mysql -u root -p
执行以下 SQL 命令:
CREATE DATABASE wordpress;
CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost';
FLUSH PRIVILEGES;
exit
3. 配置 WordPress
浏览器访问:http://你的公网IP,按照提示填写数据库信息完成安装。
🔐 四、开放防火墙端口
确保服务器的安全组中已放行以下端口:
- HTTP:80
- HTTPS:443(如使用 SSL)
- SSH:22(默认已开)
在 CentOS 上还需开启防火墙对应端口:
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
✅ 五、绑定域名(可选)
- 在腾讯云 DNS 控制台解析域名 A 记录指向服务器公网 IP。
- 修改 Nginx 配置中的
server_name example.com; - 重启 Nginx
📦 六、安装 SSL 证书(可选)
可以使用 Let’s Encrypt 免费证书:
yum install certbot python2-certbot-nginx -y
certbot --nginx -d yourdomain.com
🧪 七、测试网站是否正常运行
浏览器访问你的域名或公网 IP,查看是否能正常打开网站。
📝 总结
| 步骤 | 内容 |
|---|---|
| 1 | 登录服务器,安装 LNMP 环境 |
| 2 | 配置 Nginx 支持 PHP |
| 3 | 创建数据库,部署网站代码 |
| 4 | 开放防火墙和安全组端口 |
| 5 | 绑定域名与 SSL(可选) |
如果你有具体的网站类型(如 WordPress、Typecho、Discuz),我可以提供更详细的部署指南。需要的话请告诉我!
云计算导航