在Debian系统上部署LNMP(Linux, Nginx, MySQL/MariaDB, PHP)环境并运行网站应用,可以按照以下步骤进行:
1. 更新系统
首先,确保你的系统是最新的:
sudo apt update sudo apt upgrade -y
2. 安装Nginx
sudo apt install nginx -y
安装完成后,启动Nginx并设置开机自启:
sudo systemctl start nginx sudo systemctl enable nginx
3. 安装MySQL/MariaDB
你可以选择安装MySQL或MariaDB,这里以MariaDB为例:
sudo apt install mariadb-server -y
安装完成后,启动MariaDB并设置开机自启:
sudo systemctl start mariadb sudo systemctl enable mariadb
运行安全脚本以提高安全性:
sudo mysql_secure_installation
4. 安装PHP
安装PHP及其常用扩展:
sudo apt install php-fpm php-mysql -y
配置PHP-FPM以使用Nginx:
sudo systemctl restart php7.4-fpm
编辑Nginx配置文件以处理PHP请求。打开默认站点配置文件:
sudo nano /etc/nginx/sites-available/default
修改配置文件,添加以下内容:
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; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
保存并退出编辑器,然后测试Nginx配置:
sudo nginx -t
如果没有错误,重新加载Nginx:
sudo systemctl reload nginx
5. 配置防火墙
确保防火墙允许HTTP和HTTPS流量:
sudo ufw allow 'Nginx Full'
6. 部署网站应用
将你的网站应用文件上传到/var/www/html
目录下。你可以使用FTP、SCP或其他文件传输方法。
7. 设置文件权限
确保Nginx用户(通常是www-data
)有权访问你的网站文件:
sudo chown -R www-data:www-data /var/www/html sudo find /var/www/html -type f -exec chmod 644 {} \; sudo find /var/www/html -type d -exec chmod 755 {} \;
8. 测试网站
在浏览器中访问你的域名或IP地址,确保网站正常运行。
通过以上步骤,你应该能够在Debian系统上成功部署一个LNMP环境并运行你的网站应用。如果有任何问题,请检查日志文件以获取更多信息:
- Nginx错误日志:
/var/log/nginx/error.log
- PHP错误日志:
/var/log/php7.4-fpm.log