在Debian上部署LNMP(Linux, Nginx, MySQL/MariaDB, PHP)项目涉及多个步骤。以下是一个基本的指南,帮助你在Debian系统上设置和配置这些组件。
首先,确保你的系统是最新的:
sudo apt update
sudo apt upgrade -y
安装Nginx作为Web服务器:
sudo apt install nginx -y
启动并启用Nginx服务:
sudo systemctl start nginx
sudo systemctl enable nginx
你可以选择安装MySQL或MariaDB。这里以MariaDB为例:
sudo apt install mariadb-server mariadb-client -y
启动并启用MariaDB服务:
sudo systemctl start mariadb
sudo systemctl enable mariadb
运行安全脚本以设置root密码和其他安全选项:
sudo mysql_secure_installation
安装PHP及其常用扩展:
sudo apt install php-fpm php-mysql -y
启动并启用PHP-FPM服务:
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
编辑Nginx的默认站点配置文件:
sudo nano /etc/nginx/sites-available/default
将以下内容添加到server
块中:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
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;
}
}
保存并退出编辑器,然后测试Nginx配置:
sudo nginx -t
重新加载Nginx以应用更改:
sudo systemctl reload nginx
创建一个目录来存放你的项目文件:
sudo mkdir -p /var/www/html/your_project
sudo chown -R www-data:www-data /var/www/html/your_project
将你的项目文件上传到/var/www/html/your_project
目录中。你可以使用FTP、SCP或其他文件传输方法。
登录到MariaDB并创建一个新的数据库和用户:
sudo mysql -u root -p
在MySQL shell中执行以下命令:
CREATE DATABASE your_database_name;
CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
在你的项目文件中,确保PHP代码正确配置了数据库连接信息。例如,在config.php
文件中:
<?php
$servername = "localhost";
$username = "your_user";
$password = "your_password";
$dbname = "your_database_name";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
打开浏览器并访问你的服务器IP地址或域名,你应该能够看到你的项目运行正常。
通过以上步骤,你已经在Debian上成功部署了一个LNMP项目。根据具体需求,你可能还需要进行更多的配置和优化。