We are starting a new series of articles on Hcode's Blog, where we will deploy a project in PHP and MySQL on DigitalOcean. The chosen project is the one we developed in our Complete PHP 7 Course, which is a complete and simply amazing e-commerce.
The source code for this project is available on our GitHub, and it will be through this platform that we will update our server whenever there are changes in the code.
In this first article, we will create the project on DigitalOcean and configure all access to the server, in addition to installing PHP and Nginx on it.
You need to have an account on DigitalOcean to create a new project. Additionally, you may be asked to add a credit card to our account.
Creating Project and First Droplet on DigitalOcean
Let's start by creating a new project. To do this, just click on “New Project” in the Admin Panel:
On the next page, we will add a name for our project and then click on “Create Project”:
With the project created, we will create our first Droplet. A Droplet is a virtual machine created on DigitalOcean. Each Droplet is an individual server that we can manipulate and configure.
To create our Droplet, we will click on “Create” and then on “Droplets”:
The next page will display a series of options for creating our server. We will choose Ubuntu as the distribution and select the most basic plan on the platform, about $5 per month:
On the same page, we will select the location of our server. In our case, we will choose New York City, but we could select another region if we wished.
In addition, we will select the option to enable “SSH keys.” SSH keys are essential for authentication and access to our server. We will configure this issue now.
To configure a new SSH key, we can use the PuTTY program, which is an SSH client for Windows.
We will open a new tab in the browser to install and configure this program.
Installation and Configuration of PuTTY
PuTTY can be downloaded from the site https://www.putty.org/. When accessing this page, just click the download button and run the installation file. This way, PuTTY will be installed globally on our computer:
With PuTTY installed, we will create an SSH key, which will be used for authentication on DigitalOcean and to access our server remotely. To create this key, open the Start Menu and type “puttygen.” Then, just click the option that appears:
In the window that will be displayed, click the “Generate” button. Then, move the mouse around the blank area in this window until the green bar is completed. PuTTY will use your mouse movement pattern to create the SSH key, which will ensure a unique and very secure key for our remote access:
After this process is completed, our key will have been created. If we wish, we can set a password for the access to this key, which will make it even more secure. If we choose to do this, just fill in the desired password in the “Key passphrase” and “Confirm passphrase” fields. Then, we will save the file of our SSH key on our computer, both the public and private keys. We will do this by clicking the two highlighted buttons below:
It is important that we choose a secure folder to save this SSH key. Preferably a folder where we can easily find these files when we need them.
After saving the key files, do not close the puttygen window. Copy the generated key, which will be in the area where we moved the mouse earlier. Copy all its content, which will start with “ssh-rsa.” This is necessary because we will now add the created key to DigitalOcean.
Let's return to the Droplet creation tab in the browser and click on the “New SSH Key” button:
Clicking this button will open a modal, where we can paste the content of our newly created key. We can also give a name to this key and then click on “Add SSH Key”:
Next, we just need to click on “Create Droplet”:
With this, our first Droplet will have been created and started. We can say this was the first step. Quite a few configurations, right? But now we are ready to access our server and perform the initial configurations.
Accessing our Server via PuTTY
To access our server, we need to know its IP. To do this, let's go back to the initial page of our project on DigitalOcean and look for the Droplets section. In this section, we will be able to see our Droplet's IP, as highlighted in the image below:
With this information in mind, let's access the “PuTTY” application on our computer and paste the IP in the “Host Name” field. We can give this server a name if we wish:
After defining these details, we will click on “Save.” But, before accessing our server, we need to configure authentication. We will do this through the SSH key we just created.
To add this key, select the name of the server we created in PuTTY and in the left sidebar click on “SSH” and then on “Auth.” In the window that will open, click the “Browse” button and then select the SSH key that was previously created. We will select the file that has the .ppk extension:
After this, just go back to the main page of PuTTY by clicking on “Session” and then click again on “Save” and finally on “Open”:
A new window will open, which will be our command line for access and manipulation on the server. A login will be requested. By default, this login will be “root”:
Then, the password for the SSH key we created will be requested if we defined one:
After entering the password, we will see the following image:
This image means we successfully logged into the server, and now we are ready to start configuring PHP on it.
Initial Configuration on Our Server
We will perform all the configuration of the Nginx server and PHP. We have already taught this in more detail in the article Nginx with PHP 8 on Ubuntu server. Therefore, we will not go into as much detail regarding the commands related to PHP, but only on what is different from the article we already mentioned.
The first command we will execute is sudo apt-get update, to update all installed packages.
We will install all the necessary packages for PHP to work with Nginx and also MySQL, which will be the Database used in the project. We will install these packages with the following command:
sudo apt-get install -y build-essential pkg-config libxml2-dev sqlite3 libsqlite3-dev zlib1g-dev
We will download PHP 8 exactly as we showed in the other article. After the download is complete, we will access the PHP folder and perform its configuration and installation. We will configure it in a slightly different way than we did in the other article. We will perform its configuration with the command below:
sudo ./configure --with-mysqli --with-pdo-mysql --enable-fpm
Note that we added two flags related to MySQL and PDO, essential technologies for the functioning of our project.
After executing this command, we will actually install PHP using the following command:
sudo make && sudo make install
At this point, it is possible that a memory error will be returned. In this case, make sure that your Droplet has enough memory to run PHP. Generally, 1 GB is usually enough.
The next step will be to add our project code to the server. As we mentioned at the beginning of the article, we will use the project created in our Complete PHP 7 Course in this series of articles, and it is hosted on GitHub. Therefore, we will use Git to download it to the server.
We will access the default directory for all sites hosted on an Ubuntu server. We will do this with the command cd /var/www/. In this directory, we will perform the git clone of the project.
In our case, we will use the SSH protocol to perform the download. If you wish to use it as well, you can see how to configure it in the article Adding an SSH key to GitHub.
If you do not want to, you can use the HTTPS protocol.
We will execute the command below:
git clone [email protected]:anthony1910/ecommerce.git hcode-store
With this, our project will be added to our server. And whenever we have a change in our code, just execute a git pull and the server will be automatically updated.
Now let's define that Nginx can “see” our project. To do this, we will access the Nginx configuration file with the command below:
sudo nano /etc/nginx/sites-available/default
A Text Editor will open. In this interface, we will look for the line root /var/www and we will leave it as follows:
root /var/www/hcode-store;
After saving this file, we will restart the Nginx service using the command below:
sudo service nginx restart
Now, when accessing the IP of our server, in my case http://167.172.128.137/, we will see the following result:
And we can notice that a huge bug was returned. But we programmers are already used to that ?. The error message indicates that it couldn’t connect to the Database because the connection was denied. This occurs because we have not set up MySQL on the Server. To resolve this problem, we will need to install and configure this Database. This configuration will be carried out in the next article in this series.
Anyway, in this article, we were able to perform many configurations and learn very important concepts that we can apply whenever we need to access a remote server.
And we will definitely learn many more things in the next article, until then ?.