8-2
啟用使用者目錄
- 編輯設定檔
vi /etc/nginx/sites-available/default
- 在
server{}中加入:
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.html index.htm index.php;
autoindex on;
}
- 至此,已經支援使用者家目錄網頁。可建立新使用者並測試之。
adduser somebody
- 建立使用者網頁目錄
mkdir /home/somebody/public_html
- 建立網頁檔:
vi /home/somebody/public_html/index.html
- 其內容為:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
網頁已開通!
</body>
</html>
- 接著讓使用者加目錄網頁也可以支援PHP,編輯設定檔
vi /etc/nginx/sites-available/default
- 在
server{}中的location ~ ^/~(.+?)(/.*)?$ {上方加入:
location ~* ^/~(.+?)(/.*\.php)$ {
alias /home/$1/public_html$2;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.html index.htm index.php;
autoindex on;
}
- 重啟伺服器即可
systemctl restart nginx