How-to Install a Linux Web Server (Ubuntu)

Installing a web server for a production environment, with Ubuntu as operating system, is not too difficult because most of the required packages don’t need a lot of configurations. In this tutorial we create a (power) user, we install Apache, PHP and MySQL, phpMyAdmin and several other services needed to run a powerful and secure web server.

Preparations
LinuxFirst you need a web server (VPS or a dedicated server). Try to get a cloud server, they are very powerful, redundant and flexible. Choose a Linux image like Ubuntu 10.04 LTS and validate that the SSH server is up and running. Don’t use an image where any web service is pre-installed.

Creating the “Admin” user

    1. Login via SSH using root user account
    2. Create a new user with useradd -d /home/newuser -m newuser (-d points to the directory and -m will create the directory). Use passwd newuser to create a password for the user.
  1. Make the new user owner of the newly created user directory: chown newuser:newuser /home/newuser
  2. Add the new user to the sudo group using adduser newuser sudo
  3. Optional: change the “shell” for this user with: chsh -s /bin/bash newuser
  4. Restrict root access; use the command sudo passwd -l root to disable the root password. Open a second terminal window login with the new user name and close the first terminal window ONLY if your new account works (test an admin command using sudo first).

Installing Sendmail

We don’t install a mailserver like postfix, but we need a simple mail demon which is able to send simple mail messages.

  1. Install the sendmail program using sudo apt-get install sendmail
  2. Test the program using
    echo "hello world" | /usr/sbin/sendmail -v your@email.com

Install the CSF Firewall

To finish the installation of csf the LWP perl module (libwww-perl) has to be installed. The csf setup script will stop the installation process if this package is not yet installed. If this is the case install the module using sudo apt-get install libwww-perl and restart the csf setup script.

  1. Change to your home directory and download csf using
    wget http://www.configserver.com/free/csf.tgz
  2. Untar the downloaded file: tar -xzf csf.tgz
  3. Change into the csf directory cd csf and start the install script (as root) sudo sh install.sh
  4. Now let’s test that the required iptables modules are working for 100% in our system, type this command sudo perl /etc/csf/csftest.pl. You get a small report and if everything looks fine, continue to the next step.
  5. Open the csf config file sudo nano /etc/csf/csf.conf, check the default port numbers and eventually other settings. Each setting is well documented, if you’re ready than change also this row TESTING = “1” to enable the firewall (use ctrl+x to save the file).
  6. Now we need to restart the csf service using sudo csf -r, open a second terminal and login using SSH. You need this extra step to be sure that your firewall doesn’t have blocked ssh access for yourself ;)

Installing Apache, PHP and MySQL

  1. First we install Apache: sudo apt-get install apache2
  2. We want to use the MPM prefork module instead of the MPM worker module for the best performance: sudo apt-get install apache2-mpm-prefork
  3. Next we can install PHP using sudo apt-get install php5-cgi php5-cli (these two libs are enough for the moment, we will add more of them later)
  4. We install suPHP for more security, suPHP makes it possible that PHP scripts are executed by the user who has created the script. Enter into the terminal:
    sudo apt-get install libapache2-mod-suphp
  5. Now install the MySQL service:
    sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql. Provide a strong password for the MySQL root user.

Create your first virtual hosting account

We created already a user in the begin of this tutorial, now we like to install phpMyAdmin as our first web application.

  1. Move to your user’s home directory and create a new directory mkdir phpmyadmin
  2. Now we create a host file for Apache:
    sudo nano /etc/apache2/sites-available/myphpmyadmin.conf
  3. Inside this new file we will add the following code:
    <VirtualHost *:80>
    	ServerName myhostname.com
    	ServerAdmin webmaster@localhost
    	DocumentRoot /home/newuser/phpmyadmin/
    	ErrorLog /var/log/apache2/phpmyadmin-error.log
    	LogLevel warn
    	CustomLog /var/log/apache2/phpmyadmin-access.log combined
    </VirtualHost>
  4. Save the file with ctrl+x
  5. Enable the site configuration with sudo a2ensite myphpmyadmin.conf and reload Apache with sudo /etc/init.d/apache2 reload.

Now we’re able to install phpMyAdmin for the virtual host we have just created.

  1. Return to your user’s home directory and download phpMyAdmin using
    wget http://downloads.sourceforge.net/project/phpmyadmin/phpMyAdmin/3.4.3.1/phpMyAdmin-3.4.3.1-english.tar.gz
  2. Untar the gz archive with tar xvfz phpMyAdmin-3.4.3.1-english.tar.gz and move the extracted directory to your host directory with
    mv /home/newuser/phpMyAdmin-3.4.3.1-english /home/newuser/phpmyadmin
  3. Open your web browser, enter the server name in the address bar and you should see the phpMyAdmin login page. (you can login with the MySQL root password)

Additional tasks

We mentioned before that you should install additional PHP libs.

    • Install these common PHP libraries:
      sudo apt-get install php5-curl php5-gd php5-mcrypt You need to fix the #; comment bug inside the mcrypt.ini file or you get depreciated warnings. Restart Apache after your installed these PHP functions
    • Create a kind of super user for your database using phpMyAdmin, it’s much safer to use a different user than the “root” user for normal database operations.
    • Your phpMyAdmin host is accessible for everyone, you should protect your database tool against bots using
              <Directory /home/newuser/phpmyadmin>
              	Options Indexes FollowSymLinks MultiViews
      		AllowOverride None
      		Order allow,deny
      		# add here your IP addresses
      		allow from 100.100.100.100
      	</Directory>

      You need to enter these rules into your host configuration file.

That’s all so far, add additional hosts and install your websites. A DNS service is not part of this tutorial, use instead the DNS zone from your domain name provider and create A records for your server’s IP address.

If you have any problems using this tutorial for your own server or for any other question please post your comment below.

Published in: Web Hosting

11 Comments

  1. Hi,
    Nice tutorial!
    I’m missing a PHP cache module, eAccelerator is a good choice.
    While I’m looking on your requirements, I think you should be able to follow the steps from my own eAccelerator tutorial.

    Thanks for sharing!

  2. Sounds Good.
    Many cloud ISPs like Rackspace discourage installing Sendmail. We need not budge.
    I would like to advice using webmin to automate many things like virtual hosts.

    1. Hi,
      I have a Rackspace cloud server too and they are okay for sendmail if you use this mail service only for you webhosted applications. Webmin is a great idea, but only as GUI, my advice is to not install packages with Webmin and apt-get together.

  3. I’ve used most of the steps for installing a test server on my laptop. While moving the phpmyadmin directory to his destination I’ve noticed that I need to use this option to the “mv” command:

    -T, –no-target-directory treat DEST as a normal file

    $ mv -T phpMyAdmin-3.4.3.1-english phpmyadmin

    without that option the extracted directory was moved complete INTO the destination directory.

  4. After creating the host file for phpmyadmin I got this error message:

    “Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName”

    I fixed it this way:

    Open this config file
    sudo nano /etc/apache2/httpd.conf
    and add the directive
    ServerName localhost
    save the file and restart Apache
    sudo /etc/init.d/apache2 restart

  5. You forgot to mention the following suPHP configuration detail:

    “docroot=/var/www:${HOME}/public_html”

    must be

    “docroot=/var/www:${HOME}/public_html:/home/newuser/public_html”

    since the example location is not included here a 500 error is given. Depending on the user/group ID you need to change these rows too:

    ; Minimum UID
    min_uid=100

    ; Minimum GID
    min_gid=100

    (compare these IDs with your user/group ID in “/etc/group”

  6. You forgot to disable automatic directory listing, I added this directive to my “/etc/apache2/httpd.conf” file:

    Options -Indexes

    Reload your configuration after that: $ sudo /etc/init.d/apache2 reload

  7. Hi Olaf,

    this is the most accurate and the most up-to-date step-by-step how-to tutorial I could find for installing and configuring a web server. Thank you for that.
    Now I will try to describe where I have found some problems in following your tutorial:

    ubuntu version used: Ubuntu 10.04.3 LTS Codename: lucid
    new user = marius

    01. I am having a problem with point 28, if I try to access web server from my LAN (http://192.168.230.109/) I get this message:
    “It works! This is the default web page for this server. The web server software is running but no content has been added, yet.”
    but I can not see the page of phpMyAdmin where to login with the MySQL root password
    if I try to access:
    http://192.168.230.109/phpmyadmin/
    I get this error message:
    “Not Found The requested URL /phpmyadmin/ was not found on this server. Apache/2.2.14 (Ubuntu) Server at 192.168.230.109 Port 80”
    this is my server used path:
    root@albert:/home/marius/phpmyadmin# ls
    browse_foreigners.php navigation.php server_synchronize.php

    trying to search on other forums I have found this command:
    sudo dpkg-reconfigure phpmyadmin
    but here is what I get using it:
    root@albert:/home/marius/phpmyadmin# sudo dpkg-reconfigure phpmyadmin
    Package `phpmyadmin’ is not installed and no info is available.
    Use dpkg –info (= dpkg-deb –info) to examine archive files,
    and dpkg –contents (= dpkg-deb –contents) to list their contents.
    /usr/sbin/dpkg-reconfigure: phpmyadmin is not installed
    root@albert:/home/marius/phpmyadmin#

    Where do you think the problem is, and how may I get to the phpMyAdmin page
    Thank you in advance

    1. Hello Marius,
      That will not work this way. Your default host is in “/var/www” and your phpMyAdmin install is in “/home/marius/phpmyadmin”
      You need to create a host file for phpMyAdmin first.

      My advice for phpMyAdmin in this tutorial is maybe not the default install method but much easier to solve if there are problems with suPHP and phpMyAdmin

  8. Today I have noticed that not all “default” Ubuntu Lucid distributions are the same.
    If you have problems with some packages (getting errors like “Package php5-suhosin has no installation candidate”) than update your source list with “nano /etc/apt/sources.list” and add/replace the entries with:

    deb http://archive.ubuntu.com/ubuntu/ lucid main restricted universe
    deb-src http://archive.ubuntu.com/ubuntu/ lucid main restricted universe

    deb http://archive.ubuntu.com/ubuntu/ lucid-updates main restricted universe
    deb-src http://archive.ubuntu.com/ubuntu/ lucid-updates main restricted universe

    deb http://security.ubuntu.com/ubuntu lucid-security main restricted universe
    deb-src http://security.ubuntu.com/ubuntu lucid-security main restricted universe


    Safe (ctrl+x) and update your repositories with “apt-get update”.

  9. Since Ubuntu version 12.04 I get this error while installing packages:

    perl: warning: Please check that your locale settings:

    To remove this warnings for my EN / NL installation I need to run this commands:

    locale-gen en_US en_US.UTF-8 nl_NL nl_NL.UTF-8
    dpkg-reconfigure locales

Comments are closed.