Ubuntu 14.04 Web Server Tutorial

9
by on April 29, 2014 at 4:38 pm

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 14.04 LTS server edition.  Not only will you learn how to install a complete web server or “LAMP stack” from the command line, you’ll also understand a little bit more about how each service works.  Ubuntu LTS releases are proven server platforms, and 14.04 brings many needed updates to the LAMP stack, most notably Apache Server 2.4

I personally don’t prefer to install “Web server” package groups during server install time.  I like to install each necessary package one by one to ensure I only have the software that I require for my operation.  This tutorial is also useful if you’re running Ubuntu 14.04 desktop version and want to install a LAMP stack for testing or development purposes.



Requirements:

An Ubuntu 14.04 LTS install.  Each version (Desktop or Server) should work about the same, but keep in mind I wrote this tutorial using the server version.

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!

Ubuntu and Debian based systems automatically assumes that you want to have apache2 (and several other services) started every time you boot your system as soon as you install them.  init scripts are included with the packages and installed to /etc/init.d/  On the server, the default run level for Ubuntu is 2.  If you ls /etc/rc2.d/ you’ll see a list of files that control the starting and stopping of services when that run level is entered.  You should see a script that beings with ‘S’ followed by a number and ends with ‘apache2′ which are links to the file in /etc/init.d/ If you want to start apache2 manually on your system, you can remove the startup scripts by running  update-rc.d -f apache2 remove

Of note:  Many system services are controlled by Ubuntu’s upstart system.  These system services can be controlled with ‘start <service>’, ‘stop <service>’, and ‘status <service>’.  At this time, apache2 does not seem to fall into that category.  To start, stop, or see the status of apache2, use the service command:  service apache2 <command>

To test that your installation is running and processing requests, you can use wget localhost/index.html

This should create a local file in your working directory.  cat that file and make sure there are some contents in that file.

Step 2:  Configuring Apache2

Web Root

The default public directory for apache2 is /var/www/html  .  This is where your html and php files will typically reside.

I prefer to give my web files the following permissions:

chown root:www-data /var/www/html -R

chmod g+s /var/www/html

chmod o-wrx /var/www/html -R

This changes the group ownership of all the web root and all files therein to apache2′s user, www-data.  Setting g+s tells the file system to create all new files in that directory with the same group ownership.

Config files

Most software and services in linux have associated config files.  In most major distributions, those files are located in the /etc directory tree.  apache2 is no different, many important config files are located in /etc/apache2 .  Our default config file is /etc/apache2/apache2.conf  .  This file contains import settings for apache2, such as logging and a few basic security settings.

Two subdirectories that are important are /etc/apache2/sites-available and /etc/apache2/sites-enabled

Typically, sites-available hosts config files defining VirtualHosts.  VirtualHosts allow apache2 to server multiple websites instead of just one.  For instance, you can have completely different websites for www.mydomain.com and www2.mydomain.com, or any number of different domain names.  These sites are defined in .conf files inside this directory.

sites-enabled is a directory which typically contains symlinks to files in sites-available.  Apache will load the files found in sites-enabled upon startup.  You may also elect to just put the .conf’s in this directory itself instead of symlinking, that is also very common.

New VirtualHost

Let’s create our first virtual host.  First, paste the following into /etc/apache2/sites-available/mysite.com.conf

<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, let’s create our web root (aka DocumentRoot) folder and place a new index.html file into it.  mkdir /var/www/mysite.com

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

chmod g+s /var/www/mysite.com

Paste whatever you like into /var/www/mysite.com/index.html , obviously, a simple html web page would be fine.

Okay, now let’s cd /etc/apache2/sites-enabled and create a symlink to our new config file.  ln -s ../sites-available/mysite.com.conf

That will tell apache2 to load our newly created .conf file upon restart:  service apache2 restart

Everything should be fine, you might see a warning about “Could not reliably determine servers fqdn…”  That’s fine, it just indicates that there’s not a proper DNS entry for our server, or at the very least apache wasn’t able to determine it.  This will not affect the operation of your server for our purposes.

To test to make sure apache2 is actually serving our new page on our new virtualhost, let’s update our hosts file.  Add the following line to /etc/hosts

127.0.0.1 www.mysite.com

This will over ride www.mysite.com for your system, all of those requests will resolve to your local loopback IP, 127.0.0.1

You should now be able to run wget www.mysite.com/index.html and see the contents of your file placed in the working directory.  I recommend you cd ~ so you don’t accidentally create junk files in your config directories.

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

Most settings are appropriate by default.  Upon writing, the version that ships with Ubuntu 14.04 is 5.5.9.

Next, let’s rename index.html to index.php mv /var/www/mysite.com/index.html /var/www/mysite.com/index.php, and replace the contents with the following:

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

Restart apache2.  Now, visit www.mysite.com again (or use wget www.mysite.com/index.php ).  The only text on the screen should be “hello from php!”.  If this is the case, you have successfully installed php5!

Step 4: Installing MySQL / MariaDB

Since Oracle’s acquisition of Sun Microsystems, the Linux community at large has been a little weary of MySQL’s future.  Many distros are now supporting MariaDB as a drop in replacement for MySQL.  At this time, the two seem to be mostly compatible, and most software is supported by / supports either package.  As of this writing, Ubuntu officially supports MySQL 5.5 as the default for this LTS release, so it may be prudent to choose that.  MariaDB is also available in the repositories, however.

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.  As with apache2 above, after installation, mysql will start automatically by default.  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 g+w /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!  After install, run chmod g-w /var/www/mysite.com to remove write permissions from that folder.  Some folders may require additional permissions, please consult the wordpress documentation for further details.

in How-To, Ubuntu

, , , , ,

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

9 Comments

  • Os

    24/05/2014

    Hi!
    I tried to run a simple PHP code to test and not running anything

    • Mike

      27/05/2014

      Is there any step you had trouble with?

    • Josh Cooper

      21/07/2014

      In my case, the escape characters ‘/’ weren’t necessary and caused it to throw errors in the log and return a blank screen to the user – so just take those out and see if it runs correctly. If not, check the /var/www/apache2/error.log and see what the message is. It can point you in the right direction

  • ki

    03/06/2014

    i stuck at configure apache2. can you explain more?

    • Mike

      05/06/2014

      What is the problem you are having?

  • mig

    17/06/2014

    Nice tutorial, thanks !

    I suggest adding:

    FLUSH PRIVILEGES;

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

  • ahma siddiqui

    20/06/2014

    I need to change the document root to something else
    for example I want my root to chnage from /var/www/html to ~/Dropbox/www

    To achieve that I’ve changed my /etc/apache/sites-availabl/000-default.conf file

    when I restart the server it seems fine

    But When I open http://localhost/
    it gives me a forbidden error
    “Forbidden

    You don’t have permission to access / on this server.

    Apache/2.4.7 (Ubuntu) Server at msg.co Port 80″

    • Mike

      22/06/2014

      Make sure you apache group has read permissions on the files, and read and execute permissions on the directory.

Leave a Reply

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

* Copy This Password *

* Type Or Paste Password Here *

Categories