在Ubuntu 18.04上使用Nginx安装WordPress

本教程详细介绍了如何在Ubuntu 18.04服务器上使用NGINX安装WordPress。从更新系统、安装NGINX、MariaDB和PHP,到创建WordPress数据库、配置NGINX,以及最终安装WordPress,每个步骤都有清晰说明。通过这个过程,读者可以了解如何在Linux环境中搭建WordPress网站。
摘要由CSDN通过智能技术生成

WordPress is one of the most popular and open source content management system (CMS) with a whopping 60% of market share as compared with other CMS like Drupal or Joomla.

WordPress是最流行的开源内容管理系统(CMS)之一,与其他CMS(如Drupal或Joomla)相比,其市场份额高达60%。

WordPress can be used to develop any kind of website, be it for a blog, a small business or for a large enterprise. With its free and premium plugins and themes, a WordPress site can be extended quickly to offer advanced functionalities.

WordPress可用于开发任何类型的网站,无论是用于博客,小型企业还是大型企业。 借助其免费和高级的插件和主题,可以快速扩展WordPress网站以提供高级功能。

It is also possible to develop a custom theme and plugins from scratch that will embed unique functionalities to any WordPress site. In this tutorial, we will cover the installation of WordPress CMS using LEMP stack i.e with NGINX as a web server in Ubuntu 18.04.

也可以从头开始开发自定义主题和插件,将独特的功能嵌入任何WordPress网站。 在本教程中,我们将介绍使用LEMP堆栈(即,将NGINX作为Ubuntu 18.04中的Web服务器)安装WordPress CMS。

先决条件 (Prerequisites)

  • You can open an SSH session in your Ubuntu 18.04 server using root or a sudo enabled user.

    您可以使用root或启用sudo的用户在Ubuntu 18.04服务器中打开SSH会话。
  • A registered domain name or subdomain to point to the WordPress installation.

    指向WordPress安装的注册域名或子域。
  • Domain DNS A-record pointing to your server’s IP address.

    指向服务器IP地址的域DNS A记录。
This tutorial requires the use of domain names. Whenever you see either the SUBDOMAIN, DOMAIN, or TLD variables, replace them with your own domain name values.
本教程要求使用域名。 每当您看到SUBDOMAIN,DOMAIN或TLD变量时,请将其替换为您自己的域名值。

更新您的Ubuntu 18.04 (Update your Ubuntu 18.04)

Before you start installing NGINX, it is always recommended to upgrade your Ubuntu 18.04 to the latest. The following apt-get commands will do it for you.

在开始安装NGINX之前,始终建议您将Ubuntu 18.04升级到最新版本。 以下apt-get命令将为您完成此操作。

# apt-get update 
# apt-get upgrade

The first command will update the list of available packages and their versions and the second one will actually install the newer versions of the packages that you have. Once you are done with upgrading the system, check the release version of your Ubuntu system with the following command.

第一个命令将更新可用软件包及其版本的列表,第二个命令将实际安装所拥有软件包的较新版本。 升级完系统后,请使用以下命令检查Ubuntu系统的发行版本。

# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.2 LTS
Release:        18.04
Codename:       bionic

在Ubuntu上使用NGINX安装WordPress (Install WordPress with NGINX on Ubuntu)

Follow the steps below to install the WordPress with NGINX on Ubuntu server.

请按照以下步骤在Ubuntu服务器上使用NGINX安装WordPress。

步骤1:安装NGINX (Step 1: Install NGINX)

NGINX is available in the default repositories of Ubuntu and can be installed with a single line command as shown below.

NGINX在Ubuntu的默认存储库中可用,并且可以通过单行命令安装,如下所示。

# apt-get install nginx

Once NGINX has been installed, it will run automatically. You can verify that by the following systemctl command.

一旦安装了NGINX,它将自动运行。 您可以通过以下systemctl命令进行验证。

# systemctl status nginx
 ● 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 Tue 2019-02-12 09:12:08 UTC; 11s ago
     Docs: man:nginx(8)
  Process: 17726 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 17714 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 17729 (nginx)
    Tasks: 2 (limit: 1152)
   CGroup: /system.slice/nginx.service
           ├─17729 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─17732 nginx: worker process

The output of the above command verifies that NGINX is loaded and active with PID of 17729.

上面命令的输出将验证NGINX已加载并处于活动状态,其PID为17729。

步骤2:安装MariaDB (Step 2: Install MariaDB)

MariaDB is available in the default repository of Ubuntu. It is also possible to install it from the separate MariaDB repository. But we will stick to install it from default repository of Ubuntu. Issue the following commands from the terminal to install it and optionally you can run mysql_secure_installation to make it secure.

MariaDB在Ubuntu的默认存储库中可用。 也可以从单独的MariaDB存储库中安装它。 但是我们会坚持从Ubuntu的默认存储库安装它。 从终端发出以下命令进行安装,并且可以选择运行mysql_secure_installation以使其安全。

