在 ECith Nginx、MySQL、PHP 和 Git 上設定 PHP 網站

PHPz
發布: 2024-07-28 14:49:33
原創
926 人瀏覽過

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中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!