Saturday, November 23, 2013

LAMP: Creating virtual hosts and resolving 500 Internal server error

This guide expects, that you already have web server on your machine.

1. Copy file /etc/apache2/sites-available/default and rename it to your host name. (for example testweb.net. Also your web application must be in /var/www/testweb.net/ directory.) Dont forget to add permissions to your web application directory using
sudo chmod -R 755 /var/www/testweb.net/ For development you can use 777, but it is still not the best practice.
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/testweb.net

2. Configure testweb.net file
gksudo gedit /etc/apache2/sites-available/testweb.net

This command will open testweb.net file in gedit text editor.

ServerName testweb.net
<VirtualHost *:80>
 ServerAdmin admin@mail.com
 ServerName testweb.net
 ServerAlias www.testweb.net
 [...]

DocumentRoot /var/www/testweb.net
I'm using my own hosts configuration file:
    ServerNametestweb.net
    ServerAlias www.testweb.net

    DocumentRoot /var/www/testweb.net
    <Directory /var/www/testweb.net>
        # enable the .htaccess rewrites
        AllowOverride All
        Order allow,deny
        Allow from All
    </Directory>

    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>

3. Activate your host

sudo a2ensite testweb.net

4. Edit /etc/hosts
gksudo gedit /etc/hosts
Add this line to hosts file:

127.0.0.1    testweb.net

5. Now you need to restart your apache service.
sudo service apache2 restart
Voila, your virtual host is now available!

What should you do, if you have 500 Internal Server Error? 

This problem might be caused by .htaccess file in your project root dir. First of all, check your logs (/var/log/apache2/) and try to access your site by this link: localhost/testweb.net. But for me it was not enough, so I found this solution (not sure what this command do, so run it on your own risk T_T)

sudo a2enmod rewrite
sudo service apache2 restart

Here you can read about 500 error solving: http://askubuntu.com/questions/148246/apache2-htaccess

This resource was really helpful: https://www.digitalocean.com/community/articles/how-to-set-up-apache-virtual-hosts-on-ubuntu-12-04-lts

P.S. YEAH, I know, there are LOTS of english mistakes, sorry for that :(

No comments:

Post a Comment