Multiple Remote Virtual host and sub domains

Just by doing whats below the server will automatically recognize if its requesting or and navigate to the correct page. No additional configuration is necessary, everything to make subdomains and virtual host work is here.

$ sudo nano /etc/apache2/sites-available/

    # domain:
    # public: /var/www/mulsite/

    <virtualhost *:80>

      # Admin email, Server Name (domain name) and any aliases

      # Index file and Document Root (where the public files are located)
      DocumentRoot /var/www/mulsite/

$ sudo a2ensite
$ sudo service apache2 reload


For subdomain:
configure you dns provider godaddy or

$ sudo nano /etc/apache2/sites-available/

    # domain:
    # public: /var/www/mulsite/

    <virtualhost *:80>

      # Admin email, Server Name (domain name) and any aliases

      # Index file and Document Root (where the public files are located)
      DocumentRoot /var/www/mulsite

$ sudo a2ensite
$ sudo service apache2 reload

the default page should be /var/www/

sub domains

Python script in the browser

This site shows you how to setup apache2 so you can run python script in the browser (lampp). He shows a method on how to use an html form to fetch date from python using POST.

Running a script directly without POST and using urllib2 to fetch a website:
in the browser type:

import cgi
import cgitb
import urllib2

# Begin HTML generation, must be here
print "Content-Type: text/html; charset=UTF-8" # Print headers
print "" # Signal end of headers

# HTML is pushed to the page with print
# To save time you can use ''' to open and close a print
# statement for block printing

response = urllib2.urlopen('')

print "hello world"

run python script html apache2 server
run python script html
urllib2 execute html

Multiple Sites on One Server – Virtual Hosts

This works for Ubuntu

Go to your site directory and make two folder and
$ mkdir /var/www/ /var/www/

Modify your host file
$ sudo nano /etc/hosts

Add the two lines of code into that file

Make configuration changes to apache by creating 2 files in the /etc/apache2/sites-available directory
$ sudo touch /etc/apache2/sites-available/ /etc/apache2/sites-available/

Put this code in those new created file. file:
<VirtualHost *:80>
DocumentRoot /var/www/
</VirtualHost> file:
<VirtualHost *:80>
DocumentRoot /var/www/

Final configuration commands
$ sudo a2ensite
$ sudo a2ensite
$ sudo service apache2 reload

Now if you put either or in your browser you’ll go to those sites. Now if you own these domain names put you internet IP address with the domain hosting and it will automatically navigate to the specified site location, making accessibility public.

if error:
apache2: Could not reliably determine the server’s fully qualified domain name, using for ServerName

$ sudo nano /etc/apache2/httpd.conf

add the line below:
ServerName localhost


AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using Set the ‘ServerName’ directive globally to suppress this message

Allow htaccess to override php.ini

In order .htaccess to override the configuration on php.ini you must edit your apache configuration.

In order for the .htaccess code below to work in your site

<IfModule mod_php5.c>
php_value upload_max_filesize 10MB
php_value post_max_size 10MB

You’ll will need to insert the code below to /etc/apache2/sites-available/default

<VirtualHost *:80>
        ServerAdmin webmaster@localhost


    <Directory /var/www/>
        AllowOverride All


Then restart apache
$ /etc/init.d/apache2 restart


ubuntu server add .htaccess directory
allow php.ini to be override using .htaccess
php.ini increase upload size
php ini httpd conf htaccess
.htaccess is not overriding php.ini
.htaccess override php.ini
changing php ini values using .htaccess
change php ini htaccess not working

Data Log

When building a site or managing a server its very beneficial to look at data and error logs.

To check if php error logs is enabled run phpinfo() and under Core table and the error_log row if ‘no value’ it means its not enabled. To enable PHP error logging open C:\wamp\bin\apache\Apache2.2.17\bin\php.ini and uncomment error_log line. See below

; Log errors to specified file. PHP's default behavior is to leave this value
; empty.
; Example:
;error_log = php_errors.log
; Log errors to syslog (Event Log on NT, not valid in Windows 95).
;error_log = syslog
error_log = "c:/wamp/logs/php_error.log"


Ubuntu Server:
PHP error logs is enabled and shows up /var/log/apache2/error.log

For MySQL logging uncomment the general_log_file and general_log line in /etc/mysql/my.cnf see example below

# * Logging and Replication
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
general_log_file        = /var/log/mysql/mysql.log
general_log             = 1

log_error                = /var/log/mysql/error.log

Then restart MySQL
$ /etc/init.d/mysql restart


XAMPP troubleshoot

XAMPP on linux ubuntu stopped working I belive it was because I
ran the python barebones server script. While XAMPP was running I
would go to http://localhost and the page wouldn’t load. So I tried
to restart xamp
$ sudo opt/lampp/lampp restart

I received this error message

(20014)Internal error: Error retrieving pid file logs/
Remove it before continuing if it is corrupted.

so I deleted the logs/ file, restarted LAMPP and it started to work again

troubleshoot, linux ubunut

Python and MySQL

I needed a better way to prevent spammers from coming to my site.  I was manually retrieving their IP address and blocking on my .htaccess file.  However, I decided to automate it by using a python script, crontab, wordpress and MySQL.  My biggest issue was to get python to connect to mysql.

I build a script that fetches every IP address marked as spam in wordpress. Crontab runs this script every week which creates and replaces the .htaccess file with updated data.

Block IP Address Access to your site

It seems that spammers are usually the same people. If your site isn’t that big you can block these people by their IP address with little work. In WordPress every time someone comments on your blog their IP is shown. Below is how to block with Apache .htaccess file.

order allow,deny
deny from
deny from 123.45.6.
allow from all

Blocking of all visitors except yourself:

order allow,deny
allow from
deny from all

Detailed information click here

htaccess python script, Clean URL

I enjoy the clean URL when it looks like

However every time I create a new app I have to re-edit/create a htaccess file. This is too much work and remembering. Below is a python script for windows and linux that once ran inside the root folder of your site it will create an htaccess file to make that clean URL you always wanted. You can also fork it at GitHub

Warning! You may need to configure apache and php for the htaccess file to work. Click me for instructions.


# this script customizes and creates the .htaccess to correspond to the current directory

import os

def create_htaccess():
f = open(‘./.htaccess’,’w’)
# get directory path
directory = os.getcwd()

# if linux
if == ‘posix’:
dirname = directory[directory.rfind(‘/’)+1:]
# if windows
elif == ‘nt’:
dirname = directory[directory.rfind(“\\”)+1:]
exit(‘error occurred: couldnt determine the system this computer is on’)

# make sure you change the root url on line 27 RewriteRule ^(.*)$ directory/index.php?/$1 [L]
RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#’system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#When your application folder isn’t in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename ‘application’ to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ”’+ dirname + ”’/index.php?/$1 [L]
# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php


if __name__ == ‘__main__’: