在Debian系統上配置PHP虛擬主機,通常需要以下幾個步驟:
對于Apache:
sudo apt-get update
sudo apt-get install apache2
對于Nginx:
sudo apt-get update
sudo apt-get install nginx
sudo apt-get install php7.4 php7.4-cli php7.4-fpm php7.4-mysql php7.4-json php7.4-opcache php7.4-mbstring php7.4-xml
/etc/php/7.4/fpm/pool.d/www.conf
文件,找到listen
行,將其更改為:listen = /run/php/php7.4-fpm.sock
然后重啟PHP-FPM服務:
sudo systemctl restart php7.4-fpm
/etc/apache2/sites-available
目錄下創建一個新文件,例如yourdomain.com.conf
:sudo nano /etc/apache2/sites-available/yourdomain.com.conf
對于Nginx,在/etc/nginx/sites-available
目錄下創建一個新文件,例如yourdomain.com
:
sudo nano /etc/nginx/sites-available/yourdomain.com
對于Apache:
<VirtualHost *:80>
ServerAdmin webmaster@yourdomain.com
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/yourdomain.com
<Directory /var/www/yourdomain.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
對于Nginx:
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;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
sudo a2ensite yourdomain.com.conf
然后重啟Apache服務:
sudo systemctl restart apache2
對于Nginx,創建一個符號鏈接以啟用新的虛擬主機配置:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
然后重啟Nginx服務:
sudo systemctl restart nginx
/etc/hosts
文件,添加以下內容:127.0.0.1 yourdomain.com www.yourdomain.com
現在,你應該可以在瀏覽器中訪問你的域名,并看到你的PHP應用程序。