在腾讯云轻量应用服务器上使用 CentOS 系统搭建网站,通常包括以下几个步骤:
🧩 一、准备工作
-
购买腾讯云轻量应用服务器
- 镜像选择:CentOS(建议 7.x 或 8.x)
- 开放端口:HTTP(80)、HTTPS(443)、SSH(22)
- 获取公网 IP 地址
-
连接服务器
使用 SSH 工具连接服务器(如 Xshell、FinalShell、Termius 或命令行):ssh root@你的服务器IP
🛠️ 二、安装 LAMP / LNMP 环境
✅ 推荐方式:LNMP(Nginx + MySQL + PHP)
1. 安装 Nginx
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
访问 http://你的服务器IP 测试是否看到 Nginx 欢迎页面。
2. 安装 MariaDB(MySQL 替代)
sudo yum install mariadb-server mariadb -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
3. 安装 PHP 及常用扩展
sudo yum install php php-fpm php-mysqlnd php-gd php-xml php-mbstring -y
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
修改 PHP-FPM 的监听地址(可选):
sudo sed -i 's/listen = 127.0.0.1:9000/listen = /run/php-fpm.sock/' /etc/php-fpm.d/www.conf
sudo systemctl restart php-fpm
4. 修改 Nginx 配置支持 PHP
编辑默认站点配置文件:
sudo vi /etc/nginx/conf.d/default.conf
示例配置如下(根据需要修改):
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ .php$ {
root /usr/share/nginx/html;
fastcgi_pass unix:/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
重启 Nginx:
sudo systemctl restart nginx
🌐 三、上传网站文件
将你的网站文件(HTML/PHP)上传到 /usr/share/nginx/html/ 目录下。
例如创建一个测试页面:
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/info.php
浏览器访问:http://你的服务器IP/info.php 查看 PHP 是否正常运行。
🔐 四、设置防火墙
开放 HTTP/HTTPS 端口:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
📦 五、(可选)部署 CMS(如 WordPress)
-
下载 WordPress:
cd /tmp wget https://wordpress.org/latest.tar.gz tar -zxvf latest.tar.gz sudo cp -r wordpress/* /usr/share/nginx/html/ -
创建数据库和用户:
mysql -u root -p CREATE DATABASE wordpress; CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'yourpassword'; GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost'; FLUSH PRIVILEGES; exit; -
修改 wp-config.php 中的数据库信息。
🧪 六、域名绑定与 HTTPS
- 在腾讯云控制台中绑定域名解析到服务器公网 IP。
- 使用 Let’s Encrypt 免费证书配置 HTTPS:
sudo yum install certbot python3-certbot-nginx -y sudo certbot --nginx -d yourdomain.com
✅ 总结
你现在已经完成了以下操作:
| 步骤 | 内容 |
|---|---|
| 1 | 连接并配置 CentOS 系统 |
| 2 | 安装 LNMP 环境 |
| 3 | 设置网站目录与 PHP 解析 |
| 4 | 域名解析与 HTTPS 配置 |
如果你有具体的网站类型(如 WordPress、Discuz、Typecho 等),可以告诉我,我可以提供更详细的部署教程。欢迎继续提问!
云计算HECS