Enabling SSH (https) for Apache 2 in Ubuntu/Mint/Possibly other Debian distro


# Intall apache
$ sudo apt-get install apache2

# Enable SSL module
$ sudo a2enmod ssl

# Restart Apache
$ sudo service apache2 restart

# Create a directory to store the SSLCertificateFile and SSLCertificateKeyFile
$ mkdir /etc/apache2/ssl

# Generate the keys
$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt
Generating a 2048 bit RSA private key
...................................+++
................................+++
writing new private key to '/etc/apache2/ssl/apache.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CA
State or Province Name (full name) [Some-State]:Ontario
Locality Name (eg, city) []:Ottawa
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Ronald Pringadi
Organizational Unit Name (eg, section) []:Engineering
Common Name (e.g. server FQDN or YOUR name) []:ronald-mint.com
Email Address []:webmaster@some-cool-website.com

# Edit your hosts file and add "127.0.0.1 some-cool-website.com"
$ gedit /etc/hosts

# Edit your sites-available ssl config
$ gedit /etc/apache2/sites-available/default-ssl.conf
# Make sure you add the following line under the email
ServerName some-cool-website:443
# Also replace
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key

# Activte site
$ sudo a2ensite default-ssl

# Reload Apache
$ sudo service apache2 reload

Open your browser and point it to https://some-cool-website.com. The browser will prompt you that the website is using a self-sign certificate, and do you want to continue and accept that certificate. Answer yes and that’s all to it

Adding New VirtualHost in Apache2

On linux Ubuntu
1. If your apache has /etc/apache2/sites-available directory then just create a new file “dev.newwebsite.com.conf”
with the following content:


ServerName dev.newwebsite.com
DocumentRoot "/var/www/www.newwebsite.com"

# Error handlers
ErrorDocument 500 /errordocs/500.html


AllowOverride All
Options -Indexes FollowSymLinks
Allow from all


2. Also copy the same file into /etc/apache2/sites-enabled

3. Restart your apache

$ /etc/init.d/apach2 restart

4. Edit your hosts file: /etc/hosts
add the following line to it
127.0.0.1 dev.newwebsite.com

5. Open your web browser and open http://dev.newwebsite.com

If you are a windows user and are using xampp then you need to alter the vhost configuration file.
The default xampp file is in
“\xampp\apache\conf\extra\httpd-vhosts”
Then restart your apache

Benchmark How Long a Program Runs In Linux Using Bash

The following bash code might come handy for those of you who want to benchmark how long does a program runs.
In the following example the assumption is that you want to pass along two parameters for your program to run properly.
A time will be recorded at the beginning and at the end of the execution. And a time diff, the processing time, will be reported in seconds.


#!/bin/bash
##################################################
# Benchmark the processing time when a program
# executes with two required parameters: parameter1 parameter2
##################################################

if [[ $1 = "" || $2 = "" ]]; then
echo "Usage: `basename $0` parameter1 parameter2"
else
_start_time=`date +%s`
_parameter1=$1
_parameter2=$2
### YOUR COMMAND HERE WITH parameter1 AND parameter2 ###
_end_time=`date +%s`
_processing_time=$((_end_time-_start_time))
echo "Source File: $_parameter1"
echo "Destination File: $_parameter2"
echo "Start time: $_start_time"
echo "End time: $_end_time"
echo "Processing time is: $_processing_time"
fi

Auto Login Using SSH Public and Private Keys

Assume that your main server (the one you use the most) is ServerA.
And from ServerA, you want to automatically login to ServerB.
For illustration purpose, ServerB can be a repo server and you don’t want to keep being asked for password everytime you want to commit files.

1. At ServerA issue: # ssh-keygen -t rsa
This will create a public + ptivate key for ServerA
2. Enter (empty) for paraphrase
Your identification has been saved in /home/ronald/.ssh/id_rsa.
Your public key has been saved in /home/ronald/.ssh/id_rsa.pub.
3. Copy over the newly created public key from ServerA to ServerB
scp .ssh/id_rsa.pub ronald@ServerB:~/id_rsa.pub.ServerA
Note that once the file arrives at ServerB, it named as id_rsa.pub.ServerA (not id_rsa.pub), and it will be stored at ronald’s home directory at ServerB.

