如何在Linux Mint 19上使用Nginx和PHP安装WordPress

本文将向您展示如何在Linux Mint 19上使用Nginx Web服务器和PHP-FPM / FastCGI安装wordpress。

在本文中,我们正在使用WordPress 5.3,Nginx和PHP 7.3。

01,安装PHP

如果要在Linux Mint 19上安装PHP 7.3,则需要使用以下命令启用Ondrej PHP存储库:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

在终端中运行以下命令以完成PHP7.3和一些最常见的PHP模块的安装。

sudo apt install php7.3 php7.3-common php7.3-opcache php7.3-cli php7.3-gd php7.3-curl php7.3-mysql

安装完成后,运行以下命令,将显示PHP版本:

php -v

02,安装Nginx

Nginx包在默认存储库下可用。键入以下命令:

sudo apt update
sudo apt install nginx

03,配置Nginx

我们将创建一个Nginx虚拟主机以使用FPM / FastCGI,typing命令运行:

sudo vim /etc/nginx/sites-available/example.com

如下更新配置:

# Redirect HTTP -> HTTPS
server {
    listen 80;
    server_name www.example.com example.com;

    include snippets/letsencrypt.conf;
    return 301 https://example.com$request_uri;
}

# Redirect WWW -> NON WWW
server {
    listen 443 ssl http2;
    server_name www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    root /var/www/html/example.com;
    index index.php;

    # SSL parameters
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    # log files
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

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

    location ~* .(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        expires max;
        log_not_found off;
    }
}

将所做的更改保存到配置文件,并创建指向站点启用目录的链接。

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

重新启动Nginx服务以重新加载更改。

sudo systemctl restart nginx.service

04,安装WordPress 5.3

从WordPress官方网站下载最新版本。

cd /tmp
wget https://wordpress.org/latest.tar.gz
tar xf latest.tar.gz
sudo mkdir -p /var/www/html/example.com
sudo mv /tmp/wordpress/* /var/www/html/example.com/

由于Nginx和PHP都以 www-data 用户和组,要设置正确的所有权,请运行以下chown命令:

sudo chown -R www-data: /var/www/html/example.com

通过创建指向以下地址的符号链接来启用服务器块: sites-enabled 目录:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

在重新启动Nginx服务之前,请进行测试以确保没有语法错误:

sudo nginx -t

通过键入以下命令重新启动nginx:

sudo systemctl restart nginx

打开浏览器,输入您的域,然后会出现类似以下的屏幕。

现在,您可以按照提示逐步安装。

05,结论

恭喜,您已在Linux Mint 19上成功将WordPress与Nginx和PHP一起安装。

Sidebar