Ubuntu 20.04 上搭建 LEMP/LAMP 环境运行 WordPress

Ubuntu 20.04 上搭建 LEMP 环境运行 WordPress

Wordpress 是最流行的内容管理系统之一,也是常用的 CMS(内容管理系统)程序之一。Wordpress 可以在 VPS 上以多种方式安装,除了上一篇文章讲解的LAMP软件环境运行Wordpress外,本次讲解同样非常流行的软件环境LEMP ( Linux + Nginx + MySQL + PHP )。

本教程讲解如何在 Ubuntu 20.04 上搭建 LEMP 环境运行 WordPress,学习后你可以轻松在自己的 VPS 上搭建使用 LEMP 软件环境的 Wordpress 博客。

第一步,准备 Ubuntu 20.04 系统环境

Wordpress 程序安装前,需要准备安装了 Ubuntu 20.04 TLS 系统的 VPS。你可以通过阿里云,腾讯云购买性价比高的 VPS。

购买 VPS 后,你可以获取到 VPS 的相关信息,包括 IP 地址,root帐号密码等。默认购买的 VPS 都开启 SSH 服务,你可以通过以下命令的方式 SSH 到 VPS 上进行一系列操作了。
为安全起见,建议你使用 root 登录 VPS 后创建一个具有 sudo 权限的帐号。

ssh root@你VPS的IP地址

第二步、安装 Nginx

Apache Http Server 是一个开源的,非常流行,使用非常广泛的网页服务器软件。在 Ubuntu 20.04 系统中我们可以通过以下命令安装 Apache Http Server。

sudo apt update
sudo apt nginx

使用前面建议创建的非 root 帐号安装时,需要使用 sudo 命令。首次使用 sudo 时系统会提示输入密码,根据提示输入密码后回车即可。

安装完成后,nginx并不是默认自动启动,通过以下命令启动nginx:

sudo systemctl start nginx

启动完成后,可以输入以下命令查看 nginx 运行状态

systemctl status nginx.service

输出内容类似内容:

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2020-06-28 02:47:42 UTC; 5s ago
       Docs: man:nginx(8)
    Process: 49088 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 49099 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 49100 (nginx)
      Tasks: 2 (limit: 1075)
     Memory: 8.3M
     CGroup: /system.slice/nginx.service
             ├─49100 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─49101 nginx: worker process

默认情况,Ubuntu 20.04 TLS 中使用 UFW 管理防火墙。防火墙需要允许 http(80) 和 https(443) 通讯,可以通过以下命令配置:

sudo ufw allow 80
sudo ufw allow 443

如果 ufw 没有启动,建议启动防火墙,这样可以增加 VPS 系统安全性。

关于如何安装,查看,启停 UFW ,如何配置防火墙,可以查看如何在 Ubuntu 上使用 UFW 设置防火墙。

可以通过http://your_server_ip访问,您将看到默认的Ubuntu Apache 欢迎页面,效果图如下:
在这里插入图片描述
nginx可以正常工作了。

第三步、安装 MySQL

这一步我们来安装 MySQL,非常简单,运行如下命令即可:

sudo apt install mysql-server

如果需要密码,输入密码即可。之后,安装过程中一路下来默认根据提示默认输入Y,按回车即可。

安装完成后,MySQL 数据库默认启动,我们可以通过如下命令查看其运行状态 。

sudo systemctl status mysql

输出结果类似如下:

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-06-23 08:51:37 UTC; 5 days ago
   Main PID: 787 (mysqld)
     Status: "Server is operational"
      Tasks: 41 (limit: 1075)
     Memory: 145.0M
     CGroup: /system.slice/mysql.service
             └─787 /usr/sbin/mysqld

MySQL 安装完成后,为安全起见,建议运行 MySQL 的安全配置,设置 root 密码等,运行如下命令:

sudo mysql_secure_installation

选择密码策略,设置 root 密码,之后一路提示默认输入 y,回车即可。

第四步、安装 PHP

要显示动态内容,正常运行 Wordpress 我们需要安装 PHP,安装命令如下:

