使用Ubuntu/Debian快速搭建Wordpress网站

标签

debian

ubuntu

wordpress

搭建

网站

发布时间:

本文字数:1,171 字 阅读完需:约 3 分钟

一、概述

WordPress 是网络上最流行的开源博客系统和 CMS。它基于 PHP 和 MySQL。它的功能可以通过数以千计的免费插件和主题进行扩展。 在本教程中,我们将在 Apache2 服务器上安装 WordPress 并创建我们的第一篇文章。

二、安装依赖

要安装 PHP 和 Apache,使用以下命令:

sudo apt update
sudo apt install apache2 \
                 ghostscript \
                 libapache2-mod-php \
                 mysql-server \
                 php \
                 php-bcmath \
                 php-curl \
                 php-imagick \
                 php-intl \
                 php-json \
                 php-mbstring \
                 php-mysql \
                 php-xml \
                 php-zip

   

三、安装WordPress

我们将使用来自WordPress.org的版本,而不是 Ubuntu 存档中的 APT 包,因为这是上游 WordPress 的首选方法。这也将减少 WordPress 支持志愿者无法预料并因此无法提供帮助的“陷阱”问题。 创建安装目录并从WordPress.org下载文件:  

sudo mkdir -p /srv/www
sudo chown www-data: /srv/www
curl https://wordpress.org/latest.tar.gz  sudo -u www-data tar zx -C /srv/www

 

请注意,这会将所有权设置为 user www-data,这可能是不安全的,例如当您的服务器托管具有不同维护者的多个站点时。您应该调查在这种情况下使用每个网站的用户,并使文件仅对这些用户可读和可写。这将需要配置 PHP-FPM 为每个站点启动一个单独的实例,每个实例都作为站点的用户帐户运行。在这样的设置中,wp-config.php应该(阅读:如果你做的不同,你需要一个很好的理由)对站点所有者和组是只读的,并且其他权限设置为无访问权限(chmod 400)。但是,这超出了本指南的范围。

四、为 WordPress 配置 Apache

为 WordPress 创建 Apache 站点。使用命令vim /etc/apache2/sites-available/wordpress.conf 键入以下内容:

<VirtualHost *:80>
    DocumentRoot /srv/www/wordpress
    <Directory /srv/www/wordpress>
        Options FollowSymLinks
        AllowOverride Limit Options FileInfo
        DirectoryIndex index.php
        Require all granted
    </Directory>
    <Directory /srv/www/wordpress/wp-content>
        Options FollowSymLinks
        Require all granted
    </Directory>
</VirtualHost>

启用网站:sudo a2enmod wordpress    启用 URL 重写:sudo a2ensite rewrite  (使用以下命令禁用默认的“It Works”站点:sudo a2dissite 000-default ) 或者,您可以编辑我们的配置文件以添加 WordPress 安装将响应请求的主机名,而不是禁用“它可以工作”页面。这个主机名必须以某种方式映射到您的机器上,例如通过 DNS,或编辑客户端系统的/etc/hosts文件(在 Windows 上相当于C:\Windows\System32\drivers\etc\hosts)。添加ServerName如下:

<VirtualHost *:80>
    ServerName hostname.example.com
    ... # the rest of the VHost configuration
</VirtualHost>

最后,重新加载 apache2 以应用所有这些更改:sudo service apache2 reload

五、配置数据库

要配置 WordPress,我们需要创建 MySQL 数据库。我们开始做吧!

$ sudo mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE wordpress;
Query OK, 1 row affected (0,00 sec)

mysql> CREATE USER wordpress@localhost IDENTIFIED BY '<your-password>';
Query OK, 1 row affected (0,00 sec)

mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER
    -> ON wordpress.*
    -> TO wordpress@localhost;
Query OK, 1 row affected (0,00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 1 row affected (0,00 sec)

mysql> quit
Bye

使用sudo service mysql start.

六、配置WordPress连接数据库

现在,让我们配置 WordPress 以使用该数据库。首先,将示例配置文件复制到wp-config.phpsudo -u www-data cp /srv/www/wordpress/wp-config-sample.php /srv/www/wordpress/wp-config.php 接下来,在配置文件中设置数据库凭据(不要替换database_name_hereusername_here 在下面的命令中。_请_替换<your-password>为您的数据库密码。): sudo -u www-data sed -i 's/database_name_here/wordpress/' /srv/www/wordpress/wp-config.php sudo -u www-data sed -i 's/username_here/wordpress/' /srv/www/wordpress/wp-config.php sudo -u www-data sed -i 's/password_here/<your-password>/' /srv/www/wordpress/wp-config.php 最后,在终端会话中打开 nano 中的配置文件: sudo -u www-data nano /srv/www/wordpress/wp-config.php 找到以下内容:

define( 'AUTH_KEY', 'put your unique phrase here' ); 
define( 'SECURE_AUTH_KEY', 'put your unique phrase here' ); 
define( 'LOGGED_IN_KEY', 'put your unique phrase here' ); 
define( 'NONCE_KEY', 'put your unique phrase here' ); 
define( 'AUTH_SALT', 'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' ); 
define( 'LOGGED_IN_SALT', 'put your unique phrase here' ); 
define( 'NONCE_SALT', 'put your unique phrase here' );

删除这些行(ctrl+k将在您每次按序列时删除一行)。然后替换为https://api.wordpress.org/secret-key/1.1/salt/的内容。(此地址是一个随机生成器,每次打开时都会返回完全随机的密钥。)此步骤对于确保您的站点不易受到“已知秘密”攻击非常重要。 ctrl+x 保存并关闭配置文件,然后输入y enter 

七、配置WordPress

在浏览器中打开http://localhost/ 。您将被要求提供新站点的名称、用户名、密码和电子邮件地址。请注意,您在此处选择的用户名和密码用于 WordPress,并且不提供对服务器任何其他部分的访问权限 - 选择与我们为 WordPress 使用而配置的 MySQL(数据库)凭据不同的用户名和密码,并且与您登录计算机或服务器的桌面或外壳的凭据不同。您可以选择是否要让您的网站被搜索引擎索引。