Install Craft CMS on a LEMP Stack on Alibaba Cloud

Step 1. Install Nginx

Login to your server:

$ ssh root@your_instance_ip
# apt-get update
# apt-get install nginx

Step 2. Install and Secure MariaDB

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.

# apt-get install mariadb-server
# systemctl enable mysql
# systemctl status mysql
# mysql_secure_installation

Step 3. Install PHP7

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.

# apt-get install php-fpm php-mysql
# apt-get install php7.0-curl php7.0-mbstring php7.0-mcrypt php-imagick php7.0-xml

Step 4. Install other dependencies

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

# apt-get install composer git

Step 5. Create a Root Directory

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

# mkdir /var/www/example.com
# cd /var/www/example.com

Step 6. Download and Prepare the CraftCMS files

Now download the latest zip archive containing the CraftCMS files:

# wget -O craft.zip https://buildwithcraft.com/latest.zip?accept_license=yes --no-check-certificate
# apt-get install unzip
# unzip craft.zip
# ls
# rm craft.zip
# ls -l
# chown -R www-data:www-data /var/www/example.com
# ls -l

Step 7. Configure Domain & DNS

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

Step 8. Create Nginx Virtual Host files

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

# nano /etc/nginx/sites-available/example.com
server {
listen 80;
listen [::]:80;
root /var/www/example.com/current/public;
index index.php index.html index.htm;
server_name example.com www.example.com; location / {
try_files $uri $uri/ @rewrites;
}
location @rewrites {
rewrite ^(.*) /index.php?p=$1 last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
expires off;
error_page 404 /index.php;
client_max_body_size 300M;
}
# ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
# cd /etc/nginx/sites-enabled && ls
# nginx -t
# service nginx restart

Step 9. Add a Let’s Encrypt SSL certificate

Install Let’s Encrypt Certbot

# apt-get install -y software-properties-common
# add-apt-repository ppa:certbot/certbot
# apt-get update
# apt-get install python-certbot-nginx

Make sure port 443 is open

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.

Obtain a Let’s Encrypt SSL Certificate

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

# certbot --nginx -d example.com -d www.domain.com
sudo certbot --authenticator standalone --installer nginx -d example.com -d www.example.com --pre-hook "service nginx stop" --post-hook "service nginx start"

Step 10. Create a Database

Login to your MariaDB installation:

# mysql -u root -p
CREATE DATABASE craft  DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
GRANT ALL ON craft.* TO 'new_user'@'localhost' IDENTIFIED BY 'new_users-password';
FLUSH PRIVILEGES;
EXIT;
# nano /var/www/example.com/craft/config/db.php
return array(    // The database server name or IP address. Usually this is 'localhost' or '127.0.0.1'.
'server' => 'localhost',
// The name of the database to select.
'database' => 'craft',
// The database username to connect with.
'user' => 'new_user',
// The database password to connect with.
'password' => 'new_users_password',
// The prefix to use when naming tables. This can be no more than 5 characters.
'tablePrefix' => 'craft',
);

Step 12. Install and Configure CraftCMS

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

https://example.com

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com