在Debian上配置LNMP(Linux, Nginx, MySQL, PHP)的多站点访问,可以按照以下步骤进行:
首先,确保你的系统是最新的,并安装Nginx、MySQL和PHP。
sudo apt update
sudo apt upgrade
sudo apt install nginx mysql-server php-fpm php-mysql
登录到MySQL并创建数据库和用户。
sudo mysql -u root -p
在MySQL shell中:
CREATE DATABASE site1;
CREATE DATABASE site2;
CREATE USER 'site1user'@'localhost' IDENTIFIED BY 'password';
CREATE USER 'site2user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON site1.* TO 'site1user'@'localhost';
GRANT ALL PRIVILEGES ON site2.* TO 'site2user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
为每个站点创建一个Nginx配置文件。
创建一个新的配置文件:
sudo nano /etc/nginx/sites-available/site1
添加以下内容:
server {
listen 80;
server_name site1.example.com;
root /var/www/site1;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
创建站点目录并设置权限:
sudo mkdir -p /var/www/site1
sudo chown -R www-data:www-data /var/www/site1
创建一个PHP文件测试:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/site1/index.php
创建另一个配置文件:
sudo nano /etc/nginx/sites-available/site2
添加以下内容:
server {
listen 80;
server_name site2.example.com;
root /var/www/site2;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
创建站点目录并设置权限:
sudo mkdir -p /var/www/site2
sudo chown -R www-data:www-data /var/www/site2
创建一个PHP文件测试:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/site2/index.php
创建符号链接以启用这些站点配置:
sudo ln -s /etc/nginx/sites-available/site1 /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/site2 /etc/nginx/sites-enabled/
测试Nginx配置是否正确:
sudo nginx -t
如果没有错误,重启Nginx以应用更改:
sudo systemctl restart nginx
确保你的域名(例如 site1.example.com 和 site2.example.com)指向你的服务器IP地址。你可以在你的DNS提供商的管理界面中进行配置。
确保防火墙允许HTTP(80)和HTTPS(443)流量:
sudo ufw allow 'Nginx Full'
打开浏览器并访问 http://site1.example.com 和 http://site2.example.com,你应该能够看到各自的PHP信息页面。
通过以上步骤,你就可以在Debian上成功配置LNMP的多站点访问。