首页  >  文章  >  数据库  >  在 ECith Nginx、MySQL、PHP 和 Git 上设置 PHP 网站

在 ECith Nginx、MySQL、PHP 和 Git 上设置 PHP 网站

PHPz
PHPz原创
2024-07-28 14:49:33962浏览

Setting Up a PHP Website on ECith Nginx, MySQL, PHP, and Git

本指南将引导您完成在 Amazon EC2 实例上设置 PHP 网站的过程,使用 Nginx 作为 Web 服务器,MySQL 作为数据库,PHP 用于服务器端脚本,和 Git 用于版本控制。我们将涵盖从初始设置到常见问题故障排除的所有内容。

目录

  1. 启动 EC2 实例
  2. 连接到您的 EC2 实例
  3. 更新和升级系统
  4. 安装 Nginx
  5. 安装MySQL
  6. 安装 PHP
  7. 安装 Git
  8. 配置 Nginx
  9. 设置您的网站目录
  10. 克隆您的存储库
  11. 设置正确的权限
  12. 配置 PHP
  13. 设置 SSL(可选但推荐)
  14. 常见问题故障排除
  15. 最佳实践和安全注意事项

1.启动EC2实例

  1. 登录您的 AWS 管理控制台。
  2. 导航到 EC2 并单击“启动实例”。
  3. 选择 Ubuntu Server AMI(例如 Ubuntu Server 22.04 LTS)。
  4. 选择实例类型(t2.micro 有资格享受免费套餐)。
  5. 根据需要配置实例详细信息、添加存储和标签。
  6. 配置安全组以允许 SSH(端口 22)、HTTP(端口 80)和 HTTPS(端口 443)流量。
  7. 查看并启动实例,选择或创建密钥对。

2. 连接到您的 EC2 实例

使用 SSH 连接到您的实例:

ssh -i /path/to/your-key.pem ubuntu@your-instance-public-dns

将 /path/to/your-key.pem 替换为您的密钥文件的路径,并将 your-instance-public-dns 替换为您实例的公共 DNS 名称。

3. 系统更新升级

连接后,更新和升级您的系统:

sudo apt update
sudo apt upgrade -y

4.安装Nginx

安装 Nginx 网络服务器:

sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx

验证 Nginx 是否正在运行:

sudo systemctl status nginx

5.安装MySQL

安装 MySQL 服务器:

sudo apt install mysql-server -y
sudo systemctl start mysql
sudo systemctl enable mysql

保护您的 MySQL 安装:

sudo mysql_secure_installation

按照提示设置 root 密码并删除不安全的默认设置。

6.安装PHP

我们将安装 PHP 8.1(或 Ubuntu 存储库中可用的最新稳定版本):

sudo apt install php8.1-fpm php8.1-mysql php8.1-common php8.1-cli php8.1-curl php8.1-mbstring php8.1-xml php8.1-zip -y

验证 PHP 安装:

php -v

7.安装Git

安装 Git 进行版本控制:

sudo apt install git -y

验证 Git 安装:

git --version

8.配置Nginx

创建新的 Nginx 服务器块配置:

sudo nano /etc/nginx/sites-available/your_domain

添加以下配置(将 your_domain 替换为您的实际域名或 IP 地址):

server {
    listen 80;
    server_name your_domain www.your_domain;
    root /var/www/your_domain;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

启用新站点:

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

测试 Nginx 配置:

sudo nginx -t

如果测试成功,重新加载Nginx:

sudo systemctl reload nginx

9. 设置您的网站目录

创建网站根目录:

sudo mkdir -p /var/www/your_domain
sudo chown -R $USER:$USER /var/www/your_domain
sudo chmod -R 755 /var/www/your_domain

10.克隆你的存储库

如果您的网站已有 Git 存储库,请将其克隆到您的 Web 根目录中:

cd /var/www/your_domain
git clone https://github.com/your-username/your-repo.git .

将 https://github.com/your-username/your-repo.git 替换为您的实际存储库 URL。

如果您要开始一个新项目,请初始化一个新的 Git 存储库:

cd /var/www/your_domain
git init

11.设置正确的权限

为您的网络文件设置正确的权限:

sudo chown -R www-data:www-data /var/www/your_domain
sudo find /var/www/your_domain -type d -exec chmod 755 {} \;
sudo find /var/www/your_domain -type f -exec chmod 644 {} \;

允许 Ubuntu 用户管理文件:

sudo usermod -a -G www-data ubuntu
sudo chmod g+s /var/www/your_domain

您可能需要注销并重新登录才能使组更改生效。

12.配置PHP

根据需要调整 PHP 设置:

sudo nano /etc/php/8.1/fpm/php.ini

要调整的常用设置:

upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
memory_limit = 256M

进行更改后,重新启动 PHP-FPM:

sudo systemctl restart php8.1-fpm

13. 设置 SSL(可选但推荐)

要使用 HTTPS 保护您的网站,您可以使用 Let's Encrypt:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain -d www.your_domain

按照提示设置 SSL。

14. 常见问题故障排除

权限被拒绝错误

如果您在 Nginx 错误日志中遇到“权限被拒绝”错误:

  1. 检查文件所有权:
   ls -l /var/www/your_domain
  1. 确保 Nginx 以正确的用户身份运行:
   ps aux | grep nginx
  1. 检查 Nginx 配置:
   sudo nano /etc/nginx/nginx.conf

确保用户设置为 www-data。

PHP 错误

与 PHP 相关的错误:

  1. 检查 PHP-FPM 日志:
   sudo tail -f /var/log/php8.1-fpm.log
  1. 确保 PHP-FPM 正在运行:
   sudo systemctl status php8.1-fpm
  1. 验证 PHP-FPM 套接字文件是否存在:
   ls /var/run/php/php8.1-fpm.sock

Git 问题

如果您遇到 Git 权限问题:

  1. Ensure the .git directory is owned by your user:
   sudo chown -R ubuntu:ubuntu /var/www/your_domain/.git
  1. Use sudo for Git operations or temporarily change ownership:
   sudo chown -R ubuntu:ubuntu /var/www/your_domain
   git pull
   sudo chown -R www-data:www-data /var/www/your_domain

15. Best Practices and Security Considerations

  1. Regularly update your system and software:
   sudo apt update && sudo apt upgrade -y
  1. Use strong passwords for all services (MySQL, SSH, etc.).

  2. Configure a firewall (e.g., UFW) to restrict incoming traffic:

   sudo ufw allow OpenSSH
   sudo ufw allow 'Nginx Full'
   sudo ufw enable
  1. Implement fail2ban to protect against brute-force attacks:
   sudo apt install fail2ban -y
   sudo systemctl start fail2ban
   sudo systemctl enable fail2ban
  1. Regularly backup your website and database.

  2. Monitor your server logs for unusual activity:

   sudo tail -f /var/log/nginx/access.log
   sudo tail -f /var/log/nginx/error.log
  1. Use version control (Git) for all your code changes.

  2. Implement proper error handling and logging in your PHP application.

  3. Use prepared statements or ORM to prevent SQL injection attacks.

  4. Keep your application dependencies up-to-date and use a dependency manager like Composer for PHP projects.

By following this guide, you should have a fully functional PHP website running on an EC2 instance with Nginx, MySQL, and Git.
Remember to adapt the instructions to your specific needs and always prioritize security in your setup.

以上是在 ECith Nginx、MySQL、PHP 和 Git 上设置 PHP 网站的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn