Deploying a PHP Project on DigitalOcean - Part 4 - Configuring SSL Certificate on the Server

Ronaldo B.July 27, 20224 min read

In this article

This is the final article in our series about deploying a PHP application on DigitalOcean. You can check out the other articles in this series in the links below:

Part 1 - Creating, Configuring, and Accessing the Server

Part 2 - Installing and Configuring MySQL

Part 3 - Configuring Custom Domain Using DNS Server

In this last article, we will configure an SSL certificate on our server. It is interesting to perform this configuration because it improves our ranking on search engines, ensures a more secure browsing experience for our users, and generally increases the credibility of our project.

Currently, our site is being presented as shown in the image below:

Our site being displayed as not secure

Note that it is presented as “Not Secure.” The goal of this article is to configure the famous “green lock,” which indicates an HTTPS connection, to be set in the URL.

To perform this configuration, we will use the services of a certificate authority called Let’s Encrypt, known for providing SSL certificates for free. Thus, we won’t need to pay to make this configuration.

Initial Configuration on the Server

The first thing we will do is update the packages we already downloaded with the command sudo apt-get update.

Next, we will install two necessary packages for creating our SSL certificate in Nginx. We will download them using the following command:

sudo apt-get install certbot python3-certbot-nginx -y

From now on, our server will receive access via the HTTPS protocol. It may be that our firewall is not allowing it. To ensure that no related errors occur, we will make sure that this protocol is recognized. First, we need to enable our firewall. We will do this with the command below:

sudo ufw enable

Next, we will allow all protocols for Nginx. For that, we will use the following command:

sudo ufw allow 'Nginx Full'

Excellent! Our server is ready for the generation of our SSL certificate.

Generating SSL Certificate

We will generate the certificate with the following command:

sudo certbot --nginx -d myapp.dev.br -d www.myapp.dev.br

Note that we informed the URL of our site with and without the “www.” You should do the same, but with your project’s domain.

After executing this command, we will answer a few questions, which will be used to generate the certificate.

The first option is our email address. It is necessary for any type of contact related to changes in the certificate or security warnings.

Option to provide email for the SSL certificate

The next question will be whether we accept the Terms of Service of Let’s Encrypt. We can press “A” to indicate that we agree and then press Enter.

Option to accept the Terms of Service of Let’s Encrypt

Next, we will answer whether we want to share our email address with the Electronic Frontier Foundation, the foundation responsible for developing Certbot, the package we are using to create our certificate. We can respond “Y” for “yes” or “N” for “no” and then press Enter.

Option to share our email with the Electronic Frontier Foundation

The next configuration we will perform is whether our server should automatically redirect HTTP requests to HTTPS. We want this to happen. So, we will press “2” and then press Enter:

Option for redirecting HTTP to HTTPS

After this step, the creation of the certificate will begin:

SSL certificate being created

In a few moments, we will see the following message:

Success message for creating the SSL certificate

Now, by accessing the file sites-enabled/myapp.dev.br.conf, we will see the following configuration lines at the end of its content:

Configurations in our site file

These new configurations were defined by Certbot. We can say that it did this work for us; we do not need to change anything in this file.

Finally, we need to restart the Nginx server with the command sudo service nginx restart.

Now, by accessing the address https://myapp.dev.br, we see the following result:

Our project with the SSL Certificate

Incredible! Our site is being displayed with the SSL certificate set, and now the “green lock” is being shown. We can say that our project is complete!

Moreover, when we analyze the technologies of the project, notice that PHP 8 is also displayed:

Conclusion

In this series of articles, we learned how to perform the complete deployment of a PHP 8 application on DigitalOcean, through configurations on the Ubuntu server. We hope that these articles can help you set up the deployment of your personal or professional projects.

If you liked this content, please share it with others; it greatly helps our work. Don’t forget to check out our Complete PHP 7 Course, where we teach how to create this amazing project that we just deployed.

We’ll see you in the next article, until then :)

Keep learning with Hcode

Sign up for the course platform and follow the next blog guides.