Basic Ingredients for Installing Drupal on an AWS EC2 Instance
Ingredients :
- Linux
- Apache
- MySQL
- PHP
- Composer
- Drush
Let's Slice 'n Dice
AWS Linux Instance
First you need to launch a new EC2 instance and select an operating system. What we're going to use on this project is a light weight linux distro compiled by Amazon. As of this writing, the latest we have is Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type - ami-0b2c2a754d5b4da22. We chose this distribution because it already has the basic items we need, although we still need to update them later.
Proceed with the rest of the launch process until you are able to connect to the server through ssh. Log-on to the server and update the Operating system by running the below command.
sudo yum -y update
APACHE
is an application that turns your EC2 instance into a web server. This means you will be able to serve websites from your instance. Apache may already be installed and updated with the above shell command but if you are not sure, verify that Apache is running using the below shell command.
sudo service httpd status
If the response is other than : "httpd (pid xxxx) is running...", then you may need to install/reinstall Apache. Run the below command to install Apache.
sudo yum install httpd
Apache does not run automatically after installation so you need the command :
sudo service httpd start
By default, port 80 is already enabled with a firewall in this AWS AMI. To make sure that this is true, run the below command :
hostname -I
This command will list all the network addresses from this host and are space separated. Alternatively, you can also use curl to your localhost or from another instance within your VPC. There are more advanced things you can do depending on your application needs which we will not discuss in this article. To list a few, you may need to :
- Manage Apache Process - This is where you setup when Apache stops and starts as well as adding custom logging methods.
- Setup Virtual Hosts - Apache can run more than 1 virtual host much like IIS for windows
- Adjusting Linux Permissions for Virtual Hosts - Depending on the hosts, you may need to enable some special ports and adjust other components
MySQL
The next instructions will be based on a local MySQL setup for simple website. If you are planning to create a website for a corporate application then you may want to consider using a separate MySQL server using EC2 or another AWS service called RDS.
The MySQL version we are installing is version 8 which is stable as of the time of this writing. If you need a different version you may want to visit here.
Since we already performed the system update earlier we can just proceed with the below commands.
Download the preferred installer :
sudo wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
Prepare the repository for installation :
sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
We can proceed with the next steps once the update and download completes. Below are the installation steps.
sudo yum install mysql-server
Read carefully the prompts as the version you are installing might have different requirements. If you are installing the same version then you can just simply type "Y" and pressing the enter key at each question.
Finally you can start the MySQL service and create your own database.
sudo service mysqld start
PHP
This is the server side scripting that powers Drupal. Drupal works on all supported PHP versions but the recommended version is the best choice because. they will remain supported longer. In this tutorial we will installing PHP 7.4 because of its improvement in speed and caching.
The version we are installing is RPM and is in the remi-php74 repository for CentOS 7. If you do not have the required repositories yet, add them with the following command.
sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
You may need to do cleanup of old PHP version if you haven't done so. To know the existing PHP version run :
php -v
This will show you the current version. Run the next command if it outputs an older version where "xx" is the version number.
sudo yum -y remove phpxx
Next install the new PHP version of your choice using the below command.
sudo yum -y install yum-utils
sudo yum-config-manager --enable remi-php74
sudo yum update
sudo yum install php php-cli
Accept on all the prompts and that's it. You may need to install additional PHP packages depending on your purpose. Some Drupal solutions require packages like php-bcmath for an e-commerce module perhaps. To install additional packages, you can use the below methods :
Single package installation
sudo yum install php-xxx
Multiple package installation example :
sudo yum install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json
That's it! You can move on to installation of Drupal. But as bonus, we will add composer and drush. You can run Drupal without these but we found it handy in many instances.
COMPOSER
This is a dependency management tool in PHP. You use this t0 declare libraries needed for your project and it will do the install/update for you. This is used in Drupal specially by modules which has 3rd party libraries.
Installing composer is pretty easy. Since we already performed local repository updates earlier we can go directly to the installation. But if you got here just for composer, make sure you update the local repository first before installing the composer dependencies.
sudo yum install php-cli php-zip wget unzip
Next, download the composer installer script.
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
It is best to verify the integrity of the downloaded script. Use the below commands to verify.
HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
Then, use the following script to compare the official hash against the one we just downloaded.
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
After the verifying the installer, it's time for the actual installation process.
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Once you get the successful install message, you can then verify if composer is already available using the below command.
composer
You got the composer version? Now it's time to remove the installer script.
php –r “unlink(‘composer-setup.php’);”
DRUSH
This is a scripting interface used in Drupal. This comes in handy especially with AWS EC2 instances wherein you only have access to the shell interface.
We will be using composer to install drush and this should be very quick.
composer require --dev drush/drush
The above will install the main components and below is installing the drush launcher.
wget -O drush.phar https://github.com/drush-ops/drush-launcher/releases/download/0.6.0/drush.phar
That's it! You're all set now for an exciting Drupal development experience.