# apt-get install mariadb-server 
# systemctl enable mariadb.service
# mysql_secure_installation

The default password for MariaDB root user is blank. To update the password of the root user, get the MySQL prompt and update the password by issuing following command from MySQL shell.

MariaDB根用户的默认密码为空。 要更新root用户的密码,请获取MySQL提示符并通过从MySQL Shell发出以下命令来更新密码。

$ mysql -u root -p
MariaDB [(none)]> use mysql;
MariaDB [mysql]> update user SET PASSWORD=PASSWORD("Passw0rd!") WHERE USER='root';

The installation of MariaDB is complete in your Ubuntu 18.04 system. Now proceed with installing PHP in the next step.

MariaDB的安装已在Ubuntu 18.04系统中完成。 现在,在下一步中继续安装PHP。

步骤3:安装PHP (Step 3: Install PHP)

The latest version of PHP (7.2) is available in the repositories of Ubuntu 18.04 and is the default candidate for installation so simply run the following command in terminal to install it.

Ubuntu 18.04的存储库中提供了最新版本PHP(7.2),它是默认的安装候选版本,因此只需在终端中运行以下命令进行安装即可。

# apt-get install php7.2 php7.2-cli php7.2-fpm php7.2-mysql php7.2-json php7.2-opcache php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl

Apart from installing php7.2, the above apt-get command also installs few other packages as well like MySQL, XML, Curl and GD packages and makes sure that your WordPress site can interact with the database, support for XMLRPC, and also to crop and resize images automatically.

除了安装php7.2之外,上述apt-get命令还安装其他一些软件包,例如MySQL,XML,Curl和GD软件包,并确保您的WordPress站点可以与数据库进行交互,支持XMLRPC并进行裁剪并自动调整图像大小。

Further, the php-fpm (Fast process manager) package is needed by NGINX to process PHP pages of your WordPress installation. Remember that FPM service will run automatically once the installation of PHP is over.

此外,NGINX需要php-fpm (快速过程管理器)包来处理WordPress安装PHP页面。 请记住,一旦PHP安装结束,FPM服务将自动运行。

步骤4:建立WordPress资料库 (Step 4: Create WordPress Database)

Once the MariaDB is installed and configured in your server, create a user and a database especially for WordPress installation. To do that, log in to the MariaDB server using mysql -u root -p command and complete the steps as described below.

在服务器中安装并配置MariaDB之后,创建一个用户和一个数据库,专门用于WordPress安装。 为此,请使用mysql -u root -p命令登录到MariaDB服务器,并完成如下所述的步骤。

$ mysql -u root -p
Enter password:

MariaDB [mysql]> CREATE DATABASE wordpress_db;
Query OK, 1 row affected (0.00 sec)

MariaDB [mysql]> GRANT ALL ON wordpress_db.* TO 'wpuser'@'localhost' IDENTIFIED BY 'Passw0rd!' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> exit
Don’t get alarmed that there is no command to create ‘wpuser’ database user. It will get automatically created with the GRANT command above. I learned about this recently and thought to surprise anyone reading this tutorial. 🙂
不要担心没有命令创建“ wpuser”数据库用户。 它将通过上面的GRANT命令自动创建。 我最近了解了这一点,并以为使阅读本教程的任何人感到惊讶。 🙂

步骤5:为WordPress配置NGINX (Step 5: Configure NGINX for WordPress)

Let us now proceed with configuring NGINX server blocks to serve your WordPress domain. To start with, create the root folder for your WordPress installation.

现在让我们继续配置NGINX服务器块以服务您的WordPress域。 首先,为您的WordPress安装创建根文件夹。

# mkdir -p /var/www/html/wordpress/public_html

To create NGINX server block for your WordPress domain, navigate to the /etc/nginx/sites-available folder. This is the default location for NGINX server blocks. Use your favorite editor to create a configuration file for NGINX server block and edit it like below.

要为您的WordPress域创建NGINX服务器块,请导航到/etc/nginx/sites-available文件夹。 这是NGINX服务器块的默认位置。 使用您最喜欢的编辑器为NGINX服务器块创建一个配置文件,并如下进行编辑。

# cd /etc/nginx/sites-available
# cat wordpress.conf
server {
            listen 80;
            root /var/www/html/wordpress/public_html;
            index index.php index.html;
            server_name SUBDOMAIN.DOMAIN.TLD;

	    access_log /var/log/nginx/SUBDOMAIN.access.log;
    	    error_log /var/log/nginx/SUBDOMAIN.error.log;

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

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

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

            location = /robots.txt {
                         allow all;
                         log_not_found off;
                         access_log off;
           }
       
            location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                         expires max;
                         log_not_found off;
           }
}

Check the correctness of above configuration file using:

使用以下命令检查以上配置文件的正确性:

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

To activate the server block create a symbolic link of the above configuration file inside /etc/nginx/sites-enabled folder.

