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.
Former Director of Community at DigitalOcean. Expert in cloud topics including LAMP Stack, CentOS, Ubuntu, MySQL, SSL certificates, and more.
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!
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.
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>
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.
I also get this error in the httpd.conf when using this tutorials example: CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional “env=” clause (see docs)
David Levy i found that adding “common” as the 2nd parameter for CustomLog fixes the error :)
one question. Once I did all those steps, is there any additional steps so I can use .htaccess or is it enabled by default? Thanks
@proxyland: You have to add the follow lines to the virtualhost: –LT–Directory “/path/to/DocumentRoot”–GT– AllowOverride All –LT–/Directory–GT– replace --GT-- with > and --LT-- with <
I’m having the same problem as octavio.herrera except in reverse. I am able to access mydomain.com but not www.mydomain.com. Tried similar virtualhost file. Also in my DNS settings I have a CNAME to redirect www to my A record which is mydomain.com.
Any ideas?
@mlwarren88 Is it your .us domain? Because if so, I’m able to access both www.yourdomain.us and yourdomain.us (outputs HELLO WORLD).
It might be a DNS propagation issue, please try again - does it still not work?
@Kamal It looks like it’s working now. You’re right, probably just a propagation issue. Thanks for your help!
@kalsidon: Apache doesn’t allowing browsing directories without an index file, returning a 403 Forbidden error. You don’t need to set any permissions.
For anyone who wants domain.com to redirect to www.domain.com (as shown in your browser), you can use the redirect command in your httpd.conf like this: <VirtualHost *:80> ServerName domain.com Redirect permanent / http://www.domain.com/ </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@domain.com DocumentRoot /var/www/domain.com/public_html ServerName www.domain.com ErrorLog /var/www/domain.com/error.log CustomLog /var/www/domain.com/access.log </VirtualHost>
The virtual host directive was removed from my post. So the first 2 lines are included in one directive
VirtualHost *:80 ServerName domain.com Redirect permanent / http://www.domain.com/ /VirtualHost
and the other 5 lines are enclosed within another.
Add “combined” behind /var/www/example.com/requests.log
<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 combined </VirtualHost>
@Do Tran: I believe the default is combined so it should work without adding it in.
I get “chown: invalid user: `www:www’”
when I try “sudo chown -R www:www /var/www/example.com/public_html”. ?
Hi I have added domain and supdomain each one have its document root but when i try to access any one of them it shows me the domain content xfox.info content “1” test.xfox.info content “11”
@xfox2010: Did you restart apache? <pre>sudo /etc/init.d/httpd restart</pre>
its ok now
I forgot to Uncomment (remove the number sign) NameVirtualHost without making any changes sorry for wasting your time
Regurds XFOX
Dear Digital Ocean Incorporated;
Please write a tutorial that starts from sudo yum update and ends at where the work is done and this tutorial should include these, *httpd, installing and configuring *vsftpd installing and configuring *creating a user that can put files via ftp and httpd can read (grouping) *installing mod_wsgi and configuring for django (we already have automated wsgi.py files) *installing postgresql and configuring for django (we already have a smart database handler) *installing phpPgAdmin and setting basic auth
I already have a basic todo list for most of them but it will be awesome if you do and release this tutorial (bit long but should be ok to copy and paste from other tutorials you done) Not everyone support django in hosting and we are forced to have private servers. At least we buy from where we love. I love you guys, please let other people love you too.
Farewell.
I went through the process of setting up a single Virtual Host and everything worked great, but when I added a 2nd Virtual Host both the old domain and the new are just showing the Apache start screen.
@andy: The Virtualhosts seem correct. Try restarting apache (<pre>sudo apachectl -k stop; sudo /etc/init.d/httpd start</pre>)
Do you have any other virtualhosts?
@Kamal: No, just these 2. The ‘andyrench.com’ one was working fine until I added ‘believedbehavior.com’.
I tried restarting Apache and no luck.
I updated the pastebin to show all the the lines that I’ve updated in my httpd.conf: http://pastebin.com/dMwJ3YM2
Any chance it could have something to do with my DNS settings?
@andy: Check the config files for errors, what does it output? <pre>sudo /etc/init.d/httpd configtest</pre>
@andy: Hmm. Check the error logs, do you see anything that could help?
<pre>tail /var/www/andyrench.com/error.log /var/www/believedbehavior.com/error.log /var/log/httpd/error_log</pre>
Ah yes, a lot of errors that look like this: Directory index forbidden by Options directive: /var/www/andyrench.com/public_html/
@Kamal: I’m getting a ton of these type of errors. I don’t recognize those client IPs either.
[Tue Sep 17 02:29:26 2013] [error] [client 69.47.170.93] Directory index forbidden by Options directive: /var/www/andyrench.com/public_html/ [Tue Sep 17 02:36:25 2013] [error] [client 69.47.170.93] File does not exist: /var/www/andyrench.com/public_html/wp-admin
These instructions resulted in a blank white screen :( Removing the vhost settings and restarting goes back to a working html folder.
Also tried with just the one vhost to confirm if it was the same multiple entry issue Andy had.
What a bummer to be stuck at the starting like with DigitalOcean.
@Kamal: One thing I discovered, I changed Options -Indexes to Options +Indexes in welcome.conf and it listed to the directories in the browser but wouldn’t execute or list the index file. Also, both believedbehavior.com and andyrench.com (the 2 domains for my 2 virtual hosts) were both resolving to the andyrench.com directory.
@joshuaklind: make sure to add a nested directory tag listing to each virtual host.
@Kamal: I still haven’t resolved the issue fully, but I did get the files to show up again for the virtual host that was working previously. Still no idea why both domains go to the same directory. Should I comment out all the other directory listings earlier in the config? Could they be conflicting with the virtual hosts?
hello digital ocean, how can i move my existing domain hosted on free hosting (000 webhost) to a virtual host on my vps server? i did the server initial set up/LAMP/SSL certificate and so on. its a CentOS vps, please advise.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.