Host a Basic Website on CentOS 7

In order to host a website on your server you'll need to install a few things. You can setup extra packages if you want to provide a more advanced website, but we'll cover the basics here that allow you to server HTML and PHP files.

Install Apache

Apache is the standard for web server software and has been around for twenty years. They're all the way up to version 2 so you know it's a feature rich product (it really is though).

You can install Apache through yum:

sudo yum install httpd

That doesn't say "apache", but if you do a yum search for httpd you'll see that the description is something like:

"httpd.x86_64 : Apache HTTP Server"

Now we need to enable the httpd service so that it automatically starts when the machine boots, as well as start running the service right now.

sudo systemctl enable httpd.service

sudo systemctl start httpd.service

If you are running the firewalld service then you also need to add firewall rules to allow web traffic:

sudo firewall-cmd --permanent --zone=public --add-service=http

sudo firewall-cmd --reload

Install PHP

Next, we'll install PHP to allow you to service dynamic content.

sudo yum install php

You won't need to configure anything, but you do need to restart httpd.

sudo systemctl restart httpd

Setup permissions

Now we need to setup the permissions on the appropriate directories from which content will be served. First, create and add yourself to the "www" group so you'll have permission to edit pages in the appropriate directories.

sudo groupadd www

sudo usermod -a -G www john

Where "john" is your username. You'll need to log out and back in for this change to take effect. Next, make sure the web directory permissions are set appropriately and we'll be ready to test everything.

sudo chown -R root /var/www/

sudo chgrp -R www /var/www/

sudo chmod -R 775 /var/www/

sudo chmod g+s /var/www/

Personally, I like to put those commands in a bash script which makes it easier to reset the permissions after editing or moving new files to the content directories. It ensures that the permissions are set consistently and that I don't forget any of the steps.

Test it

Now if you enter your domain or IP address in a web browser you should get the default "Testing 123..." page that's provided by Apache. That's no fun though so let's hand make two test files to verify that we can serve content.

Create a file named "index.html" at /var/www/html and fill it with the following contents:

<html><body>Hi there</body></html>

Create another page in the same location and name it test.php. Fill it with the following:

<?php phpinfo(); ?>

If you check the details of those two files using

ls -la /var/www/html/

You'll see that your username is listed under both the owner and group column. Re-run the permission commands from above (or re-run the script you put them in) to assign the appropriate privileges.

Finally, open your browser and go to your domain or IP address, you should see the text "Hi there" displayed for you. Next, add "/test.php" to the end of the domain/IP address and see what's on that page. You should see a bunch of information about your PHP installation.

Wrap up

Now, go delete that /var/www/html/test.php file. You don't want to just be giving information away for free. Congratulations, you've got a web server that can serve HTML and PHP content to your audience. Great job!