在Debian系统上搭建LNMP(Linux, Nginx, MySQL/MariaDB, PHP)环境是一个相对简单的过程。以下是详细的搭建步骤:
首先,确保你的系统是最新的:
sudo apt update
sudo apt upgrade -y
安装Nginx作为Web服务器:
sudo apt install nginx -y
安装完成后,启动Nginx服务并设置开机自启动:
sudo systemctl start nginx
sudo systemctl enable nginx
验证Nginx是否安装成功,可以在浏览器中访问服务器的IP地址或域名。
你可以选择安装MySQL或MariaDB。这里以MariaDB为例:
sudo apt install mariadb-server -y
安装过程中会要求设置root密码,按照提示操作即可。安装完成后,启动MariaDB服务并设置开机自启动:
sudo systemctl start mariadb
sudo systemctl enable mariadb
为了安全起见,建议运行以下命令进行数据库的安全加固:
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
注意:根据你的Debian版本和PHP版本,可能需要调整PHP-FPM的版本号。
编辑Nginx的默认配置文件:
sudo nano /etc/nginx/sites-available/default
在server块中添加以下内容:
server {
listen 80;
server_name your_domain_or_ip;
root /var/www/html;
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;
}
}
保存并退出编辑器,然后测试Nginx配置是否正确:
sudo nginx -t
如果没有错误,重新加载Nginx以应用更改:
sudo systemctl reload nginx
在服务器的/var/www/html目录下创建一个info.php文件,并写入以下内容:
<?php
phpinfo();
?>
在浏览器中访问http://your_server_ip/info.php,如果能看到PHP的详细信息页面,则表示LNMP环境搭建成功。
根据需要,你可以安装其他软件,如WordPress、Drupal等。
如果你启用了防火墙,确保允许HTTP和HTTPS流量:
sudo ufw allow 'Nginx Full'
sudo ufw enable
完成以上步骤后,你就成功在Debian系统上搭建了一个基本的LNMP环境。