Install a Web Server into a VirtualBox Machine

Source: http://www.web-development-blog.com/archives/how-to-install-a-linux-web-server-ubuntu/

What?

We want to install a web server into a VirtualBox Machine.

We will use:

  • OS: this guide is valid for any Debian/Ubuntu derivative (using right now Linux Mint 12/13)

  • web server: Apache

  • DB: MySQL

  • DB admin: phpMyAdmin

  • Other: PHP

  • Mail Server: sendmail

How?

Easy! Following these steps!

  1. Install the OS in the VM as usual. (be sure you can reach the network from your guest OS)

  2. First than all install SSH Server, so the rest of this guide can be done through a SSH conection:

    1. sudo apt-get install openssh-server

  3. Install CSF firewall

    1. Install libwww-perl

      1. sudo apt-get install libwww-perl

    2. Get the code

      1. wget http://www.configserver.com/free/csf.tgz

    3. Untar it

      1. tar -xzf csf.tgz

    4. Run installation script (first cd csf)

      1. sudo sh install.sh

    5. Now let’s test that the required iptables modules are working for 100% in our system, type this command:

      1. sudo perl /etc/csf/csftest.pl

      2. You’ll get a small report and if everything looks fine, continue to the next step.

    6. 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 then change also this row TESTING = “1″ to enable the firewall (use ctrl+x to save the file).

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

  4. Install Apache & PHP & MySQL

    1. APACHE

      1. sudo apt-get install apache2

      2. We want to use the MPM prefork module instead of the MPM worker module for the best performance:

        1. sudo apt-get install apache2-mpm-prefork

    2. PHP

      1. sudo apt-get install php5-cgi php5-cli

      2. We install suPHP for more security, suPHP makes it possible that PHP scripts are executed by the user who has created the script. (keep in mind that suPHP needs all scripts run under certain directory, i.e. /var/www, because of this I will create a soft link from the user home dir to a user dir into /var/www) Enter into the terminal:

        1. sudo apt-get install libapache2-mod-suphp

    3. MySQL

      1. sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

        1. Provide a strong password for the MySQL root user!

  5. phpMyAdmin – The first virtual hosting account

    1. If you don’t have yet a user created, create one (usually with Mint 12/13 you create one at the installation, if you do not want to use it, create a new one)

    2. Create a dir for the user into /var/www:

      1. sudo mkdir /var/www/newuser

    3. And give the ownership to the user

      1. sudo chown newuser:newuser /var/www/newuser

    4. Move to your user’s home directory and create a new soft link

      1. ln -s /var/www/newuser public_html

    5. Now we create a host file for Apache:

      1. sudo nano /etc/apache2/sites-available/myphpmyadmin.conf

    6. Inside this new file we will add the following code:

<VirtualHost *:80>
ServerName myhostname.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/newuser/phpmyadmin
ErrorLog /var/log/apache2/phpmyadmin-error.log
LogLevel warn
CustomLog /var/log/apache2/phpmyadmin-access.log combined
</VirtualHost>
  1. Save the file with ctrl+x (you will create the phpmyadmin dir soon)

    1. Enable the site configuration with

      1. sudo a2ensite myphpmyadmin.conf

    2. and reload Apache with

      1. sudo /etc/init.d/apache2 reload

      2. (Apache will say that your dir does not exist… and he’s right… so…)

    3. Return to your user’s home directory and download phpMyAdmin using

      1. wget http://downloads.sourceforge.net/project/phpmyadmin/phpMyAdmin/3.4.3.1/phpMyAdmin-3.4.3.1-english.tar.gz

    4. Untar the gz archive with

      1. tar xvfz phpMyAdmin-3.4.3.1-english.tar.gz

    5. and move the extracted directory to your host directory with

      1. mv /home/newuser/phpMyAdmin-3.4.3.1-english /home/newuser/phpmyadmin

    6. Restart apache with sudo /etc/init.d/apache2 reload and then open your client’s 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)

      1. http://<hostname>/

      2. You will need to edit your /etc/hosts so you can reach the hosted OS’ IP through the hostname you used in the Apache’s configuration file. The best is to set manualy an IP in the hosted OS and the in your client set the hosts. For example if your manualy set IP is 192.168.0.100 and the hostname you set is mynewhostedos, then you will need in your /etc/hosts a line like this one:

    192.168.0.100 mynewhostedos

    Then you can type http://mynewhostedos in the address bar of your browser.

  2. Aditional tasks

    1. PHP packages

      1. sudo apt-get install php5-curl php5-gd php5-mcrypt

        1. You need to fix the #; comment bug inside the mcrypt.ini file or you get depreciated warnings. (The file misses the # before the comment, it has only the semi colon, edit it and add the #, it
          is in /etc/php5/conf.d) Restart Apache after your installed these PHP functions

    2. MySQL super user

      1. 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.

        1. CREATE USER ‘usr’@'localhost’ IDENTIFIED BY ‘pass.’;

        2. GRANT ALL PRIVILEGES ON *.* TO ‘usr’@'%’ WITH GRANT OPTION;

    3. phpMyAdmin access (if your server will become public you need to restrict the access to your DB)

    4. 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>
  1. You need to enter these rules into your host configuration file.

And you are done, you have a web server working.

Now you’ll need to add virtual hosts so you can host different sites with differents URLs, but this is stuff for another post.

Remember 1: to put CSF’s tesing = 0 if you will go to a productive environment.

Remember 2: it’s a good practice to run a package database update frenquently. So, let’s update it now, after installing all the packages above: sudo apt-get update; sudo apt-get upgrade

Optional:
SMTP Server

  1. install postfix (https://help.ubuntu.com/community/Postfix)

  2. If needed configure it to use a ISP’s SMTP (http://www.howtoforge.com/how-to-relay-email-on-a-postfix-server)

  3. Test it:

    1. echo “hello world” | /usr/sbin/sendmail -v your@email.com

  4. To test it fully try with mutt (http://www.hypexr.org/linux_mail_server.php)

About these ads

Posted on October 1, 2012, in English, OS, System Administration, WebServer and tagged , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink. 1 Comment.

  1. I love php and mysql web development and its helped me improve my customer support in many ways. Its an easy yet very powerful tool. I started my search and found what i needed about how and what Web Based Support Desk Software was all about.. Install a Web Server into a VirtualBox Machine Juan Matas de … was easy to learn with and the best thing about it is that I can access my support tickets wherever I go through my iphone. – Fred

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 598 other followers

%d bloggers like this: