在云主机上部署 PHP 网站涉及多个步骤,包括环境配置、代码上传、数据库设置等。以下是详细的步骤指南:
一、准备工作
-
购买云主机
- 选择云服务提供商(如阿里云、腾讯云、AWS、Google Cloud 等)。
- 根据需求选择配置(CPU、内存、带宽等)。
- 获取云主机的 IP 地址、用户名和密码(或 SSH 密钥)。
-
域名解析
- 如果已有域名,将域名解析到云主机的 IP 地址(通过 DNS 设置 A 记录)。
二、环境配置
-
连接云主机
- 使用 SSH 工具(如 Xshell、Putty 或终端)连接到云主机:
ssh root@你的云主机IP
- 如果使用密钥,需指定密钥文件:
ssh -i /path/to/private_key root@你的云主机IP
- 使用 SSH 工具(如 Xshell、Putty 或终端)连接到云主机:
-
安装必要的软件
- 更新系统:
sudo apt update && sudo apt upgrade -y # Ubuntu/Debian sudo yum update -y # CentOS
- 安装 Web 服务器(如 Nginx 或 Apache):
- Nginx 示例:
sudo apt install nginx -y # Ubuntu/Debian sudo yum install nginx -y # CentOS
- Nginx 示例:
- 安装 PHP:
- 安装 PHP 及常用扩展(如 MySQL、GD 库等):
sudo apt install php php-fpm php-mysql php-gd -y # Ubuntu/Debian sudo yum install php php-fpm php-mysqlnd php-gd -y # CentOS
- 安装 PHP 及常用扩展(如 MySQL、GD 库等):
- 安装数据库(如 MySQL 或 MariaDB):
sudo apt install mysql-server -y # Ubuntu/Debian sudo yum install mariadb-server -y # CentOS
- 更新系统:
-
配置 Web 服务器
-
Nginx 配置示例:
-
编辑 Nginx 配置文件(如
/etc/nginx/sites-available/default
):server { listen 80; server_name 你的域名或IP; root /var/www/html; index index.php index.html; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据 PHP 版本调整 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
- 测试配置并重启 Nginx:
sudo nginx -t sudo systemctl restart nginx
-
-
三、上传 PHP 网站代码
-
通过 SFTP 上传代码
- 使用 SFTP 工具(如 FileZilla)将本地 PHP 代码上传到云主机的
/var/www/html
目录。
- 使用 SFTP 工具(如 FileZilla)将本地 PHP 代码上传到云主机的
-
通过 Git 部署(可选)
- 在云主机上安装 Git:
sudo apt install git -y
- 克隆代码仓库到 Web 根目录:
git clone https://github.com/你的仓库地址.git /var/www/html
- 在云主机上安装 Git:
四、配置数据库
-
初始化数据库
- 启动 MySQL/MariaDB 服务:
sudo systemctl start mysql # 或 mariadb
- 运行安全配置脚本(可选):
sudo mysql<em>secure</em>installation
- 登录数据库:
sudo mysql -u root -p
- 创建数据库和用户:
CREATE DATABASE your<em>database</em>name; CREATE USER 'your<em>username'@'localhost' IDENTIFIED BY 'your</em>password'; GRANT ALL PRIVILEGES ON your<em>database</em>name.* TO 'your_username'@'localhost'; FLUSH PRIVILEGES;
- 启动 MySQL/MariaDB 服务:
-
导入数据库
- 如果网站有 SQL 文件,使用以下命令导入:
mysql -u your<em>username -p your</em>database<em>name < /path/to/your</em>database.sql
- 如果网站有 SQL 文件,使用以下命令导入:
五、测试网站
-
访问网站
- 在浏览器中访问
http://你的域名或IP
,检查网站是否正常显示。
- 在浏览器中访问
-
检查错误日志
- 如果网站无法访问,查看 Nginx 或 PHP 错误日志:
sudo tail -f /var/log/nginx/error.log sudo tail -f /var/log/php7.4-fpm.log # 根据 PHP 版本调整
- 如果网站无法访问,查看 Nginx 或 PHP 错误日志:
六、优化与安全
-
设置文件权限
- 确保 Web 根目录的权限正确:
sudo chown -R www-data:www-data /var/www/html # Ubuntu/Debian sudo chown -R nginx:nginx /var/www/html # CentOS sudo chmod -R 755 /var/www/html
- 确保 Web 根目录的权限正确:
-
启用 HTTPS
- 使用 Let’s Encrypt 免费证书:
sudo apt install certbot python3-certbot-nginx -y # Ubuntu/Debian sudo certbot --nginx -d 你的域名
- 使用 Let’s Encrypt 免费证书:
-
防火墙设置
- 仅开放必要的端口(如 80、443):
sudo ufw allow 'Nginx Full' # Ubuntu sudo firewall-cmd --permanent --add-service=http --add-service=https && sudo firewall-cmd --reload # CentOS
- 仅开放必要的端口(如 80、443):
常见问题
-
网站显示 404 错误
- 检查 Nginx 配置中的
root
路径是否正确。 - 确保 PHP 文件存在且权限正确。
- 检查 Nginx 配置中的
-
数据库连接失败
- 检查数据库用户名、密码和主机名是否正确。
- 确保数据库服务已启动。
-
PHP 版本不匹配
- 确保云主机上的 PHP 版本与网站代码兼容。
通过以上步骤,您可以在云主机上成功部署 PHP 网站。如果遇到问题,建议逐步排查日志并参考官方文档或社区支持。