Configuring the Droplet - Part II

Cover Image for Configuring the Droplet  - Part II

Hello 👋,
Welcome to the third article in the "Deploying PHP Application on Digital Ocean" series. In this article, we will continue our configuration. As we have configured Apache & PHP, now we will install MySQL Database, connect our domain, and set up SSL.

Installing MySQL

Let's install the mysql-server package with the following command.

sudo apt install mysql-server

Once the installation finishes, MySQL Server will start running. You can check the status by using systemctl command.

sudo systemctl status mysql

Currently, the MySql root user doesn't have a password. So let's set up the password.

sudo mysql

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

exit

Now we have configured the root user for MySQL. To keep our MySQL installation secure we need to run mysql_secure_installation a script.

sudo mysql_secure_installation

While executing the script you will be asked to enter the root password which we configured just now. Then you will be asked to set up VALIDATE PASSWORD component. This ensures you can use secure passwords only. Press the Y and Return key to continue. In the next step, you need to choose the security level Choose STRONG by pressing 3. You can change the root password if you think your password is not secure enough. Otherwise, just press N and skip this step.

Furthermore, you will be asked to the below prompts.

PromptAnswer
Remove anonymous users?Y
Disallow root login remotely?Y
Remove the test database and access to it?Y
Reload privilege tables now?Y

Done. We have installed the MySQL server successfully. 🎉

You can use adminer to manage the database easily. You can install the adminer in the following way.

cd /var/www/html
wget https://github.com/vrana/adminer/releases/download/v4.8.1/adminer-4.8.1-mysql-en.php
mv adminer-4.8.1-mysql-en.php adminer.php

This will install adminer. You can access it via the below URL similar to the previously created info.php.

http://[your-ip]/adminer.php

You can use your root credentials to manage the databases.

Mapping Domain

Now, we will map a domain to our droplet. At first, we need to create vhost in apache. Create a new file named domain.com.conf in /etc/apache2/sites-available/ directory. Replace domain.com with your domain. I will be using php-sample.akashpate.com

sudo nano /etc/apache2/sites-available/php-sample.akashpate.com.conf

Put the following contents in it.

<VirtualHost *:80>
        ServerName php-sample.akashpate.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/php-sample/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

In ServerName mention the domain name which you want to set up.
In DocumentRoot specify the directory where our code will be stored. Generally, it is in /var/www/ Create a directory with your project name and mention its path here.

To enable this host, use the below command.

sudo a2ensite php-sample.akashpate.com.conf
sudo systemctl restart apache2

You need to create A record in your domain name. Steps differ depending on your domain registrar. Once you find the DNS management option you need to create a record with the following data. You need to enter the name if you are using a sub-domain. Otherwise, and @ or keep empty as per your registrar. In the value section, enter the droplet IP address.

TypeNameValue
Aphp-sample64.227.138.91
A64.227.138.91

DNS propagation can take up to 48 hours. Once the propagation is complete you can access your website with the domain name.

Installing SSL

Now we have a website running let's secure it with an SSL certificate. I will be using Certbot to get free SSL.

snap install certbot --classic
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot --apache

You will be prompted to enter your email ID for renewals. Enter your email Id and continue. You need to accept the terms & conditions. Just press Y. In the next step, you will be asked to subscribe to the newsletter, depending on your choice press Y / N. A list of available hosts will be displayed. You need to choose the one you want the SSL certificate to be installed. Choose the domain and the certificate will be installed and configured with apache.

You can test if SSL is installed correctly by visiting your site. You will be auto-redirected to the secure site.

Not Found - 404

After visiting your website you will see a 404 - Not found error as we have not uploaded our code yet. If you are using git for version control, then you can simply clone the repository into the directory specified in creating the virtual host.

cd /var/www/
git clone https://github.com/digitalocean/sample-php.git ./php-sample

#dependency installation
sudo apt install composer
composer install

You need to clone your repository and configure it as per your need. Now as you can see the website is now live with SSL.

https://php-sample.akashpate.com
This URL may not be accessible currently as the droplet may have been destroyed.

Congratulations 🎉. You now have understood how to deploy the PHP application on the Digital Ocean droplet.

Thanks for Reading. ❤️
Do share your feedback in the comments and react to the article.