天天看点

Install Craft CMS on a LEMP Stack on Alibaba Cloud

From the CraftCMS website:

"Craft is a content-first CMS that aims to make life enjoyable for developers and content managers alike."

Craft is laser focused on doing one thing really well - managing content. While being an incredibly flexible CMS, it is also extremely easy to use with a clean and fresh user interface (UI). Out of the box, it includes everything a performant CMS needs within core, including user management, media and asset management, flexible taxonomies, field management, content relationships, routing and highly performant search. Having said that, it is also extensible with a healthy plugin selection.

This tutorial assumes that you have already provisioned an Alibaba Cloud Instance. It is recommended to provision an instance with at least 1GB memory.

During the tutorial, we will be issuing commands on the server as a 'root' user. If you are using a superuser, you will need to issue the sudo commands before any commands contained in the tutorial.

In the command code examples, I will be using 'example.com' and 'www.example.com' where appropriate. In the screenshots of live execution, I used a test domain 'duang.party' for demonstrative purposes. When you are running through this tutorial replace all domains with your own domain.

Login to your server:

First we will update our package manager, before installing the highly performant Nginx web server:

You will be prompted to confirm the use of some additional disk space, return yes to continue and Nginx will install.

<Install Nginx>

Next up we need to install our MySQL database. We will be using MariaDB, a highly performant drop in replacement for MySQL created by MySQL's original developer. It is free and open source and comes with the guarantee that it will always remain so.

Do that with the following command:

Click yes when prompted:

<Install MariaDB as a drop in replacement for MySQL>

Once you have installed MariaDB, enable auto starting, start the service, and check its status by executing:

Your terminal should show you that MariaDB is both 'loaded' and 'active':

Install Craft CMS on a LEMP Stack on Alibaba Cloud

<Enable MariaDB to start and check its status>

We must also remember to secure our MariaDB installation, do that with the following:

You will be prompted to create a root password:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

<Secure MariaDB - Create root password>

Then remove anonymous users, disable remote root login, and drop the test database, before reloading privileges:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

<Perform necessary security operations on MariaDB>

Once these are complete MariaDB is ready to use.

The final part of the LEMP stack we need to run Craft CMS is PHP. Although Craft will run on PHP5.3 and upwards, it is not advisable to use old and unsupported versions. PHP7 is much more secure and provides incredibly superior performance.

As Nginx doesn't natively process dynamic content, such as PHP scripts, we will need to install PHP-FPM. This will install all the core PHP packages we need to run with an NGINX server. Let's also install the PHP extension for MySQL at the same time:

Craft CMS requires a few other PHP packages that aren't included in PHP-FPM, so we need to install them separately:

You will need to enter yes to continue when prompted:

Install Craft CMS on a LEMP Stack on Alibaba Cloud
Install Craft CMS on a LEMP Stack on Alibaba Cloud

<Install PHP and the necessary PHP packages>

CraftCMS is a modern PHP application, as such its workflow uses both 'git' and the 'composer' php package manager during development.

Install these next:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

<Install development workflow dependencies>

We will need a root directory for the CraftCMS files and sub-directories. We will create this in the '/var/www/' directory:

Change directory to your newly created route directory:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

<Create a root directory for your site and enter it>

Now download the latest zip archive containing the CraftCMS files:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

<Download the CractCMS archive>

We will need to unzip the archive, but first we need to install a package to do that:

We can now unzip the archive:

When you unzip the archive it will create two directories within your root directory, 'craft' and 'public', you can see them if you list out the directory:

Craft has a very modern directory structure, its public directory only includes the 'index.php' file required to run the application, this is the file our Nginx virtual host points to in order to serve the site. All of the nuts and bolts of the CraftCMS are run securely from a directory one level above the index.php file.

When you listed out the root directory contents, you will see that the 'craft.zip' archive is still there, we need to delete it:

We also need to check ownerships of our directories:

As you can see 'root' owns both the 'craft' and 'public' directories:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

<Remove the Archive and Check directory ownerships>

Make the server user 'www-data' the owner of both directories, then recheck:

You should now be able to confirm that 'www-data' user and group owns both directories:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

<Change the ownership of the Craft directories to www-data>

