How to set up a website on a CentOS7 server using Apache
Lets get started
If a web host manager like WHM/cPanel isn’t in your budget, or you would prefer to set things up manually, you might want to think about setting up a virtual private server (VPS) with Google Cloud or AWS. At Web & Roll we have multiple servers for a variety of requirements for our Sheffield hosting clients, but generically a plain Apache server should be a good simple set up for your website. It doesn’t take too much work to get you set up so stick with me.
What was that about Let’s Encrypt? Lets Encrypt allows you to issue free SSL certificates across your server. You need root access to a server to be able to install it properly. This is a great alternative to paid SSL certificates. Obviously in this day and age, an SSL certificate for any business is a high requirement.
- You can follow this tutorial to set up your server from scratch – How to set up CentOS 7
- You will need to have altered the DNS records of your domain in question. The A record you wish to set up needs to point to the IP address of your CentOS 7 server.
- Please note: All below instances of FOLDER_NAME needs replacing with your website before running any commands.
To get started with Apache, you need to install httpd by running the following command:
sudo yum -y install httpd
Enable your new Apache service:
sudo systemctl enable httpd.service
Log into the root of your server using SSH and create a directory using the following command (If you don’t know what SSH is, you should probably go read up on that before continuing!)
sudo mkdir -p /var/www/FOLDER_NAME/public_html
You may need to assign the correct user permissions to your new folder using the following command:
sudo chown -R $USER:$USER /var/www/FOLDER_NAME/public_html
You may need to set some extra permissions on the web directory. As a rule of thumb, go with -755 for folders, and -644 for files. You can adjust permissions further as required depending on your specific folder structure. To modify permissions for this testing purpose, run the following command:
sudo chmod -R 755 /var/www/FOLDER_NAME/public_html
We will be using the VI editor within terminal to edit files, if you prefer to use another editor such as nano that will also work. Anyway, next you need to create a demo page for testing by running the following command. Alternatively, simply navigate to your new folder & create the file manually.
sudo vi /var/www/FOLDER_NAME/index.html
Add the following code into index.html & hit
ESC : wq on your keyboard in that order. This writes the file & quits.
<html>WELL DONE, THE PAGE IS WORKING</html>
Create your virtual hosts file in Apache using the following command:
sudo vi /etc/httpd/sites-available/FOLDER_NAME.conf
Next you either need to
ESC : wq like we did just above, and edit the file manually, or copy & paste the below into your VI editor after editing the folder name. You can edit the following config, just check out the Apache docs.
Run the following command to enable above virtual host:
sudo ln -s /etc/httpd/sites-available/FOLDER_NAME.conf /etc/httpd/sites-enabled/FOLDER_NAME.conf
Restart apache with following command:
service httpd restart
Now if you go to your domain in question, you should see the message “WELL DONE, YOUR PAGE IS WORKING”. Congratulations! It’s a lot easier to press a button in cPanel, but then you have just saved yourself some money, learned something, and hopefully had some fun along the way.
If not, did you restart Apache? If still not working, try navigating to
/etc/httpd/sitesavailable/FOLDER_NAME.conf – You might have done something incorrectly with the terminal editor, just check that the code matches up to how it’s written.
How to install Let’s Encrypt certificates on your new CentOS 7 server running Apache:
Now that you are fully installed with both CentOS 7 and Apache, your website is working as intended and life is good, you might want to consider securing the domain. To do that, you have a variety of options. We won’t list them here, we’ll go through one simple option which allows you to secure it for free.
We will need to install Certbot. To install it, run the following command:
sudo yum install python2-certbot-apache
That simple! You are now all installed. To install a new certificate, simply run the following command:
sudo certbot --apache
Just follow the options and instructions highlighted in your terminal to select which SSL option you want to take. It’s a good idea to note that a duplicate virtual host file will be created in the httpd folder, if you are going to edit anything such as a the document root, you will need to edit both of these files, then restart httpd. You may also want to set up a cron-job to automatically renew your certificate.