在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
为了安全起见,运行以下命令进行安全配置:
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 {
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:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
保存并退出编辑器,然后测试Nginx配置并重启服务:
sudo nginx -t
sudo systemctl restart nginx
在 /var/www/html
目录下创建一个测试PHP文件:
sudo nano /var/www/html/index.php
添加以下内容:
<?php
phpinfo();
?>
保存并退出编辑器,然后在浏览器中访问你的服务器IP地址,应该能看到PHP信息页面。
如果你启用了防火墙(如ufw),确保允许HTTP和HTTPS流量:
sudo ufw allow 'Nginx Full'
你可以使用Let’s Encrypt来安装免费的SSL证书:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com
按照提示完成证书的安装和配置。
完成以上步骤后,你就成功在Debian上配置了一个基本的LNMP环境。根据你的具体需求,你可能还需要进行进一步的定制和优化。