Now we have our directory and file structure prepared, we need to prepare our domain and Nginx virtual host configuration.

In your Alibaba Cloud Management Console, go to Alibaba Cloud DNS and add your domain.

You will then need to configure at least two DNS records. You will need to add an 'A' record with the '@' host and your Instance IP address as the value. You will also need to add a 'CNAME' record with the 'www' and your domain name as the value.

Install Craft CMS on a LEMP Stack on Alibaba Cloud

<Add a domain with an A and CNAME record>

The Alibaba Cloud DNS will also provide you with 2 DNS server records (nameservers). If you are using a third party domain registrar, you will need to login to your registrar and replace your domains current Nameserver records with those provided by Alibaba Cloud DNS.

Create a virtual host Nginx configuration file for your site in the '/etc/nginx/sites-available/' directory, and open it for editing:

Inside the configuration file add the following:

In the next step we will be using Let's Encrypt to issue an SSL certificate for our site, so make sure to remember to add your domain both with and without the 'www' host at the 'server_name' directive.

Your Virtual Host file for your domain should look like this:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

<Edit your Nginx Virtual Host file>

Save and close the file, then symlink the file into the '/etc/nginx/site-enabled/' directory.

I like to ensure that link has been created successfully by changing directory into the target directory and listing the contents:

Assuming that the link has been created, we should now check our Nginx configuration files for any syntax errors:

Assuming none have been found, reload Nginx:

Your terminal should now look like this:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

<Symlink your Virtual Host file and check your configuration file syntax>

Install Let's Encrypt Certbot

Before we install Certbot we need to add an external repository:

Now update your package repository and install Certbot:

Before we can install a certificate, we need to make sure our Alibaba Cloud security group has Port 433 (SSL) open. Visit your management console and security group and quickly check.

To obtain a Let's Encrypt SSL certificate with Certbot is easy using their Nginx Plugin:

Add your each domain (with and without www) immediately preceded by the domain flag '-p'.

Certbot will add all the necessary configurations to your Virtual Host.

In the meantime we need to use an amended command that stops Nginx before issuing the certificate, then restarts Nginx afterwards.

The workaround command:

<b>End Note</b>

You will be asked for your email address, to agree to their terms, and whether you want to create an HTTPS redirect (Yes). Once you have answered these, Certbot will have successfully obtained your SSL certificate and automagically updated your Nginx Virtual Host file so your site will be served over HTTPS.

Install Craft CMS on a LEMP Stack on Alibaba Cloud
Install Craft CMS on a LEMP Stack on Alibaba Cloud

&lt;Obtain your Let's Encrypt SSL&gt;

Login to your MariaDB installation:

Create your database:

Then add create your user and password, and grant your user the required privileges. Make sure you use a strong password:

Finally, flush privileges and exit:

After each command, you should have received a 'Query OK' response, and your terminal should look something like this:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

&lt;Create your database and user&gt;

Step 11. Configure CraftCMS to access the Database

You can find CraftCMS's database configuration file, 'db.php', within the '/craft/config/' directory. Open it for editing:

Inside the file, configure it like so, using the database details you created in the previous step:

In your terminal it should look like the following:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

&lt;Configure CraftCMS to access your Database&gt;

Once you save and exit the file, you will be ready to install CraftCMS

Time to install CraftCMS. To do that visit your domain:

If everything has been configured properly you will be greeted by the CraftCMS installer:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

&lt;Begin to install CraftCMS&gt;

You will need to create your account, enter a username, email and password:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

&lt;Create your CraftCMS account&gt;

Next up, you will set up your site. Enter your site name, the url and choose a language:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

&lt;Set up your site&gt;

The installer will take a few seconds to configure everything before letting you know it has finished and giving you a link to your dashboard:

Install Craft CMS on a LEMP Stack on Alibaba Cloud

&lt;Configuration is complete&gt;

The CraftCMS dashboard has a clean, user-friendly interface. You should explore and start managing your content.

Install Craft CMS on a LEMP Stack on Alibaba Cloud

&lt;CraftCMS dashboard&gt;

If you visit your site URL, you will see a very basic template.

Install Craft CMS on a LEMP Stack on Alibaba Cloud

&lt;Your un-themed site&gt;