How To Install Magento 2 In Ubuntu With Nginx?

The following section describes how to install Magento 2 in Ubuntu with Nginx.

Step 1: Install Nginx

Execute the following command:

apt-get -y install nginx

Step 2: Install and configure php-fpm

  1.  Get php-fpm and php-cli installed:

Execute the following command:

apt-get -y install php7.0-fpm php7.0-cli

  1. In a text editor, open the php.ini files as follows:

Execute these commands:

vim /etc/php/7.0/fpm/php.ini

vim /etc/php/7.0/cli/php.ini

  1. Change the lines in those files to match the following:

memory_limit = 2G

max_execution_time = 1800

zlib.output_compression = On

  1. Save your work and close the editor you’re using.
  2. Use the following command to restart the php-fpm service:

systemctl restart php7.0-fpm

Step 3: Install and configure MySQL

  1. Execute the command below:

sudo apt install -y mysql-server mysql-client

  1. Enter the following line to secure the installation:

sudo mysql_secure_installation

  1. Use the following command to check the installation:

mysql -u root -p

  1. By accessing /etc/mysql/mysql.cnf in a text editor and navigating to max allowed packet, you may raise the value for packets that are bigger than Magento’s default.

After that, save the changes to mysql.cnf and restart MySQL using service mysql restart.

To check your set value, type the following command at a mysql> prompt:

SHOW VARIABLES LIKE ‘max_allowed_packet’;

  1. Make your Magento database configuration

Install and configure Magento 2

We’re ready to begin after you’ve downloaded Magento 2.

  1. To go to the docroot directory, perform the following:

cd /var/www/html

  1. Download Magento 2 and save it to your computer as magento2/:


 tar -xzvf 2.3.0.tar.gz

 mv magento2-2.3.0/ magento2/

  1. To set directory ownership and file permissions, use the following commands.

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 on a worldwide basis.

curl -sS | sudo php -- --install-dir=/usr/bin --filename=composer

  1. Magento dependencies must be updated:

cd /var/www/html/magento2

 composer install -v

  1. Fill up your Magento authentication credentials.
  2. To instal Magento, type the following in the command prompt:

cd /var/www/html/magento2/bin

 ./magento setup:install --base-url=
--db-host=localhost --db-name=magento2 --db-user=magento2
--db-password=magento2 --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 and add your domain name.

9. 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. To create a new virtual host for your Magento site, use the following command:

vim /etc/nginx/sites-available/magento

  1. Make the following configuration:

upstream fastcgi_backend

     server  unix:/run/php/php7.0-fpm.sock;



     listen 80;


     set $MAGE_ROOT /var/www/html/magento2;

     include /var/www/html/magento2/nginx.conf.sample;


  1. When installing Magento, make sure your domain name matches the base URL you choose.
  2. Save your work and close the editor.
  3. Create a symlink to activate the host:

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 go to your site’s URL in a web browser and double-check it.

Subscribe to Our Updates and Newsletters