Setting up subdomain based URL locally (Ubuntu and Linux)
What if you require localhost website to run as local.mywebsite.com instead of classical localhost/website type address. This is sometimes required to check whether all assets and permalinks are all behaving exactly the way you want on production. For this, you need to configure a few things. Please note, this method will not work Windows.
Step 1: Modifying hosts file
- Open a terminal window (Ctrl + Alt + T)
- Open the hosts file in edit mode:
$ (sudo) gedit /etc/hosts - Add your localhost subdomain entry like this. Save and close the editor.
127.0.0.1 local.mywebsite.com
Step 2: Modifying apache2.conf file
- Move inside the apache2 directory and open apache2.conf file
$ cd /etc/apache2
$ (sudo) gedit apache2.conf - Add your local directory path and set up the permission.
<Directory /path/to/your/working-directory>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Save and close the file
Step 3: Enabling website file
- The move into sites-available directory
$ cd /etc/apache2/sites-available - Create the following file to be used for your website. Saving the content will create the file.
$ sudo gedit local.mywebsite.com.conf - Add the following content in the above file, save and close (Log files are not required but recommended)
<VirtualHost *:80>
ServerAdmin: admin@local.mywebsite.com
ServerName: local.mywebsite.com
ServerAlias: local.mywebsite.com
DocumentRoot: /path/to/your/working-directory
ErrorLog /path/to/your/working-directory/error.log
CustomLog /path/to/your/working-directory/access.log combined
</virtualHost> - Now, that the site is “available”, let’s enable it to acapahe server
$ sudo a2ensite local.mywebsite.com.conf
Step 4: Restarting apache service
- Finally, restart your apache server
$ sudo service apache2 restart
OR
$ sudo systemctl restart apache2.service (for Ubuntu 15.04+)
Step 5: Running the website
- Open any browser and run http://local.mywebsite.com