Subrata Sarkar

Setting up subdomain based URL at Localhost (Linux)

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

  1. Open a terminal window (Ctrl + Alt + T)
  2. Open the hosts file in edit mode:
    $ (sudo) gedit /etc/hosts
  3. 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

  1. Move inside the apache2 directory and open apache2.conf file
    $ cd /etc/apache2
    $ (sudo) gedit apache2.conf
  2. 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

  1. The move into sites-available directory
    $ cd /etc/apache2/sites-available
  2. Create the following file to be used for your website. Saving the content will create the file.
    $ sudo gedit local.mywebsite.com.conf
  3. 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>
  4. Now, that the site is “available”, let’s enable it to acapahe server
    $ sudo a2ensite local.mywebsite.com.conf

Step 4: Restarting apache service

  1. Finally, restart your apache server
    $ sudo service apache2 restart
    OR
    $ sudo systemctl restart apache2.service (for Ubuntu 15.04+)

Step 5: Running the website

  1. Open any browser and run http://local.mywebsite.com

Leave a Reply

Your email address will not be published.