4. Ssh to ServerB, and issue this command:
cat id_rsa.pub.ServerA >> .ssh/authorized_keys
ServerA public key will be merged/appended to ServerB authorized_keys file.

This way everytime you want to access ServerB from ServerA, you won’t be asked for a password.

Linux Soft Symlink Folder or Directory


$ ln -s /home/rpringad/somefolder /home/rpringad/newfolder

#or if your current directory is already /home/rpringad:
$ ln -s /home/rpringad/somefolder /home/rpringad/newfolder

$ ln -s existingSourceFolder newLinkedFolder

How to Set up an FTP Server in Ubuntu Linux

Login to your Linux shell menu as root


[root@locahost]# apt-get install vsftpd
[root@locahost]# vim /etc/vsftpd.conf

At VIM, 1st comment out anonymous_enable by adding a # sign at the beginning of the line
# anonymous_enable=YES
2nd remove comment at local enable by removing the # sign
local_enable=YES


[root@locahost]# /etc/init.d/vsftpd restart

Adding color to your tail

Tail is a very useful tool for monitoring error stream. Sometime the output from tail can have too much information and its black and white monotone output can be hard to follow with the eyes.

The basic: Linux terminal has the capability to colorize text.
for example:
$ echo -e “Sample text nicely highlighted”
will produce a simple text saying, well:
Sample text nicely highlighted

But with some terminal color tagging such as:
$ echo -e “Sample \e[36mtext\e[0m nicely highlighted”
The \e above is identical with \033. I would suggest use \033 because it is safer to be used with a programming language such as PHP. PHP will not recognize \e but it will recognize \033, you’ll see below.
$ echo -e “Sample \033[36mtext\033[0m nicely highlighted”
Sample text nicely highlighted

Some basic coloring table can be seen here: http://www.bashguru.com/2010/01/shell-colors-colorizing-shell-scripts.html

Color Foreground Background
Black 30 40
Red 31 41
Green 32 42
Yellow 33 43
Blue 34 44
Magenta 35 45
Cyan 36 46
White 37 47

With the help of perl, which most likely comes with all linux distros, here is a way to colorize your tail output. Lets’ assume that every time you log something you will have a date-time prefixing your log. For example:

[22-Dec-2011 20:28:45] E_DATASOMETHING ……
Too much information …….
Too much information …….
[22-Dec-2011 20:28:46] E_FATAL Something
Too much information …….
Too much information …….
Too much information …….

Lets create a script that can colorize the date portion.
Create an executable linux bash file:


$ touch logwatch.sh
$ chmod 755 logwatch.sh
$ vim logwatch.sh

The copy and paste the following to your empty logwatch.sh

#!/bin/bash
vNow=$(date +"%d-%b-%Y")
tail -f ~username/errorlogfile.txt|perl -pe "s/$vNow/\e[1;30;32m$&\e[0m/g"

Voilà!

[22-Dec-2011 20:28:45] E_DATASOMETHING ……
Too much information …….
Too much information …….
[22-Dec-2011 20:28:46] E_FATAL Something
Too much information …….
Too much information …….
Too much information …….

If you need something more complex, say you want to highlight several words on the file you can use a scripting power of PHP, I’m rusty with my pearl.
Anyhow, save the script below as “colorize.php”, and you can have the words: blah, na, wa, and — highlighted on the fly!

$ tail -f test.txt|php colorize.php blah na wa --


Fix Microsoft Mouse in Linux Ubuntu

nano /etc/X11/xorg.conf

Section "InputDevice"
Identifier "Configured Mouse"
Driver "mouse"
Option "CorePointer"
# Option "Device" "/dev/input/mice"
Option "Protocol" "ExplorerPS/2"
Option "Emulate3Buttons" "false"
Option "ZAxisMapping" "4 5"
Option "ButtonMapping" "1 2 3 6 7"
EndSection

Colorize Linux Shell Menu

Do you ever feel like you’re going blind because the prompt text color and the result text color are the same?
We here is how to colorize your shell menu

Open your favorite text editor vim or nano, and add this at the end

$ nano ~/.bashrc
PS1='\[\e[1;32m\][\u@\h \t \w]\$\[\e[0m\] '

0;30 black
0;31 light red
0;32 light green
0;33 light yellow
0;34 light blue
0;35 light purple
0;36 light cyan