This post content can be used to create virtual host for my previously posted Online Book Catalog Application Tutorial series.
After installing Apache on CentOS, default host directory is created at
html is the public root folder that any web client can access.
I initially configured my CentOS server's IP address to be 192.168.56.133 in my private network. This IP address can be entered into the client's address field and we can visit the page this way. However, I will use the development server for multiple projects obviously. Therefore I need to create multiple virtual hosts that will serve different web applications at different addresses.
I am going to use Name-based Virtual Host Support in Apache and share the same IP for multiple virtual hosts.
As the first step I will create
vhosts folder in
/var/www/ which will
be the parent directory for all my virtual hosts.
[smozgur@local /]$ sudo mkdir /var/www/vhosts
Then I will create
OnlineBookCatalog virtual host directory under
- which would be my application folder and change the directory ownership to stop using
for each bash command:
[smozgur@local /]$ sudo mkdir /var/www/vhosts/OnlineBookCatalog [smozgur@local /]$ cd /var/www/vhosts [smozgur@local vhosts]$ sudo chown -R smozgur:smozgur OnlineBookCatalog
Obviously, changing the directory ownership is not only saving a few keyboard touches. I use Zend Studio during development and allow only SSH access to the servers. Zend Studio and any other IDE will require permission to access your development files remotely. Since I prefer to protect even my development servers as a habit,
rootuser doesn't have SSH access just like in my production servers. Therefore I create another user, hand over all necessary rights to it, and connect to the server by using this user through IDE.
In the project directory, I need minimum 2 folders that I will also instruct Apache about their locations:
logs: the folder to keep the virtual host's access and error logs
public: the root folder I will create web accessible files, for example
[smozgur@local vhosts]$ cd OnlineBookCatalog [smozgur@local OnlineBookCatalog]$ mkdir logs [smozgur@local OnlineBookCatalog]$ mkdir public [smozgur@local OnlineBookCatalog]$ ls -l total 0 drwxrwxr-x. 2 smozgur smozgur 6 Dec 26 18:43 logs drwxrwxr-x. 2 smozgur smozgur 6 Dec 26 18:43 public
I will also create
index.html file in the root folder to use during the test of
the virtual host accessibility.
[smozgur@local OnlineBookCatalog]$ cd public [smozgur@local OnlineBookCatalog]$ echo '<h1>Welcome to Online Book Catalog Website</h1>' > index.html [smozgur@local OnlineBookCatalog]$ ls -l total 4 -rw-rw-r--. 1 smozgur smozgur 39 Dec 26 18:45 index.html [smozgur@local OnlineBookCatalog]$
The application index page is ready but Apache knows nothing about the virtual host yet. I need to create a
configuration file for the new virtual host. Considering there will be many other virtual hosts in the
future, I will create a new folder called
/etc/httpd/ to keep
all virtual host configuration files at the same place.
[smozgur@local OnlineBookCatalog]$ cd /etc/httpd [smozgur@local httpd]$ sudo mkdir vhosts.d [smozgur@local httpd]$ cd vhosts.d [smozgur@local vhosts.d]$
Generally when you see that *.d convention, it means "this is a directory holding a bunch of configuration fragments which will be merged together into configuration for some service.
excerpt from a Debian mailing list
Then create the following configuration file for the new virtual host:
<VirtualHost *:80> ServerName bc.onlinebookcatalog.com ServerAdmin email@example.com DocumentRoot "/var/www/vhosts/OnlineBookCatalog/public" CustomLog /var/www/vhosts/OnlineBookCatalog/logs/access_log combined ErrorLog /var/www/vhosts/OnlineBookCatalog/logs/error_log # Following is the setting that I will need for Zend Framework applications # to enable .htaccess usage by AllowOverride directive # It is not necessary for this post but just to be prepared for the future <Directory "/var/www/vhosts/OnlineBookCatalog/public"> Options -Indexes +FollowSymLinks AllowOverride FileInfo </Directory> </VirtualHost>
nanoLinux Command Line Editor for editing text files instead of default editor
Checking Apache configuration files to make sure it won't fail when I try to restart it and finally restarting it to include the new virtual host configuration settings:
[smozgur@local vhosts.d]$ apachectl configtest Syntax OK [smozgur@local vhosts.d]$ sudo systemctl reload httpd
It's done! Wait, it is not done. What am I supposed to enter into the browser address field to access this new domain that I just created a virtual host for?
Take a closer look at the configuration settings; especially
ServerName bc.onlinebookcatalog.com part.
I create the development domain names as
bc subdomain of the real domain name to be used in my
development server. And then I setup my operating system to resolve this subdomain to my server's IP address.
It is done in the
hosts file in Windows, Mac OS or Linux. You can find more informaiton and the exact
locations for the hosts file in different operating systems at
hosts (file) WikiPedia page.
hosts file content to access the subdomain as shown below:
## # 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 255.255.255.255 broadcasthost ::1 localhost # For the new subdomain 192.168.56.133 bc.onlinebookcatalog.com
Right after saving the hosts file, I will now be able to access my application by using