Setting Up Tomcat Server on Ubuntu

Apache Tomcat is an open source software implementation of the Java Servlet and JavaServer Pages technologies. This post explains the installation and configuration of Tomcat on Ubuntu. I assume the reader to have basic knowledge in Ubuntu.
From Terminal/SSH :
  1. Run update
    sudo apt-get update
  2. You can see the list of tomcat packages by issuing a search command
    apt-cache search tomcat
  3. Install Apache Tomcat using the following command
    sudo apt-get install tomcat7
  4. Install Java
    sudo apt-get install default-jdk
  5. Now you have to set environment variables in bashrc
    sudo nano ~/.bashrc
    Append the following to bashrc (Omit contents after //)
    export JAVA_HOME=/usr/lib/jvm/default-java
    export CATALINA_HOME=/usr/share/tomcat7 //Or tomcat's directory
  6. Reload bashrc by
    . ~/.bashrc
  7. Now start tomcat by
    sudo /etc/init.d/tomcat7 start
  8. If everything was successful
    • sudo /etc/init.d/tomcat7 status
      will show a message, "Tomcat Servlet engine is running with pid [some pid]"
    • Typing server ip address:8080 in your browser will show default response (eg: http://123.456.78.90:8080)
    • Create a index.jsp at /var/lib/tomcat7/webapps/test and write in
      <%= "Hello" />
      Now typing in http://123.456.78.90:8080/test/index.jsp should show "Hello" (without quotes)
  9. If you want to change the default port (8080), you have to edit /etc/tomcat7/server.xml
    sudo nano /etc/tomcat7/server.xml
    Look for
    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
    Refer Tomcat official site for more information
The /var/lib/tomcat7/webapps/ is where you will put your app files. In the list from apt-cache search tomcat you can find helpful stuffs like sample apps, docs etc. You may optionally install these using sudo apt-get install [package] command

Setup LAMP Server, phpMyAdmin, FTP (vsftpd) and .htaccess on Ubuntu

LAMP stands for Linux Apache MySQL and PHP. It is one of the most popular server configuration. This post explains how to set up a LAMP server on Ubuntu and then install commonly used development aids like phpMyAdmin for database, vsftpd for FTP access and then securing sensitive areas using .htaccess. The Steps below requires Root Privileges

  1. Install Ubuntu on the Intended System
  2. Access the terminal from GUI(Applications > Accessories > Terminal) or SSH 
  3. Install Apache by typing : 
    sudo apt-get update
    sudo apt-get install apache2
    Confirm the installation by typing in your server's IP address in your browser's address bar (eg: http://123.456.78.90), you should get a page saying "It Works!"
  4. Install MySQL with the following command
    sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql
    sudo mysql_install_db
    sudo /usr/bin/mysql_secure_installation
    It may prompt you to set root password first and then prompt you to enter it again while setting up the configurations. Enter it as an when required. During the configuration, answer yes to all the yes/no prompts.
  5. Install PHP by typing in the following command
    sudo apt-get install php5
    After installing PHP5 you may want to install additional features like curl. You may see the available packages by typing in
    apt-cache search php5-
    This will give you a list of available packages. You may use sudo apt-get install [package-name] to install the required packages
  6. Voila! You have successfully set up a lamp server. Your php/html files may be put into /var/www/ folder. You may have to restart apache for the changs to take effect. You may do that by typing in
    sudo service apache2 restart
    To test your installation
    cd /var/www/
    sudo nano test.php
    Now type in
    <?php phpinfo(); ?>
    save,exit (CTRL+O to save CTRL+X to exit) and type in http://your server address/test.php (eg: http://123.456.78.9/test.php) and you will get a php info page.

Now you may optionally want to install phpMyAdmin, set up an FTP Server and Secure Sensitive areas(eg Admin panel and phpMyAdmin page) using .htaccess.

 phpMyAdmin installation may be done in two ways. Either you may use

 
sudo wget [url]
to download the zip file to your /var/www/ folder and unzip it using the unzip command, then use it by typing in http://server.address/[phpMyAdmin]( PS :[ ] should contain the folder name to which you unzipped the contents of the zip file downloaded, and you may have to install unzip using the sudo apt-get install command.), or you may install it as following
  1. sudo apt-get install phpmyadmin
    Select apache2 as server and answer YES when prompted on whether to Configure the database for phpmyadmin with dbconfig-common. You will also have to enter the MySQL root password and a login password.
  2. After the installation, add the following lines to /etc/apache2/apache2.conf (sudo nano /etc/apache2/apache2.conf)
    Include /etc/phpmyadmin/apache.conf
    and restart apache with sudo service apache2 restart .
  3. Access phpMyAdmin by typing in http://server.address/phpmyadmin

To Setup FTP server, you have to install and configure vsftpd.

  1. Install vsftpd with the following command
    sudo apt-get install vsftpd
  2. Configure vsftpd by editing /etc/vsftpd.conf
    sudo nano /etc/vsftpd.conf
    Now set the following values(Omit the ones after //)
    anonymous_enable=NO //IMPORTANT
    local_enable=YES
    write_enable=YES
    chroot_local_user=YES //This one restricts the local users to their chroot and denies access to other parts of server
    Save and Exit (CTRL+O CTRL+X). Sometimes(In case of errors) you may have to create a directory within the users home directory( mkdir /home/[username]/ftpFiles) and change the ownership to root(chown root:root /home/[username]) and do your changes in the ftpFiles directory.
  3. Now restart vsftpd
    sudo service vsftpd restart

Configuring .htaccess :

 htaccess file may be used to restrict access to sensitive folders/files in your web server

  1. To enable .htaccess, you need to edit /etc/apache2/sites-available/default.
    sudo nano /etc/apache2/sites-available/default
  2. Look for
    <Directory /var/www/>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride None
      Order allow,deny
      allow from all
      # Uncomment this directive is you want to see apache2's
      # default start page (in /apache2-default) when you go to /
      #RedirectMatch ^/$ /apache2-default/
    </Directory>
    and Change AllowOverride None to AllowOverride All
  3. Restart apache
    sudo /etc/init.d/apache2 reload
  4. Now create a file named .htaccess in the directory that is to be secured
    sudo nano /path/to/directory/.htaccess
    and write
    AuthUserFile /your/path/to/.htpasswd
    AuthName "Authorization Required"
    AuthType Basic
    require valid-user
    To protect a single file, you may add the following to .htaccess file
    <Files "mypage.html"> Require valid-user </Files>
  5. The .htpasswd file will contain the authorized users list. It is advised to save it outside the www folder. To create a new .htpasswd file
    htpasswd -c /path/to/your/.htpasswd user1
    and enter the password. -c is used to create a new file, omit it to add new users to an existing .htpasswd file.
  6. Finally, add these to /etc/apache2/apache2.conf (sudo nano /etc/apache2/apache2.conf)
    <Directory /var/www> AllowOverride All </Directory>
    /var/www may be replaced with whatever path you wish to enable .htaccess, this will enable .htaccess for your whole website
  7. Restart apache server
    sudo /etc/init.d/apache2 restart
  8. Read More on .htaccess file