debian

Debian LNMP如何部署网站

小樊
34
2025-04-09 22:10:57
栏目: 云计算

在Debian系统上部署LNMP(Linux, Nginx, MySQL/MariaDB, PHP)环境是一个相对简单的过程。以下是详细的步骤:

1. 更新系统软件包列表

首先,确保你的系统软件包列表是最新的:

sudo apt update

2. 安装Nginx

安装最新版本的Nginx:

sudo apt install nginx

安装完成后,启动Nginx并设置它在系统启动时自动启动:

sudo systemctl start nginx
sudo systemctl enable nginx

验证Nginx是否正常运行:

curl -I http://your_server_ip

你应该会看到类似以下的信息:

HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Mon, 31 Oct 2024 10:00:00 GMT
Content-Type: text/html
Content-Length: 1234
Connection: keep-alive

3. 安装MariaDB

在Debian中,MySQL已经被MariaDB取代。安装MariaDB:

sudo apt install mariadb-server mariadb-client

在安装过程中,系统会提示你设置MariaDB的root用户密码,请确保设置一个强密码。

安装完成后,启动MariaDB并设置它在系统启动时自动启动:

sudo systemctl start mariadb
sudo systemctl enable mariadb

登录到MariaDB并设置密码(如果需要):

sudo mysql -u root -p

按照提示设置新密码。

4. 安装PHP

安装PHP及其必要的扩展。以下是一个示例,安装PHP 7.4及其常用扩展:

sudo apt install php7.4 php7.4-fpm php7.4-mysql php7.4-xml php7.4-json php7.4-common php7.4-cli

安装完成后,启动PHP-FPM并设置它在系统启动时自动启动:

sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm

5. 配置Nginx支持PHP

编辑Nginx的默认配置文件:

sudo nano /etc/nginx/sites-available/default

找到以下部分并进行修改:

server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

保存并退出编辑器,然后测试Nginx配置是否正确:

sudo nginx -t

如果没有错误,重启Nginx:

sudo systemctl restart nginx

6. 部署网站

将你的网站文件(例如WordPress)放置在/var/www/html目录下。你可以通过浏览器访问你的服务器IP地址来验证网站是否正常运行。

例如,如果你使用的是WordPress,访问http://your_server_ip应该能看到WordPress的安装页面。

7. 配置PHP

根据需要,你可以进一步配置PHP。例如,修改php.ini文件中的相关配置语句。

注意事项

以上步骤应该可以帮助你在Debian系统上成功部署LNMP环境并部署网站。如果有任何问题,请参考官方文档或在线社区的资源。

0
看了该问题的人还看了