14 April 2016

How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS

Well this has been tested on Ubuntu 14.04 LTS, but it could work on your linux distro too.

Anyway, here is a quick tutorial for using Apache’s virtual hosts to organise sites to be developed locally.

Note: If you are working with a framework like CodeIgniter and need to modify URLs, you’ll need to activate mod_rewrite. See https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite for more details. tldr;

sudo a2enmod rewrite

Create the folder. I’m using example.dev as the test site. Note: I’ve used .dev because I have a plugin that forces the browser to request sites that support https to change the url from, for example http://example.com to https://example.com This will then take you to the online web page instead of the local one.

sudo mkdir -p /var/www/example.dev/public_html

Allow current user privileges to that folder

sudo chown -R $USER:$USER /var/www/example.dev/public_html

Create a test page

nano /var/www/example.dev/public_html/index.html

and add for example,

<html>
  <head>
    <title>This is example.dev</title>
  </head>
  <body>
    <h1>Success! We are running example.dev on virtual hosts!</h1>
  </body>
</html>

Make a copy of your default virtual host file

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.dev.conf

Edit the copy you just created

sudo nano /etc/apache2/sites-available/example.dev.conf

Change the following lines to point to example.dev

<VirtualHost *:80>
    ServerAdmin admin@example.dev
    ServerName example.dev
    ServerAlias www.example.dev
    DocumentRoot /var/www/example.dev/public_html
</VirtualHost>

Enable the virtual host file with the a2ensite tool

sudo a2ensite example.dev.conf

Restart apache

sudo service apache2 restart

Edit your local hosts file

sudo nano /etc/hosts

Add the line

127.0.0.1 example.dev

Test it. Open a browser and type

http://example.dev

Remember, this will now send you your local site example.dev instead of the actual example.dev online (if such a site exists).

You can delete or comment those local host entries after you’ve finished developing your site.

Update

I recently discovered ubuntu disables the functionality of the .htaccess file by default. If you intend to use it during local development, you will need to add this to your example.dev.conf file.

<Directory /var/www/example.dev/public_html >
    # AllowOverride All allows using .htaccess
    AllowOverride All
</Directory>

so the /etc/apache2/sites-available/example.dev.conf will look like this

<VirtualHost *:80>
    ServerName example.dev
    ServerAlias www.example.dev
    ServerAdmin admin@example.dev
    DocumentRoot /var/www/example.dev/public_html

    <Directory /var/www/example.dev/public_html >
        # AllowOverride All allows using .htaccess
        AllowOverride All
    </Directory>
</VirtualHost>

Update 2

If you want to process url routes make further changes

<Directory /var/www/example.dev/public_html >
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all      
</Directory>

12 April 2016

Give your home web server a domain name on the web

If you want to host your own web server on the WWW from home, this tutorial will give you a static domain name if you have a dynamic ip address. I assume you have a device to act as a web server, like the RaspberryPi, and know how to forward ports.

Step one, sign up for a ydns account from https://ydns.io/. Google your ip address and input it in the static ip address field (don’t worry about the field being static for now).

Next install ddclient

sudo apt-get install ddclient

Ignore the setup screen for now, we’ll change that later.

Now sign up for an account at https://www.dnsomatic.com/. Select ydns from the list of providers when prompted.

With your account information, edit

sudo nano /etc/ddclient.conf

Have it look like

protocol=dyndns2
use=web, web=myip.dnsomatic.com
ssl=yes
server=updates.dnsomatic.com
login=your_dnsomatic_username_here
password='your_dnsomatic_password_here'
your_ydns_domain_name_here

This file should already be root only access so no need to chmod it to 600. Test everything went ok by running

sudo ddclient

and you should see your ip address output in the console.

Double check by checking the service page at dnsomatic (hit refresh page). Your current ip will now appear in the Status column and History file. Also check My Hosts in ydns, but if it fails only in ydns, it’s likely a configuration issue you made between dnsomatic and ydns.

ddclient will incremently contact dnsomatic if your ip address changes. dnsomatic will contact ydns if this happens too. This is how the static ip address entry in ydns becomes a more dynamic one.

One more note, if you want to change the default time the ddclient deamon activates to check your ip address (default is 300 seconds), the setting is in

sudo nano /etc/default/ddclient

I set mine to 600 (once every ten minutes).

11 April 2016

Getting my wifi working on a new bare bones Debian install

Here’s a quick way to get connected to your wireless network. Note, your router will have to have dhcp enabled. This won’t work for other networks — you’ll need a network manager for that.

Login as su and edit

nano /etc/network/interfaces

At the end of the file, add this

auto wlan0
iface wlan0 inet dhcp
        wpa-ssid the_ssid_name_of_your_network
        wpa-psk your_network_password

As this file now contains your network password, make sure only root can access it.

chmod 600 /etc/network/interfaces

Reboot for changes to take effect.

10 April 2016

Display in-page PHP error reporting

In-browser/in-page errors are by default turned off in php runing on apache. Obviously, you’ll only want to do this during the development phase and not when you go to production.

Here’s how to turn on PHP’s in-page error reporting on Ubuntu when developing locally.

Method 1

Open

sudo nano /etc/php5/apache2/php.ini

Locate

display_errors = Off

Replace the parameter ‘Off’ with ‘On’.

display_errors = On

Restart Apache

sudo /etc/init.d/apache2 restart

Method 2

Create a .htaccess file in your app’s root directory.

Add these three lines

php_value error_reporting -1 
php_value display_errors stdout 
php_flag display_startup_errors on