Skip to Content

How to setup LAMP/LEMP/WAMP for Magento 2

Posted on 11 mins read

With the release of Magento 2.0, many developers started getting their hands dirty with Magento. The first hurdle was preparing a perfect developer machine with all required toolsets. I was one of them, and I faced a lot of trouble while just to run Magento 2 on my machine. So today I am going to show you, how to set up a perfect LAMP/LEMP (or WAMP for Windows users) environment on their machine.

I have divided this tutorial into three sections. I will start from scratch in every environment to give you the better understanding of how things work on each platform.

  1. LAMP (Linux, Apache, MySQL, PHP)
  2. LEMP (Linux, Nginx, MySQL, PHP)
  3. WAMP (Windows, Apache, MySQL, PHP)

Setup LAMP (Linux, Apache, MySQL, PHP) for Magento 2

For this, I am using Ubuntu 14.04 machine. You can also follow this guide on a virtual machine from DigitalOcean or from your favorite provider. Open your terminal and get logged in as a root user, we will install these apps/tools to set up our LAMP machine for Magento 2:

  • Apache 2.4.7
  • MySQL Server 5.6
  • PHP 5.6

Magento 2 and Apache-Ubuntu

The first step is to make sure that our system is up to date. Enter the following command in terminal:

sudo apt-get update
sudo apt-get upgrade

Install Apache2:

So as our system is ready, we can proceed to install Apache web server on our system.

sudo apt-get -y install apache2

This will install Apache 2.4 on your system. Also enable mod_rewrite module for Apache.

sudo a2enmod rewrite

Install MySQL Server 5.6:

Next, Let’s install MySQL server. Magento 2 requires MySQL 5.6 or higher, to install MySQL server follow these commands.

sudo apt-get -y install mysql-server-5.6

To secure your MySQL server enter the command given below. You should select “Yes” for most of the options it prompts:

sudo mysql_secure_installation

Install PHP 5.6:

Now it’s time to install PHP. Magento supports both 5.5 and 5.6 versions of PHP. To install PHP5.6 follow these commands:

sudo add-apt-repository ppa:ondrej/php5-5.6
sudo apt-get -y update
sudo apt-get -y install php5 php5-common php5-cli php5-mcrypt php5-gd php5-intl php5-xsl php5-curl php5-mysql

Don’t forget to enable php5-mcrypt extension.

sudo php5enmod mcrypt

Next, let’s optimize PHP settings for Magento 2.

PHP Configurations:

In order to run Magento 2, we will need tweak some of our PHP configurations. So open php.ini in your favorite text editor:

sudo nano /etc/php5/apache2/php.ini

Change timezone to your timezone. Just uncomment line time zone lines. In my case in php.ini:

date.timezone = Asia/Kolkata

You can find your timezone here http://php.net/manual/en/timezones.php.

Next, we will tweak some performance settings. Find & set following values as in core PHP settings.

## Maximum Execution Time.
max_execution_time = 18000

## Maximum Input Time.
max_input_time = 6000

## Increase Memory Limit (Set more that 768M).
memory_limit = 1024M

## Increase Maximum size of post data.
post_max_size = 128M

## Also increase the Maximum allowed size for uploaded files.
upload_max_filesize = 32M

Also find the line always_populate_raw_post_data and uncomment it. And set it as below:

always_populate_raw_post_data = -1

Save your php.ini file & exit your text editor. In your terminal type this command to restart apache server.

sudo service apache2 restart

If you got a message like “Could not reliably determine the server’s fully qualified domain name…”, then don’t worry just open the apache configuration file add this line at the end.

## Open Apache configuration file.
sudo nano /etc/apache2/apache2.conf

## Add following line at the end.
ServerName localhost

## Restart Apache server
sudo service apache2 restart

And we are done with our PHP configurations. Time to relax!

Creating Virtual Hosts in Apache for Magento 2

If you prefer working your Magento project as http://mage.dev instead of http://localhost/magento/ then this part is for you. So to create virtual hosts in apache follow the steps:

We will create a virtual domain http://mage.dev pointing towards /var/www/magento2 directory. And open terminal type these commands:

sudo nano /etc/apache2/sites-available/mage.dev.conf

And paste the following content.

<VirtualHost *:80>
    DocumentRoot /var/www/magento2
    ServerName mage.dev
    ServerAlias mage.dev 
    <Directory /var/www/magento2>
       Options Indexes FollowSymLinks MultiViews
       AllowOverride All
    </Directory>
</VirtualHost>

Save the file by pressing Ctrl+o in the terminal. Exit from nano editor using Ctrl+x. Enable http://mage.dev site with this command:

sudo a2ensite mage.dev

And restart the Apache server to take effect.

sudo service apache2 restart

Now you can start working on your Magento 2 project at /var/www/magento2 and you can access your project at http://mage.dev. Next, we will need to tell our system to point http://mage.dev towards IP address 127.0.0.1. So open terminal and type these commands:

sudo nano /etc/hosts

And this line at the end:

127.0.0.1    mage.dev

Save file & exit nano editor. That’s it! we are done with setting up LAMP for Magento 2. Now you can proceed with Magento 2 installation.

Setup LEMP(Linux, Nginx, MySQL, PHP) for Magento 2

n the previous section, we used Apache as our web server. But there is a popular alternative for Apache, which performances high & has low memory usage. If you prefer Nginx over Apache2, then you have good news! Magento 2 also support Nginx web server.

Magento 2 and Nginx-Ubuntu

We are using Ubuntu 14.04 (Trusty Tahir). You can also follow this guide on droplet(virtual server) from DigitalOcean. We will install these apps/tools on our system:

  • Nginx 1.4.6
  • MySQL 5.6
  • PHP-fpm 5.5.9

So let’s update our system with latest updates.

sudo apt-get -y update
sudo apt-get -y upgrade

Install Nginx web server:

Let’s install Nginx server first.

sudo apt-get -y install nginx

Install MySQL Server:

After installing Nginx, install MySQL server 5.6:

sudo apt-get install -y mysql-server-5.6

## Also for securing your MySQL installation 
sudo mysql_secure_installation

Install PHP-fpm:

Now let’s install PHP. Nginx web server doesn’t have native support for PHP. So we have to use PHP-FPM which is called as “FastCGI process manager”. You can get information about php5-fpm on their website http://php-fpm.org/.

So install PHP-fpm and other required extension by using this command:

sudo apt-get install -y php5-fpm php5-cli php5-mcrypt php5-gd php5-intl php5-xsl php5-curl php5-mysql

Also, don’t forget to enable mcrypt extension:

sudo php5enmod mcrypt

PHP Configurations

It’s time to tweak some of our PHP settings to support Magento 2 installation.

So open php.ini for php5-fpm in you favorite text editor.

sudo nano /etc/php5/fpm/php.ini

And change these settings in php.ini

## Set timezone. 
## Find your time zone here http://php.net/manual/en/timezones.php
date.timezone = Asia/Kolkata

## Maximum Execution Time.
max_execution_time = 18000

## Maximum Input Time.
max_input_time = 6000

## Increase Memory Limit (Set more that 768M).
memory_limit = 1024M

## Increase Maximum size of post data.
post_max_size = 128M

## Also increase the Maximum allowed size for uploaded files.
upload_max_filesize = 32M

## Find & set "always_populate_raw_post_data" as shown below
always_populate_raw_post_data = -1

Save the file and exit your exit your editor. Open terminal & restart both Nginx & PHP5-FPM services.

sudo service php5-fpm restart
sudo service nginx restart

And we are done with our PHP configurations. Next, we will see how we can set up a virtual host in Nginx for Magento 2.

Creating Virtual Hosts in Nginx for Magento 2

If you are working on Magento project on a Nginx web server, then you probably will want to set up http://mage.dev to pointing towards your project path /var/www/magento2. We are going to do the same in upcoming steps.

Open your terminal and enter the following command:

sudo nano /etc/nginx/sites-available/mage.dev

It will open nano editor with a blank file. Just paste the following content in that file.

Save and exit the nano editor. Now enable the site by creating a symbolic link.

sudo ln -s /etc/nginx/sites-available/mage.dev /etc/nginx/sites-enabled/

Next step in to restart both Nginx & PHP-FPM services.

sudo service nginx restart
sudo service php5-fpm restart

After that, let’s point http://mage.dev to local IP address 127.0.0.1. Open the terminal and enter this command to edit the hosts file.

sudo nano /etc/hosts

And paste this line at the end & save the file.

127.0.0.1    mage.dev

And we are finally done! Now we can proceed for Magento 2 Installation.

Setup WAMP (Windows, Apache, MySQL, PHP) for Magento 2

If you are a Windows user like me, you can also setup your Windows machine for Magento 2. We are going to AMPPS instead of WAMP because AMPPS makes things a lot easy to set up. It also provides interface to create Virtual hosts like http://mage.dev.

Magento 2 and AMPPS

So grab the installer for yourself from http://ampps.com/download. And install it like any regular Windows software. For this tutorial, I am using AMPPS version 3.4.

PHP Configurations

After installing AMPPS, you will need to enable php_intl & php_xsl. They are PHP extensions required for Magento 2. To enable them, open AMPPS Control Panel and click PHP section.

PHP Section AMPPS

Next, click on Php Extension to enable our PHP extensions. On clicking this option a select box with PHP extensions will pop up.

select-php-extension-for-magento-2

Then select “php_intl” & “php_xsl” from the extension list. Also make sure that other extensions, which are required by Magento 2 are selected and hit Apply. After this, we will need to restart Apache server. So to restart Apache server, again click on Control Panel and under Apache logo, click on restart button.

ampps-restart-apache

That’s it! We are finished with PHP configuration for AMPPS. Next, we will see how to create virtual hosts in AMPPS.

Creating Virtual Hosts in AMPPS for Magento 2

It’s so easy to set up virtual hosts in AMPPS. Just open http://localhost/ampps/ in your favorite browser. And click on Add Domain icon.

ammps-add-domain

Then add your virtual host info. mage.dev and project path to your Magento root.

add-domain-page-ammps

And click on Add Domain. That’s it! no need to play with Apache configuration files. Next, we will need to tell Windows to point domain mage.dev to our local IP address 127.0.0.1.

So find Notepad in the start menu and Run it as Administrator. Open “hosts“ file located here at C:\Windows\System32\drivers\etc & add this line at the end of the file.

127.0.0.1    mage.dev

Save file and exit the editor. And we are finally done 🙂 Hurrah!!!

Installing Magento 2

So that now, you are done with setting up your system for Magento 2, let’s get a quick overview on how to install Magento 2.

Install Composer

We are going to install Magento 2 using composer, which is an easy way to install Magento 2. If you don’t know about Composer yet, then let me tell you it is a PHP dependency manager. You can use it to manage PHP libraries for your project. You can get more information Composer on their official site https://getcomposer.org.

So to install Composer in your Linux(Ubuntu) system, enter these two commands:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Windows user can directly grab an MSI installer from their official site https://getcomposer.org/download/. After installing Composer it’s to install Git on our system.

Install Git

Git is version control system for your project. To install git on your system enter following command.

sudo apt-get install -y git

Windows users can grab a Windows installer from https://git-scm.com/. After installing git let’s get back to installing Magento 2.

Install Magento 2

Before we start installing Magento 2 using Composer, please make sure that you have configured a swap partition on your system (Windows users don’t worry about swap partition). Check for swap partition like this:

free -m

## It will show output like this
total used free shared buffers cached
Mem: 2048 357 1690 0 0 237
-/+ buffers/cache: 119 1928
Swap: 0 0 0

If you find output like this (Swap: 0 0 0), then you will need to configure swap like this:

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1

To install Magento 2, open your terminal & change current directory /var/www.

cd /var/www

Windows users should change their directory to C:\Program Files (x86)\Ampps\www.

And enter next command to install Magento 2 Community Edition using Composer.

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition <installation directory name>

It will ask you form authentication keys. Get your ones from Magento Connect Account from here https://www.magentocommerce.com/magento-connect/customerdata/secureKeys/list/.

Also, it will ask you to enter a token from Github, you can generate a token for yourself from Your Account > Settings > Personal access tokens which is located here https://github.com/settings/tokens.

This process will take some time depending on your internet connection. After the process is completed, Linux users will need to set up permissions for Magento 2 installation files.

Setting File Permissions

To set up file permissions, change your directory to the base directory of your Magento installation. Enter this command

cd /var/www/magento2

Then set up ownership for files using this command (Applies for Ubuntu users).

sudo chown -R :www-data .

Next setup file permissions using this command.

sudo find . -type d -exec chmod 770 {} \; && sudo find . -type f -exec chmod 660 {} \; && sudo chmod u+x bin/magento

And we are done! You can now continue installation by opening this address in the browser http://mage.dev/setup/.

Deploy static files

If you are intended to use Magento 2 in production mode (Checkout more on Magento modes), then you must deploy static view files to Magento’s filesystem using this command (Thanks to Talesh Seeparsan @_Talesh).

php bin/magento setup:static-content:deploy

You can find more information on deploying static view files here.

Conclusion

And that was another long tutorial on Magento 2. More are coming in upcoming days. I am still learning new things in Magento 2 & I will bring more articles on these as I will be exploring Magento 2. Please do send me your suggestions, also please ask me any question you have in the comments below. I will try my best to help you out.

Follow me on Twitter @swapnil_bhavsar, tweet me your suggestions, questions… I will be happy to help you! Thanks 🙂

ICONS CREDITS : http://www.myiconfinder.com/

comments powered by Disqus