WordPress with LEMP on Alibaba Cloud – Part 4 Installing WordPress on Your Alibaba Cloud ECS Instance

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: In the previous tutorials we secured an Alibaba Ubuntu 16.04 ECS instance, then installed NGINX, MariaDB, and PHP7 to complete our LEMP stack.

API_plan_Dynamic_Table

By Jeff Cleverley, Alibaba Cloud Tech Share Author

In the previous tutorials we secured an Alibaba Ubuntu 16.04 ECS instance, then installed NGINX, MariaDB, and PHP7 to complete our LEMP stack. After that, we configured our DNS and nameservers so our site is now served with a domain name and protected with a Let's Encrypt SSL certificate.

Now we are ready to install WordPress!

Completing the previous tutorials is a prerequisite for this one, so if you haven't completed them yet, then follow the links above and come back here when you're done.

Step 1. Configure NGINX for WordPress

The First thing we need to do is make some slight modifications to our NGINX server blocks. Open the default NGINX configuration file with sudo privileges:

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

Within the server block we need to add some locations blocks.

Turn off logging requests for /favicon.ico and /robots.txt . Then use a regular expression to match requests for static files, then turn logging off for these requests too, and mark them as highly cacheable since they are generally expensive resources to serve. If you have any other static files you want to cache, add them here too:

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

We also need to make some adjustments inside the location / block.
Comment out the try_files list line, where it returns a 404 error as a default option, and add code that passes control to the index.php file with the request argument instead:

server {        
    . . .        
            
    location / {                                
        #try_files $uri $uri/ =404;                
        try_files $uri $uri/ /index.php$is_args$args;
    }            
            
    . . .        
}            

When you are finished your NGINX configuration file should look like the following:

1_change_nginx_conf_wordpress

<Adjust NGINX configuration file for WordPress>

Make sure to check for syntax errors:

$ sudo nginx -t

If there are none, reload NGINX:

$ sudo systemctl reload nginx

Step 2. Install PHP Extensions most commonly used with WordPress

Previously we only installed the minimum PHP extensions required to get PHP to communicate with MariaDB and NGINX. However, WordPress and its myriad ecosystem of plugins often leverage additional PHP extensions for increased functionality. We will install some of the most popular ones next.

This step is optional, you could install these when you need them, but since we are already tweaking the LEMP stack for WordPress, why not do these now?

First, update your package repository index, and then install the chosen extensions by entering:

$ sudo apt-get update
$ sudo apt-get install php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc

Once they are finished installing, we need to restart the PHP-FPM process so these new features can be leveraged:

$ sudo systemctl restart php7.0-fpm

Now PHP and NGINX are ready, we just need to add our Database with MariaDB and we can move on to WordPress.

Step 3. Create a MySQL Database and User for WordPress

WordPress needs a MySQL Database to store site and user information. In the previous tutorial, we installed MariaDB as a drop in, more performant, replacement for MySQL. Now we need to use MariaDB to create the Database and User for WordPress to use.

Log in to MariaDB, with root privileges, using the following mysql command:

$ sudo mysql -u root -p

As usual, you will need to enter your superuser password before you can progress.
Now create a Database for WordPress. Name it appropriately, for this tutorial I will be sticking with wordpress as my database name:

MariaDB [(none)]> CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Next, we need to create the user for this database. This user will be exclusive to our new database. I'm just going to use the same username as I have for my superuser, to keep things simple. You should choose whatever username you think is appropriate. I am also using a simple password for the benefit of the tutorial, but yours should be much more secure:

MariaDB [(none)]> GRANT ALL ON wordpress.* TO 'new_user'@'localhost' IDENTIFIED BY 'new_users_password';

Now we have the necessary Database and User account, all we have to do is flush the privileges so that MariaDB knows about the changes we've made. Then exit MariaDB:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

If you have followed along exactly, then your terminal should look like this:

2_wordpress_mariadb

<Create a MariaDB Database and User for WordPress>

Step 4. Configure root directory ownerships

We will be setting up WordPress in the /var/www/html directory, as set up in our NGINX configuration file server block. Change directory to this directory:

$ cd /var/www/html

If we try to import the WordPress files in this directory immediately, then we will receive a permissions error. That is because this directory currently belongs to root user in the root group.

You can verify this by entering the following command to show the owner:

$ ls -l

Your terminal will display the file and directory permissions, owner, date, time, and contents. You should see root root in the output. We will need to change the owner of the directory and the group the directory belongs to. This is necessary to allow us to download the files that we need into this directory.