要激活服务器块,请在/etc/nginx/sites-enabled文件夹中创建上述配置文件的符号链接。

# cd /etc/nginx/sites-enabled
# ln -s ../sites-available/wordpress.conf .

Reload NGINX to apply the new WordPress domain settings.

重新加载NGINX以应用新的WordPress域设置。

# systemctl reload nginx

步骤6:下载并配置WordPress (Step 6: Download and Configure WordPress)

In this step, download the archived WordPress file using wget and unzip it to the root of the WordPress installation that we have created in the previous step. To accomplish it run the following commands from the terminal.

在此步骤中,使用wget下载已归档的WordPress文件,并将其解压缩到我们在上一步中创建的WordPress安装的根目录。 为此,请从终端运行以下命令。

# cd /var/www/html/wordpress/public_html
# wget https://wordpress.org/latest.tar.gz
# tar -zxvf latest.tar.gz
# mv wordpress/* .
# rm -rf wordpress

Change the ownership and apply correct permissions to the extracted WordPress files and folders. To do that, use the following command from the terminal.

更改所有权并将正确的权限应用于提取的WordPress文件和文件夹。 为此,请从终端使用以下命令。

# cd /var/www/html/wordpress/public_html
# chown -R www-data:www-data *
# chmod -R 755 *

Now provide the database name, database user and the password in the WordPress config file so that it can connect to the MariaDB database that we had created earlier. By default, WordPress provides a sample configuration file and we will make use of it to create our own configuration file. To do that, first, rename the sample WordPress configuration file to wp-config.php and edit it with your own favorite editor.

现在,在WordPress配置文件中提供数据库名称,数据库用户和密码,以便它可以连接到我们之前创建的MariaDB数据库。 默认情况下,WordPress提供了一个示例配置文件,我们将利用它来创建我们自己的配置文件。 为此,首先,将示例WordPress配置文件重命名为wp-config.php并使用您自己喜欢的编辑器进行编辑。

# cd /var/www/html/wordpress/public_html
# mv wp-config-sample.php wp-config.php
# vi wp-config.php
 ...
 ...
 define('DB_NAME', 'wordpress_db');
 define('DB_USER', 'wpuser');
 define('DB_PASSWORD', 'Passw0rd!');
 ...
 ...
Wordpress Database Config

WordPress Database Config

WordPress数据库配置

To secure your WordPress site, add the security key in the above WordPress config file just after database configuration options by generating it though this link.

为了保护您的WordPress网站,请通过此链接生成安全密钥,只需在数据库配置选项之后在上述WordPress配置文件中添加安全密钥。

Wordpress Add Salt

WordPress Add Salt

WordPress加盐

You are now ready to install your WordPress site using your favorite browser.

您现在可以使用自己喜欢的浏览器安装WordPress网站。

步骤7:安装WordPress (Step 7: Install WordPress)

To complete the installation of WordPress, point your favorite web browser to SUBDOMAIN.DOMAIN.TLD and follow the steps as described below.

要完成WordPress的安装,请将您喜欢的Web浏览器指向SUBDOMAIN.DOMAIN.TLD,然后按照以下步骤进行操作。

Choose Language For WordPress Installation

Choose Language For WordPress Installation

选择用于WordPress安装的语言

The installer will prompt you to choose a language. Choose a language and click ‘Continue’.

安装程序将提示您选择一种语言。 选择一种语言,然后单击“继续”。

Provide WordPress Site Information

Provide WordPress Site Information

提供WordPress网站信息

Now provide the site information like site title, username, password, email and click on ‘Install WordPress’ button.

现在提供站点信息,例如站点标题,用户名,密码,电子邮件,然后单击“安装WordPress”按钮。

Wordpress Install Confirmation Page

WordPress Install Confirmation Page

WordPress安装确认页面

You are done with installing WordPress site. Click ‘Log In’ to login to Dashboard and proceed with configuring plugins and themes for your site.

您已完成安装WordPress网站。 单击“登录”以登录到仪表板,然后继续为您的站点配置插件和主题。

Wordpress Login Page

WordPress Login Page

WordPress登录页面

Provide the user name and password that we have entered previously to login for the first time.

提供我们之前输入的用于首次登录的用户名和密码。

Wordpress Dashboard

WordPress Dashboard

WordPress资讯主页

Congratulations! Your WordPress website is installed and ready for you to customize according to your requirements.

恭喜你! 您的WordPress网站已安装,可以根据您的要求进行自定义。

结论 (Conclusion)

WordPress is the most popular CMS and we learned how to install it with NGINX on a Ubuntu server. You can now proceed further to create your website with it.

WordPress是最受欢迎的CMS,我们学习了如何在Ubuntu服务器上使用NGINX安装它。 现在,您可以继续使用它来创建您的网站。

翻译自: https://www.journaldev.com/25670/install-wordpress-nginx-ubuntu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值