How to Prepare an Optimized Environment to Run Magento on Ubuntu 14

Try it in our public cloud & Get $5 Credit
CLAIM NOW

Magento is a free, open-source e-commerce platform used by thousands of retailers to power their online stores. The many features of Magento include shopping cart management, conversion tracking and easy catalog customization. There are a wide variety of plugins that can be installed to provide even more features.

This guide will help you get a suitable environment for Magento up and running.

Getting started

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) running a clean installation of Ubuntu 14.
• All commands should be run as the root user
• A LAMP stack using Nginx, PHP and MariaDB

Step-by-step guide

To start the setup process, make sure your server is fully up to date.

apt-get update
apt-get dist-upgrade -y

There are a few required programs that Magento will need. First, install the base applications. At this time, you will also set the root user for MariaDB, which is a common drop-in replacement for MySQL.

apt-get install nginx php5-fpm redis-server memcached php5-dev php5-mysql php5-mcrypt mariadb-server php-pecl

Next, install the two required PHP extensions, php-memcached and php-redis. You should use Redis for managing Magento’s backend cache.

yum -y install php-pecl-memcache php-pecl-memcached

mkdir ~/redis
cd ~/redis
wget https://pecl.php.net/get/redis-2.2.8.tgz
tar zxpf redis*
cd redis*
phpize
make
make install

Make sure to add the memcached and redis modules to the PHP extensions list in php.ini.

nano /etc/php5/mods-available/memcached.ini

; Redis Extension
extension=memcached.so

nano /etc/php5/mods-available/redis.ini

; Redis Extension
extension=redis.so

Naturally, you will need to enable both of these modules.

php5enmod memcached

php5enmod redis

Now it’s time to configure PHP sessions to be stored into memcached. First, configure this setting for the PHP command line interface.

nano /etc/php5/cli/php.ini

COMMENT BOTH FOLLOWING LINES
;session.save_handler = files
;session.save_path = "/var/lib/php5"

ADD BOTH FOLLOWING LINES
session.save_handler = memcached
session.save_path=127.0.0.1:11211

Then you can set it for the PHP-FPM.

nano /etc/php5/fpm/php.ini

COMMENT BOTH FOLLOWING LINES
;session.save_handler = files
;session.save_path = "/var/lib/php5"

ADD BOTH FOLLOWING LINES
session.save_handler = memcached
session.save_path=127.0.0.1:11211

We need to get Nginx to work with PHP now. Create your web directory first.

mkdir /var/www
mkdir /var/www/magento
chown www-data:www-data -R /var/www/magento/
cd /var/www/html/magento
chmod -R o+w app/etc/
chmod -R o+w var/
chmod -R o+w media/

Now add a few tweaks to Nginx’s configuration. Set Nginx to use PHP-FPM for requests.

rm -rf /etc/nginx/sites-enabled/default
touch /etc/nginx/sites-enabled/magento
nano /etc/nginx/sites-enabled/magento

server {
listen 80;
root /var/www/magento;
index index.php index.html index.htm;
server_name magento.domain.com;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /usr/share/nginx/www/404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
location ~ \.(php)$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_keep_conn on;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SERVER_NAME $host;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}

Then you can configure Nginx worker processes.

/etc/nginx/nginx.conf

worker_processes 4;
events {
worker_connections 1024;
# multi_accept on;
}

Configure the PHP-FPM worker as laid out below.

nano /etc/php5/fpm/pool.d/www.conf

pm.max_children = 512
pm.start_servers = 32
pm.min_spare_servers = 32
pm.max_spare_servers = 512

Now, start or restart all services.

service nginx start
service php5-fpm start
service redis-server start
service memcached start
service mysql start

For ease of startup during boot, configure installed services to be automatically started at boot time.

sudo update-rc.d nginx defaults
sudo update-rc.d nginx enable
sudo update-rc.d php5-fpm defaults
sudo update-rc.d php5-fpm enable
sudo update-rc.d redis-server defaults
sudo update-rc.d redis-server enable
sudo update-rc.d memcached defaults
sudo update-rc.d memcached enable
sudo update-rc.d mysql defaults
sudo update-rc.d mysql enable

Insert the famed phpinfo into your webroot for test purposes. This will help in case of any problems with your PHP configuration.

nano /var/www/magento/index.php

<?php
phpinfo();
?>

chown www-data. /var/www/magento/index.php

Install phpMyAdmin, a useful graphical tool for interacting with the databases on your server.

apt-get install phpmyadmin

Link phpMyAdmin to your webroot path.

cd /var/www/magento
ln -s /usr/share/phpmyadmin phpmyadmin

Now, create the initial database for your upcoming Magento deployment. This is where Magento will store the data used during operation.

mysql
create database magento;
CREATE USER 'magento'@'localhost' IDENTIFIED BY 'randompasshere';
GRANT ALL PRIVILEGES ON magento.* TO 'magento'@'localhost';
FLUSH PRIVILEGES;
exit

Now that your environment is set up, you can go ahead and install the actual Magento software.

Conclusion

Magento is the most popular e-commerce system today for online retailers and businesses. With Magento, you can enjoy reliable operation and incredible extensibility to create a seamless shopping experience for your customers, improving conversion and encouraging repeat business. If this guide was helpful to you, kindly share it with others who may also be interested.