How to install Magento on Ubuntu/Debian

By the end of this guide you will have installed Magento, and some sample data, locally on Ubuntu. This will allow you to get to grips with the software before installing it onto a production server. In addition to Magento I will take you through installing Apache, PHP and MySQL. I’m going to assume that you are using Ubuntu 12.04 (Precise Pangolin) and that you wish to install version 1.7.0.0 of Magento. You’re results may vary if you are using different versions.

As this is quite a long post I have broken it down into various sections. Feel free to ignore those parts that are not relevent for you.

  • Getting started.
  • Installing and configuring the Apache HTTP server.
  • Installing PHP.
  • Installing the MySQL database server.
  • Creating the directory from which Magento will be served from.
  • Configuring the Apache Virtual Host.
  • Installing Magento.
  • Running the Magento setup wizard

Getting started.

If you have never heard of Magento the following from the website will explain.

Magento is a feature-rich eCommerce platform built on open-source technology that provides online merchants with unprecedented flexibility and control over the look, content and functionality of their eCommerce store. Magento’s intuitive administration interface features powerful marketing, search engine optimization and catalog-management tools to give merchants the power to create sites that are tailored to their unique business needs. Designed to be completely scalable and backed by Varien’s support network, Magento offers companies the ultimate eCommerce solution.

Magento Frequently Asked Questions

Magento is available in two editions, Community and Enterprise. The Enterprise edition is the company’s commercial version of the software and is meant for large-scale eCommerce users. The Community edition on the other hand is available as a free download under the open source OSL 3.0 license and is the version that you will be installing.

This post will walk you through every thing you need to get a local copy of Magento running on Ubuntu. It is not meant as a guide to installing a fully working eCommerce store located on a production server.

To begin, open up a terminal and enter the command below.

sudo apt-get update

This will ensure that the computer’s database of software packages is updated to contain the latest versions. While this command is not strictly necessary, I tend to issue it before installing any software so that the most up to date versions of the packages are used.

Installing and configuring the Apache HTTP server.

Apache is easily installed by entering the following command.

sudo apt-get install apache2 -y

During the install you may notice the following warning:

apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for

This comes from Apache itself and means that it was unable to determine its own name. The Apache server needs to know its own name under certain situations. For example, when creating redirection URLs.

To stop this warning we can create an Apache config file to store the name. On a production server you would set this as either a hostname or a FQDN, but for local development we can get away with using ‘localhost’.

sudo bash -c "cat >> /etc/apache2/conf.d/servername.conf <<EOF
ServerName localhost
EOF"

In order for this change to take effect restart Apache. The warning should no longer appear.

sudo service apache2 restart

One of the features of Magento is its URL rewriting. By default Magento will use category and product IDs when generating URLs. With the rewrite tool you can create more SEO friendly URLs for your store. So rather than the URL below,

/catalog/product/view/id/16

you can instead use,

/size-6-red-shoes.html

In order to take advantage of this feature we need to enable Apache’s rewrite module with the a2enmod command.

sudo a2enmod rewrite
sudo service apache2 restart

Installing PHP.

As can be seen from the list of system requirements, Magento needs PHP version 5.2.13 or later with the following extensions.

  • PDO_MySQL
  • simplexml
  • mcrypt
  • hash
  • GD
  • DOM
  • iconv
  • curl
  • SOAP

We will therefore install PHP with the following command.

sudo apt-get install php5 php5-curl php5-gd php5-mcrypt php5-mysql -y

Installing the MySQL database server.

The command below will install MySQL. Note that the install process will ask you to create and confirm a password for the root user. Remember the password that you enter as it will be needed later.

sudo apt-get install mysql-server -y

Creating the directory from which Magento will be served from.

Before we get into the business of creating the directory I need to point out a few things. The process below is heavly infulenced by how I setup websites when developing locally. The actual whys and wherefores of how I do this is are too long to go into detail in this post but can be summarised as such.

  • Each site is served from its own directory named after the site’s domain name. E.g, my-example-site.com
  • Group ownership of these directories, and their contents, is set as www-data. The same group that the Apache process runs under.
  • The directories are located in a public_html directory that has been created in my home directory.

Many of the commands that follow use absoulte paths when refering to directories located in my home directory. Since the username on my computer is dev you will need to replace any occurrences of this with your own username. Feel free to also change the location of any of the directories. Just remember that you must use the correct location when configuring the virtual host later on.

The first thing we need to do is ensure that you belong to the same group as the Apache process. Note that after entering the command you must log out and then log back in before the system will recognise that you belong to a new group.

sudo usermod -a -G www-data dev

You can use the groups command to check that you belong to the www-data group. As long as it appears in the list of groups you can move on to creating the required directories.

mkdir /home/dev/public_html
chgrp www-data /home/dev/public_html

As both you and Apache need read and write access to the directory we have used the chgrp command to change its group to be www-data. However, any new files or directories that you create within public_html will not inherit the same group. We therefore need to change the permissions to include the “setgid” bit.

sudo chmod 2750 /home/dev/public_html

We can now create the directory that Magento will be served from and it will be given the www-data group automatically.

mkdir -p /home/dev/public_html/magento-store.com/{public,log}

Configuring the Apache Virtual Host.

We will create a simple virtual host configuration file that will instruct Apache to serve the contents of the directory /home/dev/public_html/magento-store.com/public for any requests to localhost.magento-store.com

sudo bash -c "cat >> /etc/apache2/sites-available/magento-store.com <<EOF
<VirtualHost *:80>

  ServerName  localhost.magento-store.com
  ServerAlias www.localhost.magento-store.com

  DocumentRoot /home/dev/public_html/magento-store.com/public

  LogLevel warn
  ErrorLog  /home/dev/public_html/magento-store.com/log/error.log
  CustomLog /home/dev/public_html/magento-store.com/log/access.log combined

</VirtualHost>
EOF"

Using the a2ensite command and restarting Apache will load the new configuration file.

sudo a2ensite magento-store.com
sudo service apache2 restart

To ensure that the domain localhost.magento-store.com resolves locally to the computer we need to add some entries to the system’s hosts file.

sudo bash -c "cat >> /etc/hosts <<EOF

# DNS for localhost magento store development.
127.0.0.1 localhost.magento-store.com
127.0.0.1 www.localhost.magento-store.com
EOF"

If everything has gone according to plan you should be able to open a browser and navigate to localhost.magento-store.com where you will see a directory listing as shown below.

Browser showing the directory listing when going to localhost.magento-store.com

Installing Magento.

Log into MySQL with the following command. Note that it will prompt you to enter the root user password that you specified as part of the MySQL install.

mysql -u root -p

We need to create a database for Magento and a MySQL user that it can use to access it. For local development it is fine to use the value “magento” for not only the database name but for the user’s name and password. On a production server you would use something a lot more secure.

CREATE DATABASE magento;
INSERT INTO mysql.user (User,Host,Password) VALUES('magento','localhost',PASSWORD('magento'));
FLUSH PRIVILEGES;
GRANT ALL PRIVILEGES ON magento.* TO magento@localhost;
FLUSH PRIVILEGES;
exit

Change into the directory where we wish to perform the install.

cd /home/dev/public_html/magento-store.com/public

You now need to download the tar archive of the source code and unpack it.

wget http://www.magentocommerce.com/downloads/assets/1.7.0.0/magento-1.7.0.0.tar.gz
tar -xzvf magento-1.7.0.0.tar.gz

The source code is located in a directory called magento. Since we don’t want Apache to serve Magento from this sub-directory we need to move the source code out of it.

mv magento/* magento/.htaccess .

We should now tidy up after ourselves by removing any unnecessary files.

rm magento-1.7.0.0.tar.gz
rm -r magento

Since testing an eCommerce store without any products is not much fun we will install some sample data. First download and extract the tar archive that Magento provides for us.

wget http://www.magentocommerce.com/downloads/assets/1.6.1.0/magento-sample-data-1.6.1.0.tar.gz
tar -xzvf magento-sample-data-1.6.1.0.tar.gz

The archive provides a sql file and various assets such as images. The assets need to be moved from the sample sub-directory and put into the media directory of the Magento source. The sql file is also moved to make it easier for loading into MySQL.

mv magento-sample-data-1.6.1.0/media/* media/
mv magento-sample-data-1.6.1.0/magento_sample_data_for_1.6.1.0.sql data.sql

We need to log into MySQL again but this time we will use the MySQL user magento that we set up earlier.

mysql -u magento -p

When prompted enter magento as the password and then enter the following sql statements to load in the sample data.

USE magento;
SOURCE data.sql;
exit

Again we can now remove any unnecessary files.

rm magento-sample-data-1.6.1.0.tar.gz
rm -r magento-sample-data-1.6.1.0
rm data.sql

When you run Magento for the first time you will be taken through the setup wizard. This needs to have permission to create files and directories where necessary. We therefore need to ensure that the correct write permission is set on the following.

  • The directory var
  • The file var/.htaccess
  • The directory app/etc
  • All directories under var/package
  • All directories under media

Since the Apache process runs under the www-data group we can use the chmod command to give write permission to the this group.

chmod g+w var var/.htaccess app/etc
chmod -R g+w var/package/ media

Running the Magento setup wizard.

Now that everything is setup, open your browser and navigate to localhost.magento-store.com where you will start the setup wizard as show below.

Browser showing the Magento setup wizard

You need to accept the terms and conditions before you can continue. Once that’s done fill in the localization settings with the values that are relevant to you.

Next up is the database connection settings which should be filled in with the following information.

  • Database Type: MySQL
  • Host: localhost
  • Database Name: magento
  • User Name: magento
  • Password: magento
  • Tables Prefix: Leave blank

On the same page you will also need to specify the web access options.

The next step is where you create the Admin user. Fill in the required details and choose to let the software generate an encryption key for you. Proceed to the next step and you will have finished installing Magento.

At this point you can view your store at http://localhost.magento-store.com

Browser showing a new Magento store

To access the admin section of the store navigate to http://localhost.magento-store.com/admin/ where you can login using the details set up during the installation.

Browser showing the Admin login page for a Magento store