First, with root privileges, add your superuser to the www-data usergroup, this is the NGINX server's usergroup. In my case that is new_user :

$ sudo usermod -aG www-data new_user

Next, change the ownership of the /var/www/html directory to your superuser and the www-data usergroup. In the case of my new_user that would be the following command:

$ sudo chown -R new_user:www-data /var/www/html

Once this is done you can check the ownership of the directory again with the same command as previously:

$ ls -l

Now the terminal should show the directory as belonging to your superuser and the www-data usergroup. If everything has been completed correctly, your terminal should look like the following:

3_chown_html

<Change ownership of the directory for WordPress>

We will be revisiting this directory and its ownership once we have installed WordPress, as it currently stands we won't be able to install themes and plugins once WordPress is up and running. We can do that later.

Installing WordPress

This is where you have a decision to make as this tutorial is going to show you two different ways to install WordPress.

The first is the original way, using curl and editing config files using nano etc. It is a trusty and reliable method, and worth going through the process incase you need to fall back to this method in future.

The second is using WP CLI, the awesome command line interface for WordPress, if you haven't used it before then this is your chance to start. It's an incredible time saver, and, once you get used to it, you can use it to manage all your WordPress sites with a workflow that offers such increased speed and efficiencies that you will never look back.

Step 5. Install WordPress using CURL

Step 5a. Download WordPress

First change into a writable directory, we previously took ownership of the WordPress destination directory so this isn't, strictly speaking, necessary other than as a safety precaution:

$ cd /tmp

Now use curl to download the compressed release with the following command:

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

After that extract the compressed file to create the WordPress directory and structure:

$ tar xzvf latest.tar.gz

Next, we need to change the wp-config-sample.php file into the wp-config.php filename needed by wordpress, use the copy command like so:

$ cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php

We also need to create the upgrade directory needed by WordPress, so that it won't run into permission problems later when it tries to do this on it's own:

$ mkdir /tmp/wordpress/wp-content/upgrade

Okay, now we have a correctly configured and structured WordPress directory in the /tmp directory, we can copy its entire contents into our html root directory.

We will use the -a flag to maintain permissions. Notice the . period at the end of our source directory, this indicates that everything in the directory should be copied, including hidden files:

$ sudo cp -a /tmp/wordpress/. /var/www/html

Step 5b. Set up the WordPress Configuration File

We need to configure some settings in the wp-config.php file so WordPress can talk to our database, and also to secure the installation.

Create and open the configuration file:

$ nano /var/www/html/wp-config.php

Enter the settings for the database we created with MariaDB earlier. You will need to correctly enter the name of the database, the database user, database user password and localhost, from earlier:

. . .                                        
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');               
                                              
/** MySQL database username */                
define('DB_USER', 'new_user');                
                                            
/** MySQL database password */                
define('DB_PASSWORD', 'new_users_password');
                                             
/** MySQL hostname */                        
define('DB_HOST', 'localhost');                
. . .                                        

Close the file and save it.

Your wp-config.php should look similar to the following screenshot:

4_wp_config_db

<Add your Database settings to wp-config.php>

The wp-config.php file will also contain a section for secure 'salt' keys. It will look like this:

. . .                        
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');    
. . .                        

You will need to replace these keys with secure key values. We can get them from the WordPress secret key generator with the following command:

$ curl -s https://api.wordpress.org/secret-key/1.1/salt/

Your terminal will display some unique values that look like the following:

5_curl_salt_keys

<Generate unique salt keys with the WordPress salt key generator>

Copy the salt keys. Open your wp-config.php file, and paste these keys in place of the placeholder values:

$ nano /var/www/html/wp-config.php

6_wp_config_salt

<Copy and paste the secure salt keys into your wp-config.php>

Make sure you close and save the wp-config.php file. Now you're ready to complete the installation.

Step 5c. Complete the Famous WordPress 5 Minute Installation through the web interface

The server configuration is complete and the WordPress directory and Configuration file is prepared, so now you can complete WordPress's famous 5 minute installation by visiting your domain.

Open your browser and navigate to your domain url:

https://an-example-domain.com

You will need to select your installation language, and then you'll be prompted to complete your site and admin user information:

7_install_wp_traditional

<Complete the famous WordPress 5 Minute installation>

Click to install WordPress, and you are done:

8_wp_installed_old_skool

<WordPress installed the Old Skool way!>

Your WordPress site should be up and running, and secured with an SSL.

Permissions will need some adjusting to allow you to install and delete plugins and themes, and make other WordPress configuration changes, but we can do that later.

Next we shall install WordPress using the fantastic WP CLI.

Step 6. Installing WordPress using WP-CLI

Step 6a. Install WP-CLI

You can download the WP-CLI PHP Archive file wp-cli.phar using either curl or wget commands:

$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

or

$ wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Next, you need to make the wp-cli.phar file executable, and move it to the /usr/local/bin directory, so that it can be run directly:

$ chmod +x wp-cli.phar
$ sudo mv wp-cli.phar /usr/local/bin/wp

When these commands are completed, your terminal should look like the following:

9_install_wpcli

<Install WP-CLI>

You can check that it has been installed correctly by checking its info:

$ wp --info

If you see something similar to the following, you can proceed:

10_wpcli_info

<Check wp --info to ensure WP-CLI installed correctly>

You will want to activate Bash completion because this allows you to see all the available WP-CLI commands on the fly.

First, download the bash script into your home directory:

$ cd ~/
$ wget https://github.com/wp-cli/wp-cli/raw/master/utils/wp-completion.bash

Next open the .bashrc file:

$ nano .bashrc

Add the following line of code at the end of the .bashrc file:

. . .
source /home/$USER/wp-completion.bash

In the nano editor, the file should look like the following:

11_nano_wpcli_bash_source

<Edit the .bashrc file so the shell loads it every time you login>

Close and save the file, then run the following command to reload the bash profile:

$ source ~/.bashrc

Bash completion is now enabled, you can test this by typing wp theme , including the trailing space, and then press tab twice. You will see a list of available commands with the wp theme prompt.

Your terminal should be showing:

12_source_bash

<Test Bash completion>

Step 6b. Install WordPress using WP-CLI

Move to the /var/www/html root directory for the site:

$ cd /var/www/html

Since we will be creating a set up with only one WordPress installation on the server, this is where we will download the WordPress files.

If your server will host several WordPress applications, then you would create separate folders as a root for each installation. This would also require some reconfiguration of the NGINX set up, this will covered in a different tutorial.

We took ownership of the /var/www/html root directory previously, so we shouldn't run into any errors while downloading the files and unpacking them.

A full list of the WP-CLI commands can be found here.

You can use the wp core download command to download WordPress core files into the directory:

$ wp core download --locale=en_GB

As you can see, I am downloading core WordPress with the British Language pack. You can use whichever language extension you require.

Now we need to create our wp-config file using our database values:

$ wp core config --dbname=wordpress --dbuser=new_user --dbpass=new_users_password --dbhost=localhost --dbprefix=wp_

Obviously you should use the database values that you created with MariaDB earlier, and not the above.

Also notice how we don't need to bother with the WordPress Salt Key Generator, that's because WP-CLI does that for us.

Finally, all we need to do is run the installation with the following command:

$ wp core install --url="https://an-example-domain.com" --title="New Skool WP-CLI Installation" --admin_user="new_user" --admin_password="some_password" --admin_email="email@domain.com"

Again, make sure to use the values for your domain, admin username, password, and email. Your terminal should look something like this:

13_wp_core_download_config_install

<Install WordPress with WP-CLI>

And that's it, you have a new WordPress installation created using WP-CLI. How easy was that?

14_install_wp_wpcli_newskool

<WordPress is not installed New Skool - WP-CLI FTW!>

You can use WP-CLI to update WordPress core, plugins, themes, copy whole installations, and databases, add admin users so you never get locked out, and so much more. But that is for another article.

We aren't finished yet though, remember those permissions?

Step 7. Configure Root Directory Permissions for WordPress functions

Log in to your WordPress installation, go to the Plugins menu, and try to delete one of the preinstalled plugins.

You should see a pop up box like the following:

15_wp_server_no_permissions

<Directory permissions problem - WordPress needs FTP credentials to function properly>

You could configure the SFTP account and credentials. While that would work, it is not ideal.

Better to change the ownership of the WordPress root directory to the www-data NGINX server user and usergroup so that our installation has full access to the filesystem it needs.

Issue the following command:

$ sudo chown www-data:www-data /var/www/html/

You can check that the www-data user and usergroup has taken ownership with the following command from within the domain root directory:

$ ls -l

You should see the following output:

16_chown_set_server_wp_permissions

<Set ownership of root directory to server user/usergroup>

Now you should be able to install and delete plugins, themes, and upload media:

17_wp_server_has_permissions

<Now WordPress has access to the web server>

So now we have a fully functional WordPress site with…. hmmmmm well, actually, it isn't quite functional yet, is it?

