ECommerce Insights Blog

Stay on top of it all and get ahead with useful articles, how-tos, tips and tricks on e-commerce.

Magento 2 Installation with Composer For CentOS & Ubuntu

Installing Magento 2 On CentOS or Ubuntu Using Composer

Unlike its predecessor Magento 2 seems far better and in sync with latest PHP development best practices. But for those who always install a software using setup wizards Magento 2 could be little different and somewhat confusing.

The following guide will help you setup composer, install Magento 2 community edition and dependent libraries on your server or test server. Just ensure you have minimum required hardware on your server keeping in mind that Magento is a resource hungry platform and consumes minimum 512MB RAM. Depending upon your requirement you can increase it upto 4GB RAM specially if you have hundreds of products. When indexing starts you will see Magento consuming too much resources and then stabilizes back to normal. See the system requirements here.

Install LAMP on Ubuntu

First and foremost lets update the Ubuntu apt-get library, this will ensure the upto date libraries can be installed. Skip this step if LAMP is already installed on your server. In your terminal type the following command

apt-get update

Now, lets install the LAMP stack to support Linux, Apache PHP & MySQL . In your terminal type the following command

sudo apt-get install lamp-server^

Post installation you need to specify a secure password for MySQL which we will be using later when we will install Magento 2.

Setting up Apache VirtualHost

Since we are going to install Magento 2 on live or test server lets see how to create a VirtualHost .conf file for Apache to redirect traffic to appropriate directory. Skip this step if you already have a virtual host setup for your Magento 2 install. You can use your favorite editor nano, vim or vi to create and edit a .conf file.

vim /etc/apache2/sites-available/magento.conf

Copy the following lines of code and paste in your editor and save the file


    DocumentRoot /var/www/public/sitedirectory
    
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
    

Now run the following commands in your terminal one after another

 sudo a2ensite magento.conf
 sudo a2dissite 000-default.conf

All done, it time to reload the Apache using the following command

service apache2 reload

Install LAMP on Centos 6.5+

First and foremost lets update CentOS, to get latest and up-to-date libraries can be installed. Skip this step if LAMP is already installed on your server. In your terminal type the following command

yum update

We are big fan of Webtatic, lets install the LAMP stack to support Linux, Apache PHP & MySQL . In your terminal type the following command

wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh epel-release-6-8.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm

Install Apache on CentOs 6.5

yum install httpd httpd-devel

Install PHP on CentOs 6.5

yum --enablerepo=webtatic-testing  install php56w.x86_64  php56w-cli.x86_64  php56w-xmlrpc.x86_64 php56w-xml.x86_64 php56w-tidy.x86_64 php56w-soap.x86_64 php56w-recode.x86_64 php56w-pspell.x86_64  php56w-process.x86_64 php56w-phpdbg.x86_64 php56w-pdo.x86_64  php56w-odbc.x86_64 php56w-mysql.x86_64  php56w-mbstring.x86_64  php56w-gd.x86_64  php56w-fpm.x86_64  php56w-devel.x86_64 -y

Install MySQL, PHPMyAdmin on CentOs 6.5

yum install mysql mysql-devel mysql-server mysql-client -y
/etc/init.d/mysqld start
mysqladmin -uroot password 'A VERY STRONG PASSWORD HERE'
yum install phpmyadmin

Don’t forget to change the line ‘A VERY STRONG PASSWORD HERE’ with a strong password for your MySQL which we will be using later when we will install Magento 2.

Setting up Apache VirtualHost

Since we are going to install Magento 2 on live or test server lets see how to create a VirtualHost .conf file for Apache to redirect traffic to appropriate directory. Skip this step if you already have a virtual host setup for your Magento 2 install. You can use your favorite editor nano, vim or vi to create and edit a .conf file.

vim /etc/httpd/conf.d/magento.conf

Copy the following lines of code and paste in your editor and save the file


ServerName www.yoursitename.com
ServerAlias yoursitename.com
DocumentRoot /var/www/html/sitedirectory
 
     Options ExecCGI -Indexes FollowSymLinks
     DirectoryIndex index.html index.html.var index.php

     AllowOverride all
      Order allow,deny
      Allow from all (or your IP address)
   

Just replace yoursitename.com with your domain name and replace sitedirectory with the directory we intend to create to install Magento 2 on your server under /var/www/html/ folder.

Now run the following commands in your terminal one after another

 chkconfig httpd on
 chkconfig mysqld on

All done, it time to reload the Apache using the following command

/etc/init.d/httpd restart

Install LAMP on CentOS 7

First and foremost lets update CentOS, to get latest and up-to-date libraries can be installed. Skip this step if LAMP is already installed on your server. In your terminal type the following command

yum update

We are big fan of Webtatic, lets install the LAMP stack to support Linux, Apache PHP & MySQL . In your terminal type the following command

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install yum-plugin-replace

Install Apache on CentOS 7

yum install httpd httpd-devel -y

Install PHP on CentOS 7

yum replace php-common --replace-with=php56w-common
yum --enablerepo=webtatic-testing  install php56w.x86_64  php56w-cli.x86_64  php56w-xmlrpc.x86_64 php56w-xml.x86_64 php56w-tidy.x86_64 php56w-soap.x86_64 php56w-recode.x86_64 php56w-pspell.x86_64  php56w-process.x86_64 php56w-phpdbg.x86_64  php56w-pgsql.x86_64 php56w-pdo.x86_64  php56w-odbc.x86_64  php56w-mysql.x86_64  php56w-mbstring.x86_64  php56w-gd.x86_64  php56w-fpm.x86_64  php56w-devel.x86_64 -y

Install MySQL, PHPMyAdmin on CentOS 7

yum remove MariaDB-server MariaDB-client MariaDB-shared mysql mysql-server 
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
yum install Percona-Server-client-56 Percona-Server-server-56
systemctl restart mysql.service
mysql_secure_installation
yum --enablerepo=webtatic-testing  install phpmyadmin -y

Read how to install percona server here.

Setting up Apache VirtualHost on CentOS 7

Since we are going to install Magento 2 on live or test server lets see how to create a VirtualHost .conf file for Apache to redirect traffic to appropriate directory. Skip this step if you already have a virtual host setup for your Magento 2 install. You can use your favorite editor nano, vim or vi to create and edit a .conf file.

vim /etc/httpd/conf.d/magento.conf

Copy the following lines of code and paste in your editor and save the file


  ServerName yoursitename.com
  ServerAlias  www.yoursitename.com
  DocumentRoot /var/www/html/sitedirectory/
    
   Options -Indexes FollowSymLinks
     AllowOverride all
     Order allow,deny
     Allow from all
   

        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^yoursitename\.com$ [NC]
        RewriteRule ^(.*)$ http://www.yoursitename.com$1 [R=301,L]



 CustomLog /var/www/html/sitedirectory/access_log common
 ErrorLog /var/www/html/sitedirectory/error.log


Just replace yoursitename.com with your domain name and replace sitedirectory with the directory we intend to create to install Magento 2 on your server under /var/www/html/ folder.

All done, it time to reload the Apache using the following command

systemctl restart httpd.service

Installing Composer for Magento 2

Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP. You can install Composer with just one line of command

curl -sS https://getcomposer.org/installer | php

If you wish, you can additionally install Composer globally so you don’t have to type php /path/to/composer.phar every time. Run the following code in terminal

mv composer.phar /usr/local/bin/composer

Download Magento 2

There are 2 quick ways to download Magento 2 from command line:

  1. Via Git
  2. Via Composer

Download Magento 2 via Git

To clone a git repository from Magento 2 in your /var/www/html/sitedirectory/ (CentOS) or /var/www/public/sitedirectory (Ubuntu)

git clone [email protected]:magento/magento2.git

Now go inside your sitedirectory and type the following command

composer install OR
php composer.phar install

Download Magento 2 via Composer

Just run the following command within your sitedirectory folder

composer create-project magento/community-edition magento2 -s dev --prefer-dist

If you need sample data as well here is a little modification to the above script

composer config repositories.magento composer http://packages.magento.com

And then,

composer require magento/sample-data:0.42.0.beta1 magento/sample-data-media:0.42.0-beta1 --dev

It may take a while to complete due to large size of sample data. Once the sample data is downloaded run the following command for sample data installer

php dev/tools/Magento/Tools/SampleData/install.php --admin_username=username

Setting up Required Folder & File Permissions for Magento 2

As per official Magento 2 guidelines you should run the following command to setup correct permission set

find . -type d -exec chmod 700 {} \; && find . -type f -exec chmod 600 {} \;

Create the database for Magento 2 site

You should create a database under which all tables for your site will reside. You can either create a database from PHPMyAdmin or from command line using the following command

echo "CREATE DATABASE MY_DATABASE_NAME" | mysql -u[mysqluser] -p

Replace MY_DATABASE_NAME with the database name you intend to create.

Finally, launch the setup wizard in your browser to install community edition of Magento 2. Open up yoursitename.com and install as per wizard flow. Magento 2 also comes shipped with command line installation, just paste the following command in terminal

php setup/index.php install
--db_host=localhost \
--db_name=MY_DATABASE_NAME\
--db_user=root \
--db_pass=canbempty \
--backend_frontname=admin \
--admin_username=username \
--admin_password=password \
[email protected] \
--admin_firstname=First \
--admin_lastname=Last \
--base_url=http://www.yoursitename.com/ \
--base_url_secure=http://www.yoursitename.com \
--language=en_US \
--currency=USD \
--use_rewrites=true \
--use_secure=false \
--use_secure_admin=false \
--cleanup_database

I hope the above guide will help you setup Magento 2 Community edition on a fresh server. Please leave us a comment and share you views or questions with us.