Difficulty: 2
Time: minutes
Want to host websites on your server? Using Apache? Great. This article would show you what to do exactly that using Apache's "virtual hosts."
In Apache, you can use virtual hosts to direct http
traffic-a given domain name to a particular directory (i.e. the R Oot Directory of the website for the domain in the request). This feature are commonly used to host multiple websites, and we recommend using it for every website on your server in Cluding the first.
Throughout this article, we'll use a example domain- coolexample.com -but you should replace it with the Domai n Name or subdomain want to host on your server.
Install the Apache Web server
To get Apache on your server, you can either install it as part of a LAMP stack, or you can install the Apache by itself:
- Update your packages using
yum
: sudo yum update
- Install Apache:sudo yum Install httpd
- Start up Apache, so, the httpd service would start automatically on a Reboot:sudo service httpd start
Set up the virtual host
- Create the virtual directories for your Domain:sudo mkdir-p/var/www/coolexample.com/public_html
- Change the ownership to the Apache Group:sudo chown-r apache:apache/var/www/coolexample.com/public_htmlthis le TS Apache Modify files in your web directories.
- Change the directory ' s permissions so they can is read from the Internet:sudo chmod-r 755/var/www/
Create Content for the website
If you had the content for the website prepped, you can upload it to the folder of the last section of you /public_html
created.
If you don't have content ready to upload, you can create a sample home page (also known as an index file, which is the fi RST page that loads when visitors come to your domain).
- Create the index File:sudo vim/var/www/coolexample.com/public_html/index.html
- Add some content to the file:
- Save and close the file::wq!
Configure Your Virtual host directories
We ' re going to copy a configuration usually used in Ubuntu/debian and create both Directories:one to store the virtual hos T files ( sites-available
) and another to the symbolic links to virtual hosts, that'll be published ( sites-enabled
).
Create sites-available and sites-enabled directories
- Create the Directories:sudo Mkdir/etc/httpd/sites-availablesudo mkdir/etc/httpd/sites-enabled
Edit your Apache configuration file
Edit The main configuration file () So, Apache would look for httpd.conf
virtual hosts in the sites-enabled
directory.
- Open your config File:sudo vim/etc/httpd/conf/httpd.conf
- Add this on the very end of the file:includeoptional sites-enabled/*.confthis, we ' re telling Apache to a dditional config files in the
sites-enabled
directory.
- Save and close the file::wq!
Create Virtual Host File
We ' re going to build it from a new file in your sites-available
directory.
- Create a new config File:sudo vim/etc/httpd/sites-available/coolexample.com.conf
- Paste this code in, replacing your own domain for coolexample.com.conf.
Here's what's the whole file could look like after your changes:
<virtualhost *:80> ServerAdmin [email protected] ServerName www.coolexample.com Serveralias coolexample.com documentroot/var/www/coolexample.com/public_html errorlog/var/www/coolexample.com/ Error.log Customlog/var/www/coolexample.com/requests.log Combined </VirtualHost>
The lines ErrorLog
CustomLog
and is not required to set up your virtual host, but we ' ve included them Tell the Apache where to keep the error and request logs for your site.
- Save and close the file::wq!
- Enable your virtual host file with a sym link to the
sites-enabled
directory:sudo ln-s/etc/httpd/sites-available/coolexample . com. conf/etc/httpd/sites-enabled/coolexample.com. conf
- Restart Apache:sudo Service httpd Restart
Point your domain name to your server
If your domain name isn ' t currently loading another website, you should point it to your server to test your new config.
How do I depends on where your domain name is registered and whose server you ' re using:
Domain registered? |
Server hosted? | Do this
... |
Godaddy |
Godaddy |
Point your domain name to a server |
Another company |
Godaddy |
Find a server ' s public IP address and then update your domain name ' s primary ("@") a record. |
Godaddy |
Another company |
Find Your server ' s IP address, and then change your domain's IP address to use it. |
Another company |
Another company |
Find Your server ' s IP address, and then change your domain's IP address to use it. |
Changes to your domain can take up to hours to display across the Internet. However, once they do, you can visit your domain name and view the test page you created earlier!
Adding Additional virtual hosts
To create additional sites, repeat the following sections:
- Set up the virtual host
- Create Content for the website
- Create virtual host File-but For additional virtual hosts, you'll need to the Create new config files
/etc/httpd/sites-available/
in, for Exampl e:/etc/httpd/sites-available/Your second domain name
- Point your domain name to your server
Configure Apache Virtual Hosts-centos 7