在 CentOS LNMP(Linux, Nginx, MySQL, PHP)環境中添加新站點,您需要按照以下步驟操作:
創建一個新的站點配置文件:
在 /etc/nginx/conf.d/ 目錄下創建一個新的配置文件,例如 yourdomain.com.conf。使用文本編輯器打開文件,如 vi 或 nano:
sudo vi /etc/nginx/conf.d/yourdomain.com.conf
編輯站點配置文件: 在新創建的配置文件中,輸入以下內容并根據您的實際情況進行修改:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
請將 yourdomain.com 替換為您的實際域名,將 /var/www/yourdomain.com 替換為您希望站點文件存放的目錄。
創建站點根目錄:
使用 mkdir 命令創建站點根目錄:
sudo mkdir -p /var/www/yourdomain.com
設置站點權限: 更改站點根目錄的所有者和權限,以便 Nginx 可以訪問文件:
sudo chown -R nginx:nginx /var/www/yourdomain.com
sudo chmod -R 755 /var/www/yourdomain.com
創建一個簡單的 HTML 頁面:
在站點根目錄中創建一個名為 index.html 的文件,用于測試新站點是否正常工作:
sudo vi /var/www/yourdomain.com/index.html
輸入以下內容:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Your Domain</title>
</head>
<body>
<h1>Hello, this is your new site!</h1>
</body>
</html>
重啟 Nginx 服務: 為了使新的站點配置生效,需要重啟 Nginx 服務:
sudo systemctl restart nginx
現在,您應該可以通過訪問 http://yourdomain.com 在瀏覽器中查看新添加的 CentOS LNMP 站點。