Multiple Remote Virtual host and sub domains

Just by doing whats below the server will automatically recognize if its requesting baligena.com or felipe.baligena.com 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/baligena.com

    # domain: baligena.com
    # public: /var/www/mulsite/

    <virtualhost *:80>

      # Admin email, Server Name (domain name) and any aliases
      ServerAdmin webmaster@baligena.com
      ServerName  baligena.com
      ServerAlias www.baligena.com


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

$ sudo a2ensite baligena.com
$ sudo service apache2 reload

Resource:
https://www.linode.com/wiki/index.php/Configure_apache_to_use_virtual_hosts_on_ubuntu_server


For subdomain:
configure you dns provider godaddy or namecheap.com
https://community.namecheap.com/forums/viewtopic.php?f=6&t=193

$ sudo nano /etc/apache2/sites-available/baligena.com

    # domain: felipe.baligena.com
    # public: /var/www/mulsite/

    <virtualhost *:80>

      # Admin email, Server Name (domain name) and any aliases
      ServerAdmin webmaster@baligena.com
      ServerName felipe.baligena.com
      ServerAlias www.felipe.baligena.com


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

$ sudo a2ensite baligena.com
$ sudo service apache2 reload


the default page should be /var/www/

tags:
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:
http://localhost/cgi-bin/web.py

script:
#!/usr/bin/python
import cgi
import cgitb
import urllib2
cgitb.enable()

# 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('http://facebook.com')
print response.read()

print "hello world"

tags:
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 site1.com and site2.com
$ mkdir /var/www/site1.com /var/www/site2.com

Modify your host file
$ sudo nano /etc/hosts

Add the two lines of code into that file
127.0.0.1 site1.com
127.0.0.1 site2.com

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

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

site2.com file:
<VirtualHost *:80>
DocumentRoot /var/www/site2.com
ServerName site2.com
ServerAlias site2.com
</VirtualHost>

Final configuration commands
$ sudo a2ensite site1.com
$ sudo a2ensite site2.com
$ sudo service apache2 reload

Now if you put either site1.com or site2.com 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 127.0.0.1 for ServerName

$ sudo nano /etc/apache2/httpd.conf

add the line below:
ServerName localhost

Resource.

tags:
AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. 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
</IfModule>

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
    </Directory>

</VirtualHost>

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

Resource

tags:
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.

In WAMP:
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.
; http://php.net/error-log
; 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"

Reference

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

Reference

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/httpd.pid
Remove it before continuing if it is corrupted.

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

tags:
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.

stop_spam.py

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 255.0.0.0
deny from 123.45.6.
allow from all

Blocking of all visitors except yourself:

order allow,deny
allow from 255.0.0.0
deny from all

Detailed information click here

htaccess python script, Clean URL

I enjoy the clean URL when it looks like http://baligena.com/home/page

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.

createhtaccess.py

 

# 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 os.name == ‘posix’:
dirname = directory[directory.rfind(‘/’)+1:]
# if windows
elif os.name == ‘nt’:
dirname = directory[directory.rfind(“\\”)+1:]
else:
exit(‘error occurred: couldnt determine the system this computer is on’)

f.write(”’
# http://codeigniter.com/wiki/mod_rewrite
# 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

”’)
f.close()

if __name__ == ‘__main__’:
create_htaccess()