Estimated Completion Time: 1 Hour | Difficulty: Beginner
With the web constantly expanding it is important to learn the basics and demonstrate our abilities to help the web grow. Having a web server for development or personal reasons can provide a quality of life of the administrators.
I have found Digital Ocean to be a great host for a VPS and they simplify the process immensely. I will be showing you how to create an account and VPS with Digital Ocean, how to configure the VPS to act as a web host, and finally how to use your web host to work for you. We will be using the LAMP (Linux, Apache2, Mysql, and PHP) stack to develop this server. LAMP will give us access to a web server with Apache2, a database connection with Mysql, and server-side processing with PHP.
Creating an Account with Digital Ocean
- Navigate to Digital Ocean’s website or use my referral link here. The referral gives you a free $10.00 credit to your account, which equates to two months free of web hosting.
- Create an account with a valid email address and password.
- You will be asked to verify the email address. Navigate to your email’s inbox and wait a few minutes to receive the verification email. You will need to click the link in the email to verify that the email account belongs to you.
- You will need to add a payment method to cover any bills that your account may generate. You can choose from adding a credit card or a PayPal account. The credit card method will withdraw $1.00 from your account then deposit it back in a few hours to verify the card is valid. If you choose the PayPal method, I believe they will withdraw $5.00 as a deposit. I chose to add my credit card as my default payment method. You will only be charged if you go over your credit.
- Once the payment method is added and verified you will be able to access your account and create droplets to host our web server!
Creating the Web Server Droplet
- Once you are signed into your Digital Ocean account we are ready to start creating our droplet.
- Click the green ‘Create’ drop-down menu next to your account’s profile picture and select
You will be brought to a web page to configure your droplet. As you can see there are many options here for different versions of the Linux operating system, different ‘sizes’ of your droplet and their specifications, different locations of where your droplet will physically be hosted, and other options.
Choosing a droplet’s image:
- I will be using the Ubuntu image for our droplet. If you choose another image you will have trouble following this tutorial. Make sure the version of Ubuntu you are choosing is ‘16.04.03 x64’ or later.
Choosing the droplet’s size:
- The size of the droplet is the key factor in your server’s performance. Since this is your first droplet it would be best to choose the smallest size offered for the reason that it is the cheapest and will perform fine for what we are doing. Do not too hung up on the droplet’s size as you can always resize the droplet later on for free. The cheapest option is the $5.00/month droplet, which they conveniently convert to the hourly operation of $0.0007/hour.
Choosing block storage:
- This option allows you to add additional storage to your droplet. For this tutorial, I will not be adding block storage to the droplet as the 20GB SSD disk should be more than enough for now.
Choosing a datacenter region:
- This option is where the droplet will physically be located. It is best to have the droplet hosted closest to yourself, or closest to your customers. I am living in Connecticut so I will choose the New York option. The server option (1, 2, 3) doesn’t matter too much for this tutorial.
Choosing additional options:
- These options are for more advanced folks, but for the sake of the tutorial, we will only enable the ‘Monitoring’ checkbox to see how our droplet performs live at any given time. If you want to get savvy you can choose the ‘Backups’ option to constantly have a backup of your droplet’s data, but keep in mind Digital Ocean will charge you an additional 20% for this droplet monthly.
Add your SSH keys:
- This step is not necessary for what we are trying to do. You can always add an SSH Key later on once we have a working server, which I recommend you do. I will not be covering SSH keys in this tutorial.
Finalize and create:
- Choosing droplet amount: Keep the droplet amount to 1 as we will only be creating a single droplet.
- Choosing a hostname: This is completely up to you on what you want to name the droplet.
Initializing the Droplet
This is where the fun of it begins. Digital Ocean has their own tutorial on how to configure and initialize the droplet located here, but I will write my own from my experiences. Feel free to follow Digital Oceans’ tutorial, but keep in mind I will be sourcing from their tutorial as well. Any text from now that is highlighted dark blue is a direct quote from Digital Ocean.
- To start, click the ‘Droplets’ link in Digital Ocean’s navigation bar. This will bring you to a web page where you view all your created droplet. This screen will show you the IP address of your droplet, write this down for later use.
- Click on your droplet’s name to be directed to the control panel of your droplet. Once we are in the droplet’s control panel you will see a variety of options. This guide will not cover all the options in the control panel.
- In your control panel, you will see the ‘Console’ link towards the right. This is where you will access the droplet and configure it. Open a console window by clicking the link.
- You will be greeted by a black screen with your Linux version and your droplet’s name as a login. In order to get your login information to your droplet, you will need to check the inbox of your email. You should have an email about your new droplet which contains the username (root) and password needed. Write this down, we will be changing it soon.
- Back at your console window, enter ‘root’ as the username. Then enter your password that was chosen for you in the email. The password will not be shown as you type, but it is being entered into the invisible field. Once you have successfully signed in, you will immediately be prompted to enter the password again. You will then need to change the password given to you. Enter your new password and confirm it and write it down.
- We will need to sign in once more with the same username (root) and with the password you just changed.
- Once signed in you will see that you are ready to start entering some commands. Let start with creating a new user account for ourselves and give it permission to run administrative tasks. The root username is the default administrator account, but it is best practice to not use the root account unless completely necessary. The root user is the administrative user in a Linux environment that has very broad privileges. Because of the heightened privileges of the root account, you are actually discouraged from using it on a regular basis.
- In the console run the command below with ### being the name of the account you would like. For example: adduser ryan. You will have to enter and confirm a password for this user. Write this information down.
- After creating a password you will be prompted with a few questions for this username (your full name, phone numbers, room number, etc.) these values are not critical to the account. Fill them out if you wish or hit the Enter key to skip the question. Once the questionnaire is completed you will be prompted to confirm the data. If all is correct enter ‘Y’ and hit the Enter key. We have successfully created a primary user account for this droplet.
- Now that we have an additional user we need to give it administrative permissions to complete the tasks going forward. To avoid having to log out of our normal user and log back in as the root account, we can set up what is known as “superuser” or root privileges for our normal account. This will allow our normal user to run commands with administrative privileges by putting the word sudo before each command.
- In the console window enter the command where ### is the username you have chosen earlier. For example: usermod -aG sudo ryan is what I would use.
usermod -aG sudo ###
- Now our user account will have administrative access. Let’s test it. Run the command below to log out of the root account.
- You will be brought back to the original screen where we need to login in. Enter the username you have created and then the password you created to sign in. You should now be signed in.
- Let’s configure a firewall for our server. Enter the command below which will allow SSH connections for later. You will be prompted to enter your password. We have now made an exception for our firewall to allow SSH.
sudo ufw allow OpenSSH
- Enter the command below which will enable the firewall. Enter your password again. You should receive a message Firewall is active and enabled on system startup as confirmation the firewall is working.
sudo ufw enable
Configuring the LAMP stack:
We are ready to configure the LAMP stack for this server. Since we already have the L (Linux) of the LAMP acronym, we will now work on the Apache2 portion. Digital Ocean has an awesome guide on how to configure the LAMP stack here. Again, I will be referencing the guide and explaining my experiences.
Start with running the command below in your console window to update our system. After a few minutes, you will that the server updated successfully. If your browser goes unresponsive at this time, do not exit, but be patient and wait for the server to complete its task.
sudo apt-get update
- Great, the server is upgraded – let’s install Apache2. Run the command below. Again, be patient for the server to complete its task. If the browser hangs up just wait if it does happen to crash no worries – just go back to Digital Ocean’s web page and relaunch the console. It will remember where you left off.
sudo apt-get install apache2
- You should now have to confirm the disk space being used by this package we are downloading. Enter ‘Y’ and hit the Enter key. After some time you will receive a message Firewall Reloaded which confirm Apache2 was installed correctly.
Next, we will add a single line to the /etc/apache2/apache2.conf file to suppress a warning message. While harmless, if you do not set ServerName globally, you will receive the following warning when checking your Apache configuration for syntax errors: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message.
- Run the command below. You should receive the error message above. Let’s fix that.
sudo apache2ctl configtest
- Run the command below which will open a notepad-like screen where we are actually editing a file on our server.
sudo nano /etc/apache2/apache2.conf
- You should see that the file is over 221 lines long, scroll all the way to the bottom of the file using your down arrow key for ~2 minutes until you made it to the bottom of the file. It should be a little choppy/slow, that is normal. You will know you are at the bottom of the file once your cursor is blinking under the # vim: syntax=apache ts=4 sw=4 sts=4 sr noet line.
- Enter the text below where ### is the IP address of your droplet. The IP address should be under the ‘PUBLIC IP ADDRESS’ section at the bottom of your console window. For example: ServerName 18.104.22.168. Hit the key combination ‘CTRL+X’ to exit this file and enter ‘Y’ to save the changes we made, then hit Enter again.
- Enter the command below that we entered earlier. You should no longer receive the error message, but the output Syntax OK appears. Great!
sudo apache2ctl configtest
- Now that we made changes to Apache2’s core files we need to restart Apache for the changes to take effect. Run the command below.
sudo systemctl restart apache2
- Now we have to make adjustments to our firewall so that we can allow traffic to our Apache server. Run the command below. You should see a rule being added after it is complete.
sudo ufw allow in "Apache Full"
- Now if everything was done correctly we should now be able to access a ‘test’ page from our server on any internet browser. Open your browser of choice and enter http://### where ### is your droplet’s IP Address, into your address bar and go. You should see the default Apache Demo page. Congratulations, we have a web server up and running! The next steps are to add a MySQL database to your website and a PHP server. These are both highly recommended as they server great functionality.
Now that we have our web server up and running, it is time to install MySQL. MySQL is a database management system. Basically, it will organize and provide access to databases where our site can store information.
- Run the command below to install the MySQL server. You will be prompted for your password and a confirmation for the disk space it will be using. Enter ‘Y’ to continue.
sudo apt-get install mysql-server
This download is ~162MB, so it may take some time to install. If you find your console window frozen, give it some time or reload the console from Digital Ocean’s webpage. During the installation, your server will ask you to select and confirm a password for the MySQL “root” user. This is an administrative account in MySQL that has increased privileges. Think of it as being similar to the root account for the server itself (the one you are configuring now is a MySQL-specific account, however). Make sure this is a strong, unique password, and do not leave it blank. Write this password and username down.
- After a few minutes, the installation will complete. Run the command below that acts as a security script that will prevent unwanted access to the database. Enter your password when prompted, which is the password you just created for the ‘root’ user of the MySQL database.
- You will be prompted for the ‘VALIDATE PASSWORD PLUGIN’, this is not necessary at the time, just enter ‘N’ then hit the Enter key. You will be prompted to change the password for root. This is not necessary, enter ‘N’ then hit the Enter key.
- Enter ‘Y’ for removing anonymous users, Enter ‘Y’ for disabling root login remotely, enter ‘Y’ for removing the test database and finally enter ‘Y’ for reloading the privilege tables. You should receive an All done! confirmation message. We now have a running MySQL server!
PHP is the component of our setup that will process code to display dynamic content. It can run scripts, connect to our MySQL databases to get information and hand the processed content over to our web server to display.
- Run the command below which will install several packages which will connect our MySQL, Apache, and PHP servers together. Enter your password when prompted for it. Enter ‘Y’ when it asks for permission for the disk space usage.
sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql
- Run the command below which will open the notepad like interface again. We will be editing a file to prefer files ending in ‘.php’ instead of the default ‘.html’.
sudo nano /etc/apache2/mods-enabled/dir.conf
- There will be a couple lines of code that look like:
<IfModule mod_dir.c> DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm </IfModule>
- We need to move the ‘index.php’ portion of the string right after the ‘DirectoryIndex’ portion.
<IfModule mod_dir.c> DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm </IfModule>
- Hit the key combination ‘CTRL+X’ to exit, hit ‘Y’ to save, and hit Enter again to be brought back to the main screen.
- Run the command below to restart our apache2 service to reflect the changes we have made.
sudo systemctl restart apache2
- Our PHP configuration should be complete, now let’s test it. Run the command below which will create a .php file in our web directory.
sudo nano /var/www/html/info.php
- We will be brought back to the notepad-like screen where we will enter the code:
<?php phpinfo(); ?>
- Hit key combination ‘CTRL+X’ to exit, ‘Y’ to save, then enter to be brought back to the main screen.
- Now if everything was done correctly we should now be able to access a ‘test’ page from our server on any internet browser. Open your browser of choice and enter http://###/info.php where ### is your droplet’s IP Address, into your address bar and go. You should see the PHP info demo page.
- This webpage can contain sensitive information so for now, let’s remove it. Run the command below to remove it from our server.
sudo rm /var/www/html/info.php
You have successfully installed the LAMP stack on Digital Ocean’s hosted droplets.
After following this tutorial you should now have a droplet from Digital Ocean that has the LAMP stack installed. The foundation has been laid and we are ready to start using the server.
I will be writing another tutorial soon on how to access the directory to upload our files to have them shown on a web page. See my guide on how to access the web directory of your Ubuntu server here. For now, thank you for taking the time to read this and if you have any questions, comments, or concerns please address them below.