Set Up Apache Virtual Hosts on Ubuntu.
Prerequisites:
You have to install apache2 in your os. For this follow my previous blog Install apache2 in Ubuntu.
Step 1: Create new host. Open terminal(ctrl+alt+t) and paste below code >
sudo gedit /etc/hosts
Now edit the file. Means add new host name and port.Example add below line >
127.0.1.1 example.com
Now save and close the file.
Step 2: Create the Directory Structure. Create new folder in /var/www/ directory . For this execute the below line>
sudo mkdir -p /var/www/example.com/public_html
Step 3: Grant Permissions.Now change permissions to enable regular user to modify files in our web directories. For this execute the below line>
sudo chown -R $USER:$USER /var/www/example.com/public_html
We should also modify our permissions a little bit to ensure that read access is permitted to the general web directory. For this execute the below line >
sudo chmod -R 755 /var/www
Step 4: Create Demo Pages. For this execute the below line >nano /var/www/example.com/public_html/index.html
In this file, create a simple HTML document. For example copy paste the below html.
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com</h1>
</body>
</html>
Save and close the file.
Step 5: Now create virtual host. For this you can create new virtual file for can add new virtual host in default file known 000-default.conf located in /etc/apache2/sites-available directory.
Here i create in default file instead of creating new one. For this go to /etc/apache2/sites-available directory by cd. And open the default file by >
sudo gedit 000-default.conf
Now add the below lines >
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now save and close the file.
Step 6: Finally restart the server to make these changes take effect>
Execute the below line>
sudo service apache2 restart
Enjoy
Comments
Post a Comment