Wednesday, June 25, 2008

Virtual Hosts on Apache2

This is a very useful feature of Apache2. Imagine if you have multiple domain names and wish to reduce hosting cost by hosting all the web sites on a single server, now you can do it through virtual hosting.

Basically, this features enables multiple domains such as
www.example.com
www.example.net
www.example.org
to point to the same machine (i.e. IP) and yet their hosting directory are different on the machine.
Eg, you may store files related to www.example.com in /var/www/example.com , www.example.net in /var/www/example.net, etc...
This is to enable easy management of each site affecting the other sites that are also hosted on the same machine.

There are 4 steps to doing this
1) Edit /etc/host so that the DNS resolution is pointing to the correct machine.
Of course, in production, this means updating your domain name service to point to your server machine.

2) Create the directories for the sites (eg /var/www/example.com, etc) and copy the files to the respective directories.

3) Edit /etc/apache2/conf.d/virtual.conf (create if the does not exist)
The file should contain the following line

NameVirtualHost *

4) Individual host configuration files are stored at /etc/apache2/sites-available. Create your host configuration files in /etc/apache2/sites-available, then create a symbolic link to those files in the sites-enabled directory - this will cause them to be actually loaded/read.
The symbolic links can be created using the Debian utility command, a2ensite and a2dissite to enable and disable the sites.

Example of content of the /etc/apache2/sites-available/www.example.com

#
# Example.com (/etc/apache2/sites-available/www.example.com)
#

ServerAdmin webmaster@example.com
ServerName www.example.com
ServerAlias example.com

# Indexes + Directory Root.
DirectoryIndex index.html
DocumentRoot /home/www/www.example.com/htdocs/

# CGI Directory
ScriptAlias /cgi-bin/ /home/www/www.example.com/cgi-bin/

Options +ExecCGI



# Logfiles
ErrorLog /home/www/www.example.com/logs/error.log
CustomLog /home/www/www.example.com/logs/access.log combined


Lastly, run (sudo) a2ensite www.example.com
Restart Apache2 and test out the URL.

#ref: http://www.debian-administration.org/articles/412

Wednesday, June 18, 2008

FTP and SFTP

Setting up FTP server on Ubuntu

sudo apt-get install proftpd gproftpd
*gproftpd is the GUI module for proftpd

Configure the default ftp directory through
sudo gedit /etc/proftpd/proftpd.conf
This is basically the configuration file for proftpd. Reminder to check tie user login to their home directory if need be.
To enable anonymous / general login, need to setup a generic ftp login for all users of the system. *anonymous login does NOT require password. We can also the read and write permission for each directory in this file.


sudo /etc/init.d/proftpd restart


Access Control List for FTP
/etc/ftpusers
/etc/ftphosts
/etc/ftpaccess
*ftphosts -> can use ip domain range or *.domainname.com

Enhanced security. Since authentication protocol for FTP is in plaintext, we should enable TLS/SSH login through SFTP to encrypt username and passsword for ftp logins.




Reference
http://ubuntuforums.org/showthread.php?p=429783

Boot script sequence

Start up order of linux
1) /etc/lilo.conf
2) /etc/inittab
3) /etc




Boot sequence in Red Hat
boot/grub stuff
/etc/inittab
/etc/rc.sysinit script
/etc/init.d stuff ->rc0,rc1,rc2,rc3,etc..
/etc/rc.d/rc.local


Runlevel Scripts Directory
(Red Hat/Fedora Core)
State
0 /etc/rc.d/rc0.d/ shutdown/halt system
1 /etc/rc.d/rc1.d/ Single user mode
2 /etc/rc.d/rc2.d/ Multiuser with no network services exported
3 /etc/rc.d/rc3.d/ Default text/console only start. Full multiuser
4 /etc/rc.d/rc4.d/ Reserved for local use. Also X-windows (Slackware/BSD)
5 /etc/rc.d/rc5.d/ XDM X-windows GUI mode (Redhat/System V)
6 /etc/rc.d/rc6.d/ Reboot
s or S
Single user/Maintenance mode (Slackware)
M
Multiuser mode (Slackware)


After the runlevel script is done, the kernel will execute scripts in one of the following directories.
  • /etc/rc.d/init.d/ (Red Hat/Fedora )
  • /etc/init.d/ (S.u.s.e.)
  • /etc/init.d/ (Ubuntu / Debian)
Init Script Activation:

Adding a script to the /etc/rc.d/rc#.d/ directory with either an S or K prefix, adds the script to the boot or shutdown process. The scripts are run in numerical order. S20abc is run before S30xyz. The extensibility to the boot and shutdown procedures of the operating system is one of the strengths of UNIX. The orderly sequential initiation of processes can be coordinated for dependent processes. The orderly shutdown of processes is often required of complex programs such as databases. This is how it is done. Individual processes may be monitored, shutdown and started at any time using these scripts. i.e. /etc/rc.d/rc2.d/httpd start. The modifiers start, stop or status may be used.

The start/stop/status scripts actually reside in the directory:

  • /etc/rc.d/init.d/ (Red Hat/Fedora)
  • /etc/init.d/ (S.u.s.e. and Ubuntu / Debian)
and are linked to the appropriate directories. These links may be created or destroyed using the chkconfig command. i.e. chkconfig --del httpd will remove the web server from the startup and shutdown process. Inversely chkconfig --add httpd will add it to the startup/shutdown process by generating links from the script in /etc/rc.d/init.d/ to the appropriate /etc/rc.d/rc#.d/ directory. For more information see the LINUX manual page on init.

The rc.local script is the last system script to be executed. This is equivalent to autoexec.bat in windows. This is where most of the customised commands/scripts are located.
Meaning, /etc/init.d is the central depository of all scripts.
Files in rc?.d are symbolic links to /etc/init.d.
Eg if runlevel 3 is selected, scripts in /etc/rc3.d which are symbolic links to scripts in /etc/init.d are executed.

Tuesday, June 17, 2008

Cron Jobs

Troubleshooting Cron
1) Uncomment the #cron line in /etc/syslog.conf to enable cron logging
2) Restart sysklogd : sudo /etc/init.d/sysklogd restart
3) Add cron jobs through crontab -e
4) Restart cron : sudo /etct/init.d/cron restart

To disable email notification, redirect the output to /dev/null
0 3 * * * /root/backup.sh >/dev/null 2>&1


Special strings to substitute * * * * *

Special string Meaning
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".

User crontabs are saved in
/var/spool/cron


*Cron status reporting
Set up Evolution to read mails sent by cron.
Create a new account and select "local delivery mails" from the dropdown box. This will enable Evolution to receive mails sent to /var/mail/


*cron commands are also logged in auth.log

Alternative to cron jobs
If a particular task needs to be schedule to be run once or a few times, an alternative solution would be to use the "at" command. This command basically allows the user to schedule a job at any particular time. Eg to send out an email at 4am next week.

at -f /home/user/atcommand -m now + 7 days

The f options tells the at command to run the content of a file (which is in a script format situated at /home/user/atcommand). The -m options will tell the system to send out an email (which contains the output of the command) to notify the user once the job is done and now + 7 days indicates that this job will be executed 7 days from now.

*make sure that there's either /etc/at.deny or /etc/at.allow file in your system. Else, onyl the root will be able to use the at command. These are basically ACL for at command.

You can query all at command jobs using atq and remove the jobs using atrm


Anacron for systems that are not up and running 24/7.
For each job, Anacron checks whether this job has been executed in the last n days, where n is the period specified for that job. If not, Anacron runs the job's shell command, after waiting for the number of minutes specified as the delay parameter.

After the command exits, Anacron records the date in a special timestamp file for that job, so it can know when to execute it again. Only the date is used for the time calculations. The hour is not used.

When there are no more jobs to be run, Anacron exits.
Only root can schedule anacron jobs. A way to enable other users to do so is by creating an anacron group and enable write permission for this group on /var/spool/anacron (where job timestamps are stored)

"run-parts" executes all the executable files in a directory. For example:
# run-parts /etc/cron.daily

"run-crons" executes all the scripts in the /etc/cron.daily, /etc/cron.hourly, /etc/cron.monthly, and /etc/cron.weekly directories.

*used to find files in directory that is messy, file criteria is *junk*

find /tmp -name '*junk*' -exec ls -l {} \;


To force anacron to be executed hourly, put a script which consist of the following in /etc/cron.hourly
#!/bin/sh
/usr/sbin/anacron -s

Sunday, June 15, 2008

Installing PHP5

Assuming that you already have apache2 installed, the next step is to install PHP5. It is recommended that you install phpmyadmin and mysql as well.
1) Start by selecting and installing your modules through the synaptic package manager.
OR by command line
sudo apt-get install php5 mysql phpmyadmin
*please install PHP5.gd is you intend to use drupal
2) To enable PHP5, sudo a2enmod PHP5 and restart apache2
3) Test whether php pages are parsed by entering http://localhost/ and click on phpadmin
4) If cannot access that page (greeted by save php or phtml page as) , then you need to edit /etc/apache2/apache2.conf
look for the line AddType ****
and add in "AddType application/x-httpd-php .php .phtml"
5) Restart apache2 and you should be able to access the phpmyadmin page

Unable to start in Linux GUI

If you ever get an error logging into a linux GUI because the system is complaining that it ran out of space, here are some steps to free up some hard disk space to enable you to login (to do further cleaning)

1) At the GUI login , press Ctrl + Alt + F1 to login in terminal
2) Use df -h to check hard disk utilization information
3) Try deleting files in /home which you know can be deleted.
4) Try deleting files in /tmp
5) mount a USB drive and move some of your files in /home to the USB drive and delete them from the system.

Installing Apache 2 in Ubuntu Feisty

Before installing, please ensure that apache is not installed, else uninstall it.
*Apache modules are reported not to work with Apache2 modules.
1) To uninstall Apache
sudo apt-get remove apache
*alternatively, this can be done through sypnatic package module
2) Update apt-get's package list
sudo apt-get update (to subscribe to updated packages)
3) Install Apache2
sudo apt-get install apache2
4) The system should print out a message saying that apache2 is running. To test,
type http://localhost/ in a browser. You should be able to see the apache-default folder.
5) If step 4 fails, check /etc/default/apache2
ensure that NO_START = 0 to enable apache2 to start at port 80.
It could be that NO_START = 1 if apache was installed in your system and prevented Apache2 from starting at port 80.
6) Restart apache2
sudo /etc/init.d/apache2 restart

* commands to clean up source codes / downloaded files which are no longer in used
sudo dpkg --purge apache apache2

Installation of Ubuntu in Vmware Fusion

Installing Ubuntu Feisty in a virtual machine (VMWare Fusion)
1) Allocate at least 3Gb of space for the virtual drive (about 200Mb will be needed to install VMTools)
2) Boot up ISO image in VMWare Fusion and follow the installation steps.
3) Upon booting into Ubuntu, click on install VMWare Tools in the VMWare tab
4) Press the extract button and save vmware-tools-distrib to your home directory.
5) Enter the following commands:
cd vmware-tools-distrib
./vmware-install.pl
*The default answers are all suitable - but the process takes out your network connection.
./vmware-config-tools.pl.
6) When prompted to enter password when executing sudo commands, enter the admin's (usually the first user registered in Ubuntu) password and NOT the root password.
7) Root password can be set in System / Administration / Users and Groups in the GUI.