Magento Tutorials

How to Install Magento 2 on Ubuntu 16 Using Nginx

magento-2-nginx

Hi everyone, welcome back to another SimiCart tutorial. 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.

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 --admin-email=admin@admin.com --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

How to Install Magento 2 on Ubuntu 16 Using Nginx
4.2 (84%) 5 vote[s]
7 Comments

Dom

A knowledge craver who always strive to be wiser everyday.

    avatar
    5 Comment threads
    2 Thread replies
    0 Followers
     
    Most reacted comment
    Hottest comment thread
    4 Comment authors
    VictorManuDomellis Recent comment authors
      Subscribe  
    newest oldest most voted
    Notify of
    ellis
    Guest
    ellis

    thanks very much for the perfect advice

    ellis
    Guest
    ellis

    how can I access admin panel after installation? Thanks

    Manu
    Guest
    Manu

    Nice manual! Thank you so much!
    Do you have a manual to install ElasticSearch for Magento 2.3? I’m not able to make it work, my system is an Ubuntu 18.04 with nginx and mysql

    Manu
    Guest
    Manu

    I have many doubts, can you help me? I have created this question, but if you can guide me a bit with my doubts I would thank you very much.
    https://magento.stackexchange.com/questions/261288/steps-install-elasticsearch-6-6-for-magento-2

    Victor
    Guest
    Victor

    This file permissions is for production mode o developer mode? Thanks