Ubuntu 12.04 Web Server

33
by on May 8, 2012 at 11:28 pm
Ubuntu MySQL php Apache

The Ubuntu Web Stack

In this article, I’m going to be outlining the steps to install and configure a complete web server on a base install of Ubuntu 12.04 LTS server edition.

I personally don’t prefer to install package groups relating to “LAMP” or similar during initial installation because I’m simply too lazy to review each and every package that is included in those groups, and what the dependencies are.  By installing the required packages using apt-get, this allows you to install just the software you’re looking for, without worrying about dependencies.  This software stack is time tested; we’ll install on Ubuntu’s latest release, and test a few popular applications while we’re at it!

 

 Requirements:

A newly installed Ubuntu 12.04 LTS Server Edition with no additional packages.  Need help with this step?  Check here.

Required packages:  apache2, php5, mysql-server, php5-mysql

Optional packages:  To configure this server remotely, you will need to have installed openssh-server.  This can be achieved by the following in terminal:

apt-get install openssh-server

Note:  This and many other guides on this site assuming you are performing operations as root (or other super user) unless otherwise stated.  By default, root login is prohibited on Ubuntu, even in the terminal.  During installation, the administrative account created has sudo permissions.  I suggest sudoing to root; otherwise, feel free to append ‘sudo’ and full path names as necessary.

Step 1:  Installing Apache2

Though often referred to as just ‘apache’ the name of the package we need to install is apache2.  To begin, log into your system’s terminal.  Run the following command:

apt-get install apache2

Type ‘Y’ as appropriate.  If you have a fast network connection, this should take a matter of seconds!

To test, on another machine that has connectivity to yours, type your server’s IP in a web browser, your you can type the following:

wget localhost index.html
ls

Note the created index.html file in your current working directory.  If you don’t have this file, then apache2 might not be installed.

To test this further, try the following:

rm index.html
service apache2 stop
wget localhost index.html

This will have stopped the apache2 service.  Note the error in the output of wget, about the connection being refused.  Also note there is no index.html.  Restart apache2 using:

service apache2 start

That completes the default installation of apache2.

Step 2:  Configuring Apache2

While there are entire books written on configuring Apache, what I hope to outline here is basic default settings and typical production-ready setup.  I won’t be covering how to run Apache2 from a chroot jail or anything fancy like that (which is pointless, IMO) as those steps are available elsewhere are not specific to the task at hand.

Now that apache2 has been installed, you can see the requisite startup script has been placed in /etc/rc2.d/

apache2 startup script

contents of /etc/rc2.d

The number may vary, but what’s important is the S, which stands for start.  This script is a soft link to /etc/init.d/apache2.

The location of the apache2 config file is

/etc/apache2/apache2.conf

Those of you familiar with Red Hat and similar distros will note the httpd.conf file, which has been created but is empty by default.  httpd is the name of the package/service of apache2 on Red Hat and similar distros.

Contained in the same directory as our config file, is a subdirectory named  conf.d.  Any files with the suffix .conf in this directory will be parsed by apache2 during startup.  It’s common practice to put config files in this directory if you’re running multiple sites (or subdomains) on the same server.  Create the following file:

/etc/apache2/conf.d/mysite.com.conf

and paste the following into it:

<VirtualHost *:80>
ServerAdmin webmaster@mysite.com
DocumentRoot /var/www/mysite.com
ServerName www.mysite.com
ErrorLog /var/log/apache2/error.log
</VirtualHost>

We have now configured our first virtual host named www.mysite.com.  The first line of that file tells apache which IP and port to listen on for this virtual host.  In our case, *:80 means listen on any IP, port 80.  You can put in your server’s IP here if you like, but *:80 is fine.

The second line, ServerAdmin is optional, and so is the last line ErrorLog.  Whatever settings or options aren’t configured inside the <VirtualHost> tags will be inherited from apache2.conf or other config files.

The third line denotes the directory where you server’s web content will reside.  The default location for apache2 is /var/www.  I simply have chosen to create a subdirectory named mysite.com in that directory; another common location is a subdirectory in the /home directory.  This is up to you, and will vary upon your individual needs.

The fourth line is the host header apache2 will listen for, as sent by the end-user’s web browser.  As you may know, web resources can be accessed via IP address of the server, or a domain name as resolved by DNS.  On systems with more than one site, apache2 will read the domain name and present the appropriate content.

Now that you have created your virtual host’s config file, we need to create the directory we referenced, and assign it the appropriate permissions.

After you create the directory, create a simple index.html file inside of it, with whatever content you like.  Then, perform the following:

chown root:www-data /var/www/mysite.com -R
chmod 550 /var/www/mysite.com -R

The user/group www-data is the default user which will run the apache2 process.  I suggest setting permissions this way, only adding write permissions as absolutely necessary by your application.

Next, we want to restart apache, so it will read our new configuration file.  This should proceed without error.

 

Now, to test that our virtual host is working as intended, on a remote system on which you have administrative access, edit your hosts file, and add the following entry:

youriphere www.mysite.com

Substituting youriphere with…your web server’s IP!  Don’t know where the host file is on your system?  Look here:  wikipedia

Now, on the system with the edited hosts file, type in www.mysite.com.  This should display the contents of index.html that you created!  If not please check the steps outlined above carefully!

Another option to test is to create the entry in /etc/hosts on the webserver itself, and use wget just as we did above, except this time use www.mysite.com/index.html as the target file!

Step 3: Installing php5

If you don’t have apache2 up and running yet, please complete that step first.

To install php5 perform the following:

apt-get install php5

This should only take a few minutes to complete.  The default location for the config file is:

/etc/php5/apache2/php.ini

While most settings are appropriate by default, I suggest making the following change:

safe_mode = on

Next, let’s rename index.html to index.php, and replace the contents with the following:

<?php
echo \'hello\';
?>

Restart apache2.  Now, visit www.mysite.com again.  The only text on the screen should be “hello”.  If this is the case, you have successfully installed php5!

 

Step 4: Installing MySQL

Install mysql-server:

apt-get install mysql-server
apt-get install php5-mysql

Set password for the root user.  Make this a strong password.  I recommend a 14+ character passphrase with mixed case, numbers, and special symbols.  Make it something you can remember, or store it in a safe place that’s NOT ON THE WEBSERVER.  Next, restart apache2.

Next, we’re going to setup a new database for our web application, create a new user, and assign that user permissions only access that db.

mysql -p #enter the root password when prompted on the next line

CREATE DATABASE webdb;

CREATE USER ‘webuser’@'localhost’ IDENTIFIED BY ‘password_here’; #Be sure to replace ‘password_here’ with a strong password.

GRANT ALL PRIVILEGES ON webdb.* to webuser@’localhost’;

exit

This should complete the steps for MySQL.  Next, let’s install WordPress!

Step 5:  Install WordPress

This part is obviously optional, but I’m including it as it demonstrates that everything is working properly.

Download wordpress, or upload it via sftp to your server.

wget http://wordpress.org/latest.tar.gz

Next move to, unzip and untar the archive in our site’s document root.  We’ll need to modify the root of the WordPress installation as well.

tar zxfv latest.tar.gz
chmod 770 /var/www/mysite.com
chown www-data:www-data /var/www/mysite.com

Now, let’s open up our web browser on our desktop, and attempt the installation!

Everything should go smoothly!  I recommend changing permissions on our site’s folder back to 550 after installation is complete.

Further Reading:

Check back at a later date for more articles relating to iptables and other ways to secure your webserver!

in Distros, How-To, Ubuntu

, , , , , , , ,

You can skip to the end and leave a response. Pinging is currently not allowed.

33 comments on “Ubuntu 12.04 Web Server

  1. Pingback: Installing Ubuntu 12.04 LTS Server Edition

  2. Pingback: CentOS 6 Web Server

  3. kris on said:

    This was really helpful. I would also recommend including the enabling of mod_rewrite in the apache section, especially since it’s required with wordpress customizations and a common requirement in general.

  4. Thanks a lot for the blog article.Really looking forward to read more.

  5. Thank you. Information I can understand.
    regards
    Bob

  6. matthew on said:

    this is very helpful, and exactly what i was looking for. just going to throw the only question i have at the moment out there to see if anyone can answer.

    I started looking into this installation after stumbling upon a site for drupal, i’m wondering how drupal fits into all of this, if anyone is familiar with drupal. Thanks in advance.

  7. Great article, I enjoyed reading it.

  8. zachary on said:

    i have a testing home ubuntu webserver that im running in virtualbox. i’ve got everything setup and i was wondering if i could forward a webpage from a free webhosting to my virtual server?

    • The short answer is no. Web pages need a static IP, which most ISPs don’t provide to home users. There are some work arounds for a personal site, but certainly not a good idea for a business site.

      • Steven on said:

        You most certainly can use a home webserver to point to a domain. I have done this. Really easy. Register a domain name , lets say abc.com . Now go sign up for a dyndns account or no-ip account, either one works fine they do the same thing. so lets say you register abc.dyndns.info. you configure your external NIC card on your gateway as the public ip , then go into the gateways settings and find where you can set up dyndns. youll want to use the first public ip on the default route (which is your public ip address). dyndns keeps up with your changing public ip address.

        so now you have two domains.

        abc.dyndns.info
        abc.com

        you want the end user to browse to abc.com

        so go into your dns manager for your domain, select domain forwarding and point it to abc.dyndns.info and select forward with masking. this way it covers up the ugly name and users get redirected to your dyndns address! then all you have to do is tell your gateway to forward the appropriate ports to the external interface using NAT rules and viola! you have a free hosting solution WITHOUT a static ip and hosting services. also you dont have to go in and change the public ip.

        cheers

  9. Clover Sg on said:

    Very clear and informative.
    Should inform users that they should restart the web-server if they want to see any results if they change the configuration files

  10. Very well written and concise tutorial. Thanks.

  11. gabriel on said:

    Well, got an error on the first instruction. Searched the internet. Solution: have “sudo” in front. Instead of this:
    apt-get install openssh-server
    you should have this:
    sudo apt-get install openssh-server

    That’s how it worked for me.

  12. Chairman on said:

    At installing Php5

    Next, let’s rename index.html to index.php, and replace the contents with the following:

    …..
    You said rename index.html to index.php . but mind telling where this index.html was you referering to ?

  13. Chairman on said:

    At installing Php5

    Next, let’s rename index.html to index.php, and replace the contents with the following:

    …..
    You said rename index.html to index.php . but mind telling where this index.html was you referering to ?

  14. Omar Yesid Mariño on said:

    You are amazing! Thank you very much!

  15. Sunith on said:

    Hi while installing Php5 how do I rename index.html to index.php and repalce the contents?
    Thank you

  16. dfdfdd on said:

    hi, thanks for the useful tutorial!
    However regarding the php

  17. dfdfddsimon on said:

    In your php code:

    ‘echo’ should be ‘Echo’ else will get no output
    eg

  18. Brian on said:

    You leave out all of the most important details.

  19. danny on said:

    Great stuff.

    One comment though, when I run the php5 index.php code, nothing shows up on my website. Have any idea what it could be?
    I am running on 12.04 Ubuntu and have downloaded the php5 package.

    Thanks

  20. My query comes from after the server has been set up. If I want to access it from a different computer, say from work or elsewhere, how do I go about doing this? I have it set up on a virtual machine, and can connect to the server using the computer the virtual machine is set up on, but as soon as I jump to any other computer I am unable to open the webpage?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Categories