Magento is an open source e-commerce software and content management system for e-commerce websites based on the PHP Zend Framework. It uses MySQL or MariaDB as database backend. The Magento development has been started in 2008 by Varien.
In this tutorial, I will show you how to install Magento 2 with Nginx, PHP-FPM, and MariaDB as the database. I will use ubuntu 15.10 (Wily Werewolf) as a basis for the installation. If you don't have a minimal server setup yet, please see this tutorial to get a proper base setup.
Prerequisites:
- Ubuntu 15.10 - 64 bit.
- Root Privileges.
Step 1 - Install Nginx
Login to your Ubuntu server with your root account (e.g. by SSH) and update the repository.
sudo su
apt-get update
Then install Nginx:
apt-get install nginx -y
Verify that nginx has been installed properly by checking the port:
netstat -plntu | grep 80
apt-get update
Step 2 - Install and Configure PHP-FPM
In this step we will to install PHP 5.6 in PHP-FPM mode. Additionally we will install the following PHP extensions that are required by magento.
- php-gd
- php-mhash
- php-mcrypt
- php-xsl
- php-pear
- php-soap
Install the packages with the apt command below:
apt-get install php5-fpm php5-mhash php5-mcrypt php5-curl php5-cli php5-mysql php5-gd php5-xsl php5-json php5-intl php-pear php5-dev php5-common php-soap libcurl3 curl -y
We have to enable the php5-mcrypt extension manually by creating a symlink in the conf.d directory.
cd /etc/php5/fpm/conf.d
sudo ln -s ../../mods-available/mcrypt.ini
cd /etc/php5/cli/conf.d
sudo ln -s ../../mods-available/mcrypt.ini
Now edit the php.ini files for fpm and cli.
vim /etc/php5/fpm/php.ini
vim /etc/php5/cli/php.ini
and increase the memory limit and php execution time and turn on zlib compression by adding the following lines at the end of the files:
memory_limit = 512M
max_execution_time = 1800
zlib.output_compression = On
Save and exit.
Restart the PHP-FPM service to apply the configuration changes:
systemctl restart php5-fpm
sudo ln -s ../../mods-available/mcrypt.ini
cd /etc/php5/cli/conf.d
sudo ln -s ../../mods-available/mcrypt.ini
vim /etc/php5/cli/php.ini
max_execution_time = 1800
zlib.output_compression = On
Step 3 - Install and Configure MariaDB
I will use MariaDB instead of MySQL here. Install MariaDB with the apt command:
apt-get install mariadb-server mariadb-client -y
Set the MariaDB root user password with this command:
mysqladmin -u root password mypassword
Then connect to the MySQL shell (the MariaDB shell gets started with the command MySQL) with your root password, create a database with the name 'magentodb' and a user 'magentouser' with the password 'magentouser@'. Please choose a secure password for the 'magentouser' on your server and not the one 'magentouser@' that I used in this example!
Login to the MySQL shell:
mysql -u root -p
In the MySQL shell, run these commands:
create database magentodb;
create user magentouser@localhost identified by 'magentouser@';
grant all privileges on magentodb.* to magentouser@localhost identified by 'magentouser@';
flush privileges;
\q
Database created and configured.
create user magentouser@localhost identified by 'magentouser@';
grant all privileges on magentodb.* to magentouser@localhost identified by 'magentouser@';
flush privileges;
\q
Step 4 - Install and Configure Magento 2
We will install Magento in the directory '/var/www/magento2'. For the Magento installation, we need the PHP composer.
Install php composer
Go to the root directory, download the composer installer file with curl and run it to install composer.
cd ~/
curl -sS https://getcomposer.org/installer | php
Move the file 'composer.phar' file to the bin directory of your server and rename it to composer so it can be executed easily:
mv composer.phar /usr/bin/composer
Now verify that composer command is working:
composer -v
curl -sS https://getcomposer.org/installer | php
- Download and Extract Magento 2
Go to the web directory '/var/www/' and download Magento from it's Github repository, then unpack the downloaded tar.gz file:
cd /var/www/
wget https://github.com/magento/magento2/archive/2.0.0.tar.gz
tar -xzvf 2.0.0.tar-gz
mv magento2-2.0.0/ magento2/
Done.
wget https://github.com/magento/magento2/archive/2.0.0.tar.gz
tar -xzvf 2.0.0.tar-gz
mv magento2-2.0.0/ magento2/
- Configure the Magento Key
Register an account on the Magento website repo.magento.com. This account is required to use Magento and the Magento composer store. When you have registered, go to the Tab 'My Account > Developer > Secure Keys', next generate your keys.
- Configure the Github Token
Make sure you have registered at github.com. Generate the GitHub access token by clicking on your top profile, go to 'Settings > Personal Access Tokens' and click on 'Generate new token'.
- Install Third-party Component for Magento
Go to the Magento 2 installation directory '/var/www/magento2' and run the composer command:
cd /var/www/magento2/
composer install -v
You will be asked for the Magento authentication, use the public key as username and the private key for the password.
Then you will be asked for the GitHub token, paste it and press 'enter' to continue the third-party component.
composer install -v
- Configure the Nginx Virtualhost
Magento offers a ready-made Nginx virtual host configuration, so we just have to include it in our configuration.
Go to the Nginx virtual host directory and create new file called magento:
cd /etc/nginx/sites-available/
vim magento
Paste configuration below:
upstream fastcgi_backend {
server unix:/var/run/php5-fpm.sock;
}
server {
listen 80;
server_name www.newmagento.com;
set $MAGE_ROOT /var/www/magento2;
set $MAGE_MODE developer;
include /var/www/magento2/nginx.conf.sample;
}
Replace www.newmagento.com with the domain name of the website that your shop shall use.
Save and exit.
Now activate the virtual host and restart Nginx:
ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/
systemctl restart nginx
vim magento
systemctl restart nginx
- Install Magento
We will install magento on the command line. In the Magento directory '/var/www/magento2/' there is binary file with the name 'magento' that is used to install and manage magento. Run the command:
bin/magento setup:install --backend-frontname="adminlogin" \
--key="biY8vdWx4w8KV5Q59380Fejy36l6ssUb" \
--db-host="localhost" \
--db-name="magentodb" \
--db-user="magentouser" \
--db-password="magentouser@" \
--language="en_US" \
--currency="USD" \
--timezone="America/New_York" \
--use-rewrites=1 \
--use-secure=0 \
--base-url="http://www.newmagento.com" \
--base-url-secure="https://www.newmagento.com" \
--admin-user=adminuser \
--admin-password=admin123@ \
--admin-email=admin@newmagento.com \
--admin-firstname=admin \
--admin-lastname=user \
--cleanup-database
backend-frontname = the admin page for our magento site, we use 'adminlogin'.
Key = our magento keys, we can generate it, or find it random on http://randomkeygen.com/ etc.
Base-url = make sure it is same with virtual host configuration.
At the end of the installation procedure you should see this lines:
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /adminlogin
Before we will test the Magento installation, ensure the web directory owner is 'www-data', then restart nginx.
cd /var/www/magento2/
chown -R www-data .
systemctl restart nginx
Now open the Magento domain in your browser:
In my case, the name is: www.newmagento.com.
Try to log in to the Magento admin dashboard:
www.newmagento.com/adminlogin
Note :
If you get an error about the Magento indexer and cronjob, then you can solve it by adding the following cronjob to your server:
crontab -u www-data -e
Add the following lines:
*/1 * * * * php -c /etc/php5/cli/php.ini /var/www/magento2/bin/magento cron:run
*/1 * * * * php -c /etc/php5/cli/php.ini /var/www/magento2/update/cron.php
*/1 * * * * php -c /etc/php5/cli/php.ini /var/www/magento2/bin/magento setup:cron:run
Save and exit.
Magento 2 with Nginx and PHP-FPM on Ubuntu 15.10 is installed now.
--key="biY8vdWx4w8KV5Q59380Fejy36l6ssUb" \
--db-host="localhost" \
--db-name="magentodb" \
--db-user="magentouser" \
--db-password="magentouser@" \
--language="en_US" \
--currency="USD" \
--timezone="America/New_York" \
--use-rewrites=1 \
--use-secure=0 \
--base-url="http://www.newmagento.com" \
--base-url-secure="https://www.newmagento.com" \
--admin-user=adminuser \
--admin-password=admin123@ \
--admin-email=admin@newmagento.com \
--admin-firstname=admin \
--admin-lastname=user \
--cleanup-database
Key = our magento keys, we can generate it, or find it random on http://randomkeygen.com/ etc.
Base-url = make sure it is same with virtual host configuration.
chown -R www-data .