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!
Thanks-- fixed :)
how to save and exit in step three
sudo vi /etc/httpd/conf/httpd.conf
when i type , blank page open. what is the solution, can some one guide me plz
There is no file as you mentioned above in centos7
Using username “root”. Last login: Fri Sep 23 06:34:57 2016 from 124.123.229.139 [root@server ~]# sudo vi /etc/httpd/conf/httpd.conf
This is the main Apache HTTP server configuration file. It contains the
configuration directives that give the server its instructions.
See URL:http://httpd.apache.org/docs/2.4/ for detailed information.
In particular, see
URL:http://httpd.apache.org/docs/2.4/mod/directives.html
for a discussion of each configuration directive.
Do NOT simply read the instructions in here without understanding
what they do. They’re here only as hints or reminders. If you are unsure
consult the online docs. You have been warned.
Configuration and logfile names: If the filenames you specify for many
of the server’s control files begin with “/” (or “drive:/” for Win32), the
server will use that explicit path. If the filenames do not begin
with “/”, the value of ServerRoot is prepended – so ‘log/access_log’
with ServerRoot set to ‘/www’ will be interpreted by the
server as ‘/www/log/access_log’, where as ‘/log/access_log’ will be
interpreted as ‘/log/access_log’.
ServerRoot: The top of the directory tree under which the server’s
configuration, error, and log files are kept.
Do not add a slash at the end of the directory path. If you point
ServerRoot at a non-local disk, be sure to specify a local disk on the
Mutex directive, if file-based mutexes are used. If you wish to share the
same ServerRoot for multiple httpd daemons, you will need to change at
least PidFile.
ServerRoot “/etc/httpd”
Listen: Allows you to bind Apache to specific IP addresses and/or
ports, instead of the default. See also the <VirtualHost>
directive.
Change this to Listen on specific IP addresses as shown below to
prevent Apache from glomming onto all bound IP addresses.
#Listen 12.34.56.78:80 Listen 80
Dynamic Shared Object (DSO) Support
To be able to use the functionality of a module which was built as a DSO you
have to place corresponding `LoadModule’ lines at this location so the
directives contained in it are actually available before they are used.
Statically compiled modules (those listed by `httpd -l’) do not need
to be loaded here.
Example:
LoadModule foo_module modules/mod_foo.so
Include conf.modules.d/*.conf
If you wish httpd to run as a different user or group, you must run
httpd as root initially and it will switch.
User/Group: The name (or #number) of the user/group to run httpd as.
It is usually good practice to create a dedicated user and group for
running httpd, as with most system services.
User apache Group apache
‘Main’ server configuration
The directives in this section set up the values used by the ‘main’
server, which responds to any requests that aren’t handled by a
<VirtualHost> definition. These values also provide defaults for
any <VirtualHost> containers you may define later in the file.
All of these directives may appear inside <VirtualHost> containers,
in which case these default settings will be overridden for the
virtual host being defined.
ServerAdmin: Your address, where problems with the server should be
e-mailed. This address appears on some server-generated pages, such
as error documents. e.g. admin@your-domain.com
ServerAdmin root@localhost
ServerName gives the name and port that the server uses to identify itself.
This can often be determined automatically, but we recommend you specify
it explicitly to prevent problems during startup.
If your host doesn’t have a registered DNS name, enter its IP address here.
#ServerName www.example.com:80
Deny access to the entirety of your server’s filesystem. You must
explicitly permit access to web content directories in other
<Directory> blocks below.
<Directory /> AllowOverride none Require all denied </Directory>
Note that from this point forward you must specifically allow
particular features to be enabled - so if something’s not working as
you might expect, make sure that you have specifically enabled it
below.
DocumentRoot: The directory out of which you will serve your
documents. By default, all requests are taken from this directory, but
symbolic links and aliases may be used to point to other locations.
DocumentRoot “/var/www/html”
Relax access to content within /var/www.
<Directory “/var/www”> AllowOverride None # Allow open access: Require all granted </Directory>
Further relax access to the default document root:
<Directory “/var/www/html”> # # Possible values for the Options directive are “None”, “All”, # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that “MultiViews” must be named explicitly — “Options All” # doesn’t give it to you. # # The Options directive is both complicated and important. Please see # http://httpd.apache.org/docs/2.4/mod/core.html#options # for more information. # Options Indexes FollowSymLinks
</Directory>
DirectoryIndex: sets the file that Apache will serve if a directory
is requested.
<IfModule dir_module> DirectoryIndex index.html </IfModule>
The following lines prevent .htaccess and .htpasswd files from being
viewed by Web clients.
<Files “.ht*”> Require all denied </Files>
ErrorLog: The location of the error log file.
If you do not specify an ErrorLog directive within a <VirtualHost>
container, error messages relating to that virtual host will be
logged here. If you do define an error logfile for a <VirtualHost>
container, that host’s errors will be logged there and not here.
ErrorLog “logs/error_log”
LogLevel: Control the number of messages logged to the error_log.
Possible values include: debug, info, notice, warn, error, crit,
alert, emerg.
LogLevel warn
<IfModule log_config_module> # # The following directives define some format nicknames for use with # a CustomLog directive (see below). # LogFormat “%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"” combined LogFormat “%h %l %u %t "%r" %>s %b” common
</IfModule>
<IfModule alias_module> # # Redirect: Allows you to tell clients about documents that used to # exist in your server’s namespace, but do not anymore. The client # will make a new request for the document at its new location. # Example: # Redirect permanent /foo http://www.example.com/bar
</IfModule>
“/var/www/cgi-bin” should be changed to whatever your ScriptAliased
CGI directory exists, if you have that configured.
<Directory “/var/www/cgi-bin”> AllowOverride None Options None Require all granted </Directory>
<IfModule mime_module> # # TypesConfig points to the file containing the list of mappings from # filename extension to MIME-type. # TypesConfig /etc/mime.types
</IfModule>
Specify a default charset for all content served; this enables
interpretation of all content as UTF-8 by default. To use the
default browser choice (ISO-8859-1), or to allow the META tags
in HTML content to override this choice, comment out this
directive:
AddDefaultCharset UTF-8
<IfModule mime_magic_module> # # The mod_mime_magic module allows the server to use various hints from the # contents of the file itself to determine its type. The MIMEMagicFile # directive tells the module where the hint definitions are located. # MIMEMagicFile conf/magic </IfModule>
Customizable error responses come in three flavors:
1) plain text 2) local redirects 3) external redirects
Some examples:
#ErrorDocument 500 “The server made a boo boo.” #ErrorDocument 404 /missing.html #ErrorDocument 404 “/cgi-bin/missing_handler.pl” #ErrorDocument 402 http://www.example.com/subscription_info.html
EnableMMAP and EnableSendfile: On systems that support it,
memory-mapping or the sendfile syscall may be used to deliver
files. This usually improves server performance, but must
be turned off when serving from networked-mounted
filesystems or if support for these functions is otherwise
broken on your system.
Defaults if commented: EnableMMAP On, EnableSendfile Off
#EnableMMAP off EnableSendfile on
Supplemental configuration
Load config files in the “/etc/httpd/conf.d” directory, if any.
IncludeOptional conf.d/*.conf
I did exactly the same with the tutorial 5 times and it did not work at all! :D
CustomLog takes 3 arguments (not 2)
You are missing combined.
Hey All, I am getting an error on restart of the httpd service ( running CentOS release 6.7 (Final) )
Starting httpd: [Mon Sep 14 10:29:19 2015] [warn] default VirtualHost overlap on port 80, the first has precedence
Any ideas?
I don’t think you should be making your docroot writable by apache. readable, yes.
Hello,
<VirtualHost 104.104.104.1:80> DocumentRoot /var/www/html ServerName 104.104.104.1 </VirtualHost>
<VirtualHost *:80> DocumentRoot /var/www/html/laravelDemo/public ServerName laravel.demo.com </VirtualHost>
I had above code in httpd.conf file. If will call laravel.demo.com it’s should call this directory but it’s wont call it. It shows /var/www/html file.
Help me to solve above code.
Thanks
This comment has been deleted