sudo apt install php-fpm php-opcache php-cli php-gd php-curl php-mysql
至此,PHP 及可能用到的扩展安装完成。

第五步、使用 Let` Encrypt 加密使用 Nginx 的 Wordpress 网站

自动安装Let’s Encrypt ,因为涉及到要验证域名,所以请提前将你的域名解析到你的VPS主机上。

这里推荐你访问 https://certbot.eff.org/ 网站,使用 certbot 工具来自动配置。

登录系统后,根据你实际站点的运行环境选择 web 容器,运行操作系统,如下图所示:
在这里插入图片描述
选择之后,下方会自动出现操作步骤,你只需要按操作步骤在你系统上执行相应的命令即可。

注意,命令执行之前你的域名已经配置解析到你的VPS并且已经生效,否则生成证书过程会失败。

以下是配置好后,linux265.conf 的配置文件内容,供参考!

# 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 nginx -t

如果检测没有问题,可以通过以下命令重新载入配置文件:

sudo nginx -s reload

如果你想重新启动nginx服务,可以通过以下命令:

sudo systemctl restart nginx

第六步、安装 Wordpress

安装Wordpress之前,需要先创建对应数据库及帐号用于安装wordpress使用。

sudo mysql

创建 Wordpress运行需要的数据库和帐号,这里使用的数据库名wdpressdb,用户名wdpressuser,密码WdPress&Password123 可以根据自己需要修改。

CREATE DATABASE wdpressdb DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'wdpressuser'@'localhost' IDENTIFIED BY 'WdPress&Password123';
GRANT ALL ON wdpressdb.* TO 'wdpressuser'@'localhost' IDENTIFIED BY 'WdPress&Password123';
GRANT ALL PRIVILEGES ON *.* TO 'wdpressuser'@'localhost';

需要注意,这里使用的默认安装的 MySQL 8.0.20。

成功创建完成后,输入以下命令,更新变更。

FLUSH PRIVILEGES;

最后,我们开始下载wordpress软件包,并部署wordpress,我们先通过cd命令转入tmp目录。

cd tmp

使用curl命令下载最新版本的wordpress。

curl -O https://wordpress.org/latest.tar.gz

下载完成后,通过tar命令解压软件包。

sudo tar xzvf latest.tar.gz

解压后,通过cd命令进入wordpress目录,使用mv命令重命名 wp-config-sample.php 文件为 wp-config.php。

cd /tmp/wordpress
sudo mv /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php

通过cp命令将文件内容拷贝到/var/www/html/example.com目录中。

sudo cp -a /tmp/wordpress/. /var/www/html/example.com

为避免任何权限问题,我们可以将域文档根目录的所有权使用chown命令更改为 apache 用户(www-data):

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

至此,配置完成,下一步我们将进入 Wordpress 运行配置界面。

在浏览器中输入https://example.com(你自己的域名),你会看到如下配置界面:
在这里插入图片描述
点击 “Let`s go!”,进入数据库连接配置界面,如下图所示:
在这里插入图片描述
这里我们输入之前创建的数据库,用户名,密码。点击 “Submit”,继续。
在这里插入图片描述
出现这个界面,点击 “Run the installation”,继续。
在这里插入图片描述
这里就输入站点名称,管理帐号名称及密码,email等内容后,点击"Install Wordpress"完成Wordpress安装。

至此,我们完成了Wordpress的安装,来看下默认的效果界面。

地址输入:https://example.com ,效果如下:

(这里的域名仅做验收使用,实际情况下需要你配置自己的域名)
在这里插入图片描述
剩下的事情就是登录 Wordpress 的管理界面,安装你喜欢的主题,开始你的 Wordpress 之旅吧。

Ubuntu 20.04 上搭建 LAMP 环境运行 WordPress

Wordpress 是最流行的内容管理系统之一,也是常用的Blog程序之一。Wordpress 可以在 VPS 上以多种方式安装,其中常用的软件环境之一就是 LAMP ( Linux + Apache + MySQL + PHP )。

