Want to host websites on your server? Using Apache? Great. This article will show you how to do exactly that using Apache’s “virtual hosts.”
In Apache, you can use virtual hosts to direct
http
traffic for a given domain name to a particular directory (i.e. the root directory of the website for the domain in the request). This feature is commonly used to host multiple websites, but we recommend using it for every website on your server including the first.
Throughout this article, we'll use an example domain - nafees.le - but you should replace it with the domain name or subdomain you 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 Apache by itself:
- Update your packages using
yum
:sudo yum update - Install Apache:sudo yum install httpd
- Start up Apache, so that the httpd service will 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/nafees.le/public_html
- Change the ownership to the Apache group:sudo chown -R apache:apache /var/www/nafees.le/public_htmlThis lets Apache modify files in your web directories.
- Change the directory's permissions so they can be read from the internet:sudo chmod -R 755 /var/www/
Create content for the website
If you have the content for the website prepped, you can upload it to the
/public_html
folder you created in the last section.
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 first page that loads when visitors come to your domain).
- Create the index file:sudo vim /var/www/nafees.le/public_html/index.html
- Add some content to the file:
<html> <head> <title>Welcome to my site!</title> </head> <body> <h1>Hooray! Your virtual host is working!</h1> </body> </html>
- 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 two directories: one to store the virtual host files (
sites-available
) and another to hold symbolic links to virtual hosts that will 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 (
httpd.conf
) so that Apache will look for virtual hosts in thesites-enabled
directory.- Open your config file:sudo vim /etc/httpd/conf/httpd.conf
- Add this line at the very end of the file:IncludeOptional sites-enabled/*.confThis way, we're telling Apache to look for additional 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/nafees.le.conf
- Paste this code in, replacing your own domain for nafees.le.conf.Here's what the whole file could look like after your changes:
<VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com ServerName www.nafees.le ServerAlias nafees.le DocumentRoot /var/www/nafees.le/public_html ErrorLog /var/www/nafees.le/error.log CustomLog /var/www/nafees.le/requests.log combined </VirtualHost>
The lines
ErrorLog
and CustomLog
are not required to set up your virtual host, but we've included them, in case you do want to tell Apache where to keep error and request logs for your site.
:wq!
sites-enabled
directory:
sudo ln -s /etc/httpd/sites-available/nafees.le.conf /etc/httpd/sites-enabled/nafees.le.conf
sudo vim /etc/hosts The details that you need to add are the public IP address of your VPS followed by the domain that you want to use to reach that VPS:
127.0.0.1 localhost
127.0.1.1 guest-desktop
server_ip_address nafees.le
sudo service httpd restart
No comments:
Post a Comment