linux

如何用LNMP搭建博客系统

小樊
48
2025-10-01 06:55:29
栏目: 编程语言

要用LNMP(Linux, Nginx, MySQL/MariaDB, PHP)搭建一个博客系统,你可以选择多种流行的内容管理系统(CMS),如WordPress、Joomla或Drupal。以下是使用WordPress作为示例的步骤:

1. 安装Linux操作系统

选择一个适合的Linux发行版,如Ubuntu、CentOS等,并进行安装。

2. 安装Nginx

在终端中运行以下命令来安装Nginx:

sudo apt update
sudo apt install nginx

启动Nginx服务并设置开机自启:

sudo systemctl start nginx
sudo systemctl enable nginx

3. 安装MySQL/MariaDB

安装MySQL或MariaDB数据库服务器:

sudo apt update
sudo apt install mysql-server

启动MySQL服务并设置开机自启:

sudo systemctl start mysql
sudo systemctl enable mysql

运行安全脚本以设置数据库的root密码和其他安全选项:

sudo mysql_secure_installation

4. 安装PHP

安装PHP及其常用扩展:

sudo apt update
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-pear php-bcmath

配置PHP-FPM以与Nginx一起工作:

sudo systemctl restart php7.4-fpm

5. 配置Nginx

编辑Nginx配置文件以支持PHP:

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

将以下内容添加到server块中:

server {
    listen 80;
    server_name your_domain.com www.your_domain.com;

    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;
    }

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

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

sudo nginx -t

重新加载Nginx以应用更改:

sudo systemctl reload nginx

6. 下载并安装WordPress

下载WordPress并将其解压到/var/www/html目录:

cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
sudo mv wordpress/* .
sudo rm -rf wordpress latest.tar.gz

创建MySQL数据库和用户用于WordPress:

sudo mysql -u root -p

在MySQL shell中运行以下命令:

CREATE DATABASE wordpress;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

7. 完成WordPress安装

打开浏览器并访问http://your_domain.com,按照WordPress安装向导完成安装过程。

8. 配置WordPress

在浏览器中访问http://your_domain.com/wp-admin,使用刚刚创建的数据库和用户登录,并进行必要的配置,如站点标题、用户名、密码等。

9. 安装SSL证书(可选)

为了安全起见,建议安装SSL证书。你可以使用Let’s Encrypt免费获取SSL证书:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your_domain.com -d www.your_domain.com

按照提示完成SSL证书的安装和配置。

完成以上步骤后,你就成功搭建了一个基于LNMP的博客系统。

0
看了该问题的人还看了