本教程讲解如何在 Ubuntu 20.04 上搭建 LAMP 环境运行 WordPress,学习后你可以轻松在自己的 VPS 上搭建Wordpress博客。

第一步,准备 Ubuntu 20.04 系统环境

Wordpress 程序安装前,需要准备安装了 Ubuntu 20.04 TLS 系统的 VPS。你可以通过阿里云,腾讯云购买性价比高的 VPS。

购买 VPS 后,你可以获取到 VPS 的相关信息,包括 IP 地址,root帐号密码等。默认购买的 VPS 都开启 SSH 服务,你可以通过以下命令的方式 SSH 到 VPS 上进行一系列操作了。

ssh root@你VPS的IP地址

为安全起见,建议你使用 root 登录 VPS 后创建一个具有 sudo 权限的帐号。

第二步、安装和配置 Apache 2

Apache Http Server 是一个开源的,非常流行,使用非常广泛的网页服务器软件。在 Ubuntu 20.04 系统中我们可以通过以下命令安装 Apache Http Server。

sudo apt updatesudo apt install apache2

使用前面建议创建的非 root 帐号安装时,需要使用 sudo 命令。首次使用 sudo 时系统会提示输入密码,根据提示输入密码后回车即可。

安装完成后,apache默认自动启动,通过以下命令查看 apache 运行状态:

sudo systemctl status apache2

输出类似内容:

● apache2.service - The Apache HTTP Server   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset:    Active: active (running) since Sat 2020-06-17 13:55:49 PDT; 21s ago   ...

默认情况,Ubuntu 20.04 TLS 中使用 UFW 管理防火墙。防火墙需要允许 http(80) 和 https(443) 通讯,可以通过以下命令配置:

sudo ufw allow 80sudo ufw allow 443

如果 ufw 没有启动,建议启动防火墙,这样可以增加 VPS 系统安全性。

关于如何安装,查看,启停 UFW ,如何配置防火墙,可以查看如何在 Ubuntu 上使用 UFW 设置防火墙。

可以通过http://your_server_ip访问,您将看到默认的Ubuntu Apache 欢迎页面,效果图如下:
在这里插入图片描述

第三步、安装 MySQL

这一步我们来安装 MySQL,非常简单,运行如下命令即可:

sudo apt install mysql-server

如果需要密码,输入密码即可。之后,安装过程中一路下来默认根据提示默认输入Y,按回车即可。

安装完成后,MySQL 数据库默认启动,我们可以通过如下命令查看其运行状态 。

sudo systemctl status mysql

输出结果类似如下:

● mysql.service - MySQL Community Server     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)     Active: active (running) since Tue 2020-06-23 08:51:37 UTC; 2min 42s ago   Main PID: 787 (mysqld)     Status: "Server is operational"      Tasks: 38 (limit: 1075)     Memory: 342.8M     CGroup: /system.slice/mysql.service             └─787 /usr/sbin/mysqld

MySQL 安装完成后,为安全起见,建议运行 MySQL 的安全配置,设置 root 密码等,运行如下命令:

sudo mysql_secure_installation

选择密码策略,设置 root 密码,之后一路提示默认输入 y,回车即可。

第四步、安装 PHP

要显示动态内容,正常运行 Wordpress 我们需要安装 PHP,安装命令如下:

sudo apt install phpsudo apt install libapache2-mod-phpsudo apt install php-mysql

其中软件包 libapache2-mod-php 和 php-mysql 用来实现 Apache 和 MySQL 的扩展支持。当然为了更好支持Wordpress运行,还需要安装以下扩展,命令如下:

sudo apt install php-curl php-gd php-xml php-mbstring  php-xmlrpc php-zip php-soap php-intl

至此,PHP 及可能用到的扩展安装完成。

第五步、安装 Wordpress

安装Wordpress之前,还需要对Apache进行简单的配置创建虚拟机配置文件用于网站。假设,你的购买的域名是www.example.com。

首先,进入/etc/apache2/sites-available/目录,使用touch命令创建example.com.conf文件,文件内容如下:

<VirtualHost *:80>    ServerName example.com    ServerAlias www.example.com    ServerAdmin webmaster@example.com    DocumentRoot /var/www/example.com/public_html    <Directory /var/www/example.com/public_html>        Options -Indexes +FollowSymLinks        AllowOverride All    </Directory>    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined</VirtualHost>

内容编辑完成后,保存内容。

接下来,您可以启用 mod_rewrite 以使用 WordPress 永久链接功能。可以使用以下方法完成此操作:

sudo a2enmod rewrite

启用配置后,测试语法是否正确:

sudo apachectl configtest

要使更改生效,请使用以下命令重新启动 apache2 服务:

sudo systemctl restart apache2

其次,登录终端,输入如下命令:

sudo mysql

创建 Wordpress运行需要的数据库和帐号,这里使用的数据库名wdpressdb,用户名wdpressuser,密码WdPress&Password123 可以根据自己需要修改。

CREATE DATABASE wdpressdb DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;CREATE USER 'wdpressuser'@'localhost' IDENTIFIED BY 'WdPress&Password123';GRANT ALL ON wdpressdb.* TO 'wdpressuser'@'localhost' IDENTIFIED BY 'WdPress&Password123';GRANT ALL PRIVILEGES ON *.* TO 'wdpressuser'@'localhost';

需要注意,这里使用的默认安装的 MySQL 8.0.20。

成功创建完成后,输入以下命令,更新变更。

FLUSH PRIVILEGES;

最后,我们开始下载wordpress软件包,并部署wordpress,我们先通过cd命令转入tmp目录。

cd tmp

使用curl命令下载最新版本的wordpress。

curl -O https://wordpress.org/latest.tar.gz

下载完成后,通过tar命令解压软件包。

sudo tar xzvf latest.tar.gz

解压后,通过cd命令进入wordpress目录,使用mv命令重命名 wp-config-sample.php 文件为 wp-config.php。

cd /tmp/wordpresssudo mv /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php

通过cp命令将文件内容拷贝到/var/www/example.com目录中。

sudo cp -a /tmp/wordpress/. /var/www/example.com

为避免任何权限问题,我们可以将域文档根目录的所有权使用chown命令更改为 apache 用户(www-data):

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

至此,配置完成,下一步我们将进入 Wordpress 运行配置界面。

在浏览器中输入http://www.example.com(你自己的域名),你会看到如下配置界面:
在这里插入图片描述
点击 “Lets go!”,进入数据库连接配置界面,如下图所示:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lkTp0xwM-1593484444084)(https://linux265.com/static/images/upload/image-20200623190551023.jpg)] 这里我们输入之前创建的数据库,用户名,密码。点击 “Submit”,继续。[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ti7U392x-1593484444086)(https://linux265.com/static/images/upload/image-20200623191158919.jpg)] 出现这个界面,点击 “Run the installation”,继续。[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CciSmE0A-1593484444088)(https://linux265.com/static/images/upload/image-20200623191241204.jpg)] 这里就输入站点名称,管理帐号名称及密码,email等内容后,点击"Install Wordpress"完成Wordpress安装。至此,我们完成了Wordpress的安装,来看下默认的效果界面。地址输入:http://example.com` ,效果如下:

(这里的域名仅做验收使用,实际情况下需要你配置自己的域名)
在这里插入图片描述
剩下的事情就是登录 Wordpress 的管理界面,安装你喜欢的主题,开始你的 Wordpress 之旅吧。

第六步、使用 Let` Encrypt 加密 Apache 网站

这里推荐你访问 https://certbot.eff.org/ 网站,使用 certbot 工具来自动配置。

登录系统后,根据你实际站点的运行环境选择 web 容器,运行操作系统,如下图所示:
在这里插入图片描述

参考链接

在 Ubuntu 20.04 上搭建 LAMP 环境运行 WordPress https://mp.weixin.qq.com/s/hYS4Fss0W2Vrr_FXLQKa5Q

Ubuntu 20.04 上搭建 LEMP 环境运行 WordPress : https://mp.weixin.qq.com/s/oNCDZ_5oqxSnYtthHLYDkA

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章