Magento Tutorials

How to Install Magento 2 on Ubuntu 16 Using Nginx

Hi everyone, welcome back to another SimiCart’s Magento 2 tutorials.

Today, we will guide you on how to install Magento 2 on Ubuntu 16 using Nginx. You can actually do that on CentOS 7 as well but we will take Ubuntu 16 as an example.

Note: Want to know about Magento 2 System Requirements? Here is the full checklist. Don’t skip it!

Step 1: Install nginx

Run the command:

apt-get -y install nginx

Step 2: Install and configure php-fpm

  1. Install php-fpm and php-cli:

Run the command:

apt-get -y install php7.0-fpm php7.0-cli
  1. Open the php.ini files in an editor:

Run the following commands:

vim /etc/php/7.0/fpm/php.ini
vim /etc/php/7.0/cli/php.ini
  1. Make modification in those files to match the following lines:
memory_limit = 2G
max_execution_time = 1800
zlib.output_compression = On
  1. Save and exit the editor you using
  2. Restart the php-fpm service by using the below command:
systemctl restart php7.0-fpm

Step 3: Install and configure MySQL

  1. Run the following command:
sudo apt install -y mysql-server mysql-client
  1. Secure the installation by entering the line:
sudo mysql_secure_installation
  1. Test the installation with the command:
mysql -u root -p
  1. You can increase the value for packet which is larger than Magento default by opening /etc/mysql/mysql.cnf in a text editor and navigate to max_allowed_packet.  

Then, save to mysql.cnf and restart MySQL by entering service mysql restart.

Run the following command at a mysql> prompt to verify your set value:

SHOW VARIABLES LIKE 'max_allowed_packet';
  1. Make your Magento database configuration

Install and configure Magento 2

Download your Magento 2 and then we are good to go.

  1. Run the following to change to docroot directory:
cd /var/www/html
  1. Extract the Magento 2 you downloaded and name it magento2/:
wget https://github.com/magento/magento2/archive/2.0.tar.gz
 tar -xzvf 2.0.tar.gz
 mv magento2-2.0/ magento2/
  1. Enter the following lines to set directory ownership and file permissions.
cd /var/www/html/magento2
find var vendor pub/static pub/media app/etc -type f -exec chmod g+w {} \;
find var vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} \;
chown -R :www-data .
chmod u+x bin/magento
  1. Install Composer globally.
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer
  1. Update Magento dependencies:
cd /var/www/html/magento2
 composer install -v
  1. Enter your Magento authentication keys.
  2. Run the below in command line to install Magento:
cd /var/www/html/magento2/bin
 ./magento setup:install --base-url=http://www.magento-dev.com/ --db-host=localhost --db-name=magento --db-user=magento --db-password=magento --admin-firstname=admin --admin-lastname=admin [email protected] --admin-user=admin --admin-password=admin123 --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1

Remove http://www.magento-dev.com and add your domain name.

  1. Last but not least, change to developer mode to continue with nginx configuration:
cd /var/www/html/magento2/bin
 ./magento deploy:mode:set developer

Step 4: Configure nginx

  1. Run the line to create a new virtual host for your Magento site:
vim /etc/nginx/sites-available/magento
  1. Make the following configuration:
upstream fastcgi_backend {
     server  unix:/run/php/php7.0-fpm.sock;
 }

 server {

     listen 80;
     server_name www.magento-dev.com;
     set $MAGE_ROOT /var/www/html/magento2;
     include /var/www/html/magento2/nginx.conf.sample;
 }
  1. Match your domain name with the base URL you chose when installing Magento.
  2. Save and exit the editor.
  3. Activate host by creating a symlink:
ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled
  1. Verify if the syntax is correct:
nginx -t
  1. Run the command to restart nginx:
systemctl restart nginx

Step 5: Verify the installation

Now try your site’s URL on a web browser to verify the site yourself.

Related posts:
How to install Magento 2 on localhost
How to Install Magento 2 Using Composer
How to Install Magento 2 on WAMP Server Localhost
Magento 2 Permissions: What You Need To Know?

Dom

A knowledge craver who always strive to be wiser everyday.