This article covers a version of CentOS that is no longer supported. If you are currently operating a server running CentOS 6, we highly recommend upgrading or migrating to a supported version of CentOS.
Reason: CentOS 6 reached end of life (EOL) on November 30th, 2020 and no longer receives security patches or updates. For this reason, this guide is no longer maintained.
See Instead:
This guide might still be useful as a reference, but may not work on other CentOS releases. If available, we strongly recommend using a guide written for the version of CentOS you are using.
The following DigitalOcean tutorial outlines installing the Apache web server on a CentOS 7 server, and also outlines how to set up a virtual host file:
Virtual Hosts are used to run more than one domain off of a single IP address. This is especially useful to people who need to run several sites off of one virtual private server. The sites display different information to the visitors, depending on with which the users accessed the site.There is no limit to the number of virtual hosts that can be added to a VPS.
The steps in this tutorial require the user to have root privileges. You can see how to set that up in the Initial Server Setup in steps 3 and 4. Furthermore, if I reference the user in a step, I’ll use the name www. You can implement whatever username suits you.
Additionally, you need to have apache already installed and running on your virtual server If this is not the case, you can download it with this command:
sudo yum install httpd
The first step in creating a virtual host is to a create a directory where we will keep the new website’s information.
This location will be your Document Root in the Apache virtual configuration file later on. By adding a -p to the line of code, the command automatically generates all the parents for the new directory.
sudo mkdir -p /var/www/example.com/public_html
You will need to designate an actual DNS approved domain, or an IP address, to test that a virtual host is working. In this tutorial we will use example.com as a placeholder for a correct domain name.
However, should you want to use an unapproved domain name to test the process you will find information on how to make it work on your local computer in Step Six.
We need to grant ownership of the directory to the user, instead of just keeping it on the root system.
sudo chown -R apache:apache /var/www/example.com/public_html
Additionally, it is important to make sure that everyone will be able to read our new files.
sudo chmod 755 /var/www
Now you are all done with permissions.
We need to create a new file called index.html within our configurations directory.
sudo vi /var/www/example.com/public_html/index.html
We can add some text to the file so we will have something to look at when the IP redirects to the virtual host.
<html> <head> <title>www.example.com</title> </head> <body> <h1>Success: You Have Set Up a Virtual Host</h1> </body> </html>
Save and Exit
The next step is to enter into the apache configuration file itself.
sudo vi /etc/httpd/conf/httpd.conf
There are a few lines to look for.
Make sure that your text matches what you see below.
#Listen 12.34.56.78:80 Listen 80
Scroll down to the very bottom of the document to the section called Virtual Hosts.
NameVirtualHost *:80 # # NOTE: NameVirtualHost cannot be used without a port specifier # (e.g. :80) if mod_ssl is being used, due to the nature of the # SSL protocol. # # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for requests without a known # server name. # <VirtualHost *:80> ServerAdmin webmaster@example.com DocumentRoot /var/www/example.com/public_html ServerName www.example.com ServerAlias example.com ErrorLog /var/www/example.com/error.log CustomLog /var/www/example.com/requests.log </VirtualHost>
The most important lines to focus on are the lines that say NameVirtualHost, Virtual Host, Document Root, and Server Name. Let’s take these one at a time.
The rest of the lines in this section are not required to set up a virtual host. However, it is still helpful to know what they do.
We’ve made a lot of the changes to the configuration. However, they will not take effect until Apache is restarted.
First stop all apache processes:
sudo apachectl -k stop
Then start up apache once again.
sudo /etc/init.d/httpd start
You may see the following error:
Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
The message is just a warning, and you will be able to access your virtual host without any further issues.
If you have pointed your domain name to your virtual private server’s IP address you can skip this step—you do not need to set up local hosts. Your virtual hosts should work. However, if want to try out your new virtual hosts without having to connect to an actual domain name, you can set up local hosts on your computer alone. For this step, make sure you are on the computer itself, not your droplet.
To proceed with this step you need to know your computer’s administrative password, otherwise you will be required to use an actual domain name to test the virtual hosts.
If you are on a Mac or Linux, access the root user (su
) on the computer and open up your hosts file:
nano /etc/hosts
If you are on a Windows Computer, you can find the directions to alter the host file on the Microsoft site
You can add the local hosts details to this file, as seen in the example below. As long as that line is there, directing your browser toward, say, example.com will give you all the virtual host details for the corresponding IP address.
# Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost #Virtual Hosts 12.34.56.789 www.example.com
However, it may be a good idea to delete these made up addresses out of the local hosts folder when you are done to avoid any future confusion.
Once you have finished setting up your virtual host, you can see how it looks online. Type your ip address into the browser (ie. http://12.34.56.789)
It should look somewhat similar to my handy screenshot
Good Job!
To create additional virtual hosts, you can just repeat the process above, being careful to set up a new document root with the appropriate new domain name each time. Then just copy and paste the new Virtual Host information into the Apache Config file, as shown below
<VirtualHost *:80> ServerAdmin webmaster@example.com DocumentRoot /var/www/example.com/public_html ServerName www.example.com ServerAlias example.com ErrorLog /etc/var/www/example.com/error.log CustomLog /var/www/example.com/requests.log </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@example.org DocumentRoot /var/www/example.org/public_html ServerName www.example.org ServerAlias example.org ErrorLog /var/www/example.org/error.log CustomLog /var/www/example.orgrequests.log </VirtualHost>
Once you have set up your virtual hosts, you can proceed to Create a SSL Certificate for your site or Install an FTP server
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Good, thanks!
sudo apt-get install httpd under centos … good luck
Thanks-- fixed :)
Hi there,
I have been able to install LAMP using your articles. I can run php scripts etc which are located in the default Document Root: /var/www/html
However, now I wish to move the Document_Root from default /var/www/html to another folder, in this case, lets say: /home/bingo/html where ‘bingo’ is a user with FTP access, I created this using the below command:
adduser -c ‘FTP USER Bingo’ -m bingo
Now, when I set the Document_Root to “/home/bingo/html” & try to access it through web, I am getting “Forbidden” error. Can anyone help me please?
Thanks a Ton in advance!
regards,
Sam
A forbidden error usually indicates a permissions problem so you want to ensure that the user that the webserver operates under can view the directory you set.
Make sure that the /home directory is 755 and that both bingo and html are set to 755 so that the user that the webserver runs as can browse those directories and read files which is required for serving files from that directory.
tx, raiyu is a solution mo me
Instead of <pre>sudo chown -R www:www /var/www/example.com/public_html</pre>
In my case worked with:
<pre>sudo chown -R root:root /var/www/example.com/public_html</pre>
It should be apache:apache or you’ll get some permission errors
When I go to www.mydomain.com it works fine but going to mydomain.com does not I have this virtualhost file:
NameVirtualHost *:80
<VirtualHost *:80> ServerAdmin octavio.herrera@gmail.com ServerName mydomain.com ServerAlias *.mydomain.com DocumentRoot /var/www/html </VirtualHost>
<VirtualHost *:80> ServerAdmin octavio.herrera@gmail.com ServerName blog.mydomain.com ServerAlias blog.mydomain.com DocumentRoot /var/www/html/blog </VirtualHost>
I have tried with:
<VirtualHost *:80> ServerAdmin octavio.herrera@gmail.com ServerName mydomain.com ServerAlias www.mydomain.com DocumentRoot /var/www/html </VirtualHost>
and with:
<VirtualHost *:80> ServerAdmin octavio.herrera@gmail.com ServerName www.mydomain.com ServerAlias mydomain.com DocumentRoot /var/www/html </VirtualHost>
and no luck, and my dns are with rackspace and I have an A record for mydomain.com and one for *.mydomain.com, I have also tried creating an A record for www.mydomain.com and still same problem
Thanks
“ErrorLog /etc/var/www/example.com/error.log” the etc is a typo right? There’s inconsistency in the code block there.