One of the things with cloud hosting, with any provider, is that while you get a fast server you don't have things like email functionality automatically configured and provided by the hosting provider.

Right now, your WordPress site can't send the transactional emails it requires to be fully functional.

But don't worry, Alibaba Cloud has you covered with their DirectMail transactional email service. I will cover how to set that up and configure it to work as your WordPress SMTP transactional mail service in another tutorial.

相关实践学习
2分钟自动化部署人生模拟器
本场景将带你借助云效流水线Flow实现人生模拟器小游戏的自动化部署
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情:&nbsp;https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
域名解析 网络协议 数据安全/隐私保护
阿里云轻量应用服务器搭建WordPress个人博客教程
阿里云轻量应用服务器搭建WordPress个人博客教程
|
1月前
|
域名解析 弹性计算 程序员
想要轻松地搭建一个即开即用的WordPress博客吗?借助宝塔面板镜像+阿里云ECS,迅速拥有自己的个人博客
拥有个人博客是每位程序员的梦想,但对服务器不熟悉的初学者而言,搭建博客颇具挑战。本文介绍利用阿里云市场的宝塔面板镜像与ECS云服务器,轻松搭建WordPress博客的方法,让您快速拥有专属博客空间。通过简单的操作步骤,即使是新手也能轻松上手,实现从零到有的博客搭建过程。
166 3
|
1月前
|
人工智能 运维 Kubernetes
87cloud案例分析:阿里云国际服务器如何支持在线教育
87cloud案例分析:阿里云国际服务器如何支持在线教育
|
1月前
|
域名解析 安全 网络安全
阿里云服务器WordPress环境上安装SSL证书
阿里云服务器WordPress环境上安装SSL证书
|
1月前
|
弹性计算 JSON 关系型数据库
使用ROS模板基于ECS和RDS创建WordPress环境
使用ROS模板基于ECS和RDS创建WordPress环境
|
1月前
|
弹性计算 关系型数据库 MySQL
CentOS 7.x操作系统的ECS云服务器上搭建WordPress网站
CentOS 7.x操作系统的ECS云服务器上搭建WordPress网站
|
3月前
|
弹性计算 关系型数据库 应用服务中间件
从零基础到博主大亨!一键解锁阿里云ECS,LNMP秒搭WordPress,你的个性博文帝国,今日崛起!
【8月更文挑战第5天】随着互联网技术的发展,个人博客成为技术爱好者和内容创作者分享知识的平台。阿里云ECS以其高性能和灵活性成为搭建博客的优选。本文指导你购买配置ECS,并安装CentOS 7。通过SSH登录后,更新系统并安装LNMP环境,包括Nginx、MariaDB、PHP。配置Nginx处理PHP请求,初始化数据库并设置WordPress数据库。接着下载WordPress,解压并设置权限。最后,通过浏览器完成安装向导。利用WordPress丰富的资源定制网站,开启个性化创作之旅。记得定期备份数据,利用ECS的扩展性支持网站成长。
68 4
|
3月前
|
微服务
【Azure Cloud Services】云服务频繁发生服务器崩溃的排查方案
【Azure Cloud Services】云服务频繁发生服务器崩溃的排查方案
|
9天前
|
机器学习/深度学习 人工智能 弹性计算
什么是阿里云GPU云服务器?GPU服务器优势、使用和租赁费用整理
阿里云GPU云服务器提供强大的GPU算力,适用于深度学习、科学计算、图形可视化和视频处理等多种场景。作为亚太领先的云服务提供商,阿里云的GPU云服务器具备灵活的资源配置、高安全性和易用性,支持多种计费模式,帮助企业高效应对计算密集型任务。
|
11天前
|
存储 分布式计算 固态存储
阿里云2核16G、4核32G、8核64G配置云服务器租用收费标准与活动价格参考
2核16G、8核64G、4核32G配置的云服务器处理器与内存比为1:8,这种配比的云服务器一般适用于数据分析与挖掘,Hadoop、Spark集群和数据库,缓存等内存密集型场景,因此,多为企业级用户选择。目前2核16G配置按量收费最低收费标准为0.54元/小时,按月租用标准收费标准为260.44元/1个月。4核32G配置的阿里云服务器按量收费标准最低为1.08元/小时,按月租用标准收费标准为520.88元/1个月。8核64G配置的阿里云服务器按量收费标准最低为2.17元/小时,按月租用标准收费标准为1041.77元/1个月。本文介绍这些配置的最新租用收费标准与活动价格情况,以供参考。