在Debian系统上配置LNMP(Linux, Nginx, MySQL, PHP)的虚拟主机,可以按照以下步骤进行:
首先,确保你已经安装了Nginx、MySQL和PHP。如果没有安装,可以使用以下命令进行安装:
sudo apt update
sudo apt install nginx mysql-server php-fpm php-mysql
登录到MySQL并创建一个新的数据库和用户:
sudo mysql -u root -p
在MySQL shell中执行以下命令:
CREATE DATABASE your_database_name;
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
EXIT;
编辑PHP-FPM配置文件 /etc/php/7.4/fpm/pool.d/www.conf
(根据你的PHP版本调整路径),确保监听地址设置为 127.0.0.1:9000
:
listen = 127.0.0.1:9000
然后重启PHP-FPM服务:
sudo systemctl restart php7.4-fpm
创建一个新的Nginx配置文件来处理你的虚拟主机。例如,创建一个名为 yourdomain.com.conf
的文件在 /etc/nginx/sites-available/
目录下:
sudo nano /etc/nginx/sites-available/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;
}
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;
}
location ~ /\.ht {
deny all;
}
}
保存并关闭文件。
创建一个符号链接将配置文件链接到 sites-enabled
目录:
sudo ln -s /etc/nginx/sites-available/yourdomain.com.conf /etc/nginx/sites-enabled/
测试Nginx配置文件是否有语法错误:
sudo nginx -t
如果没有错误,重新加载Nginx服务:
sudo systemctl reload nginx
确保防火墙允许HTTP和HTTPS流量:
sudo ufw allow 'Nginx Full'
如果你在本地测试,可以在 /etc/hosts
文件中添加你的域名:
127.0.0.1 yourdomain.com www.yourdomain.com
创建你的网站根目录并设置权限:
sudo mkdir -p /var/www/yourdomain.com
sudo chown -R www-data:www-data /var/www/yourdomain.com
在网站根目录下创建一个简单的PHP文件来测试:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/yourdomain.com/index.php
现在,你应该能够在浏览器中访问 http://yourdomain.com
并看到PHP信息页面。
通过以上步骤,你已经成功在Debian系统上配置了一个LNMP虚拟主机。