How to Install Magento with Nginx on Ubuntu 18.04 (LEMP)

Prerequisites

  • Ubuntu 18.04
  • 2GB or more RAM memory
  • Root privileges

What we will do

  1. Install PHP Composer
  2. Download Magento 2
  3. Install Magento Components
  4. Generate SSL Letsencrypt
  5. Configure Nginx Virtual Host for Magento
  6. Magento Post-Installation

Step 1 – Install Nginx on Ubuntu 18.04 LTS

In this tutorial, we will be using the Nginx web server for our Magento installation.

Log in to the server, update the repository, and upgrade all packages.

sudo apt update
sudo apt upgrade

Now install the Nginx web server using the apt command below.

sudo apt install nginx -y

After the installation is complete, start the Nginx service and enable it to launch every time at system boot.

systemctl start nginx
systemctl enable nginx

Nginx web server has been installed, check it using netstat command
and make sure the HTTP port 80 is on the ‘LISTEN’ state. Another way is
by using curl command as below.

netstat -plntu
curl -I localhost

Step 2 – Install and Configure PHP-FPM 7.1

After the Nginx web server installation, we will install PHP 7.1 on
the server as Magento does not support PHP 7.2 yet. We will install
PHP-FPM with all extensions needed by Magento 2.

List of PHP extensions needed for Magento 2 installation:

  • bc-math
  • ctype
  • curl
  • dom
  • gd, ImageMagick 6.3.7 (or later) or both
  • intl
  • mbstring
  • mcrypt
  • hash
  • openssl
  • PDO/MySQL
  • SimpleXML
  • soap
  • spl
  • libxml
  • xsl
  • zip
  • json
  • iconv

For this guide, we will install PHP-FPM packages from the PPA repository. We will be using the ‘ondrej/php’ repository.

Install the ‘software-properties-common’ package and add the ‘ondrej/php’ repository using commands below.

sudo apt install software-properties-common -y
sudo apt-get install python-software-properties sudo add-apt-repository ppa:ondrej/php -y sudo apt-get update sudo apt-get install -y php7.1

Now install PHP-FPM 7.1 with all extensions needed.

sudo apt install php7.1-fpm php7.1-mcrypt php7.1-curl php7.1-cli php7.1-mysql php7.1-gd php7.1-xsl php7.1-json php7.1-intl php-pear php7.1-dev php7.1-common php7.1-mbstring php7.1-zip php7.1-soap php7.1-bcmath -y

After the installation is complete, check the PHP version and installed extensions using PHP commands.

php -v
php -me

Next, we will configure the php.ini file for the PHP-FPM and PHP-CLI.

Edit the php.ini files using nano

vim /etc/php/7.1/fpm/php.ini
vim /etc/php/7.1/cli/php.ini

Change the value of those lines as below.

memory_limit = 512M
max_execution_time = 180
zlib.output_compression = On

Save and exit.

or 

sed -i "s/memory_limit = .*/memory_limit = 512M/" /etc/php/7.1/fpm/php.ini
sed -i "s/upload_max_filesize = .*/upload_max_filesize = 128M/" /etc/php/7.1/fpm/php.ini
sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php/7.1/fpm/php.ini
sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php/7.1/fpm/php.ini

sed -i "s/memory_limit = .*/memory_limit = 512M/" /etc/php/7.1/cli/php.ini
sed -i "s/upload_max_filesize = .*/upload_max_filesize = 128M/" /etc/php/7.1/cli/php.ini
sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php/7.1/cli/php.ini
sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php/7.1/cli/php.ini

Now restart the PHP-fpm service and enable it to launch every time at system boot.

systemctl restart php7.1-fpm
systemctl enable php7.1-fpm

The PHP-FPM 7.1 installation and configuration has been completed, check the service using netstat command.

netstat -pl | grep php

And you will get the PHP-fpm socks file as below.

WARNING

https://stackoverflow.com/questions/16002268/prevent-nginx-504-gateway-timeout-using-php-set-time-limit

or Step 2.1 – Install and Configure PHP-FPM 7.0

sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y

Now install PHP-FPM 7.0 with all extensions needed.

sudo apt install php7.0-fpm php7.0-mcrypt php7.0-curl php7.0-cli php7.0-mysql php7.0-gd php7.0-xsl php7.0-json php7.0-intl php-pear php7.0-dev php7.0-common php7.0-mbstring php7.0-zip php7.0-soap php7.0-bcmath -y

After the installation is complete, check the PHP version and installed extensions using PHP commands.

php -v
php -me

Next, we will configure the php.ini file for the PHP-FPM and PHP-CLI.

Edit the php.ini files using nano

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

Change the value of those lines as below.

memory_limit = 512M
max_execution_time = 180
zlib.output_compression = On

Save and exit.

or

sed -i "s/memory_limit = .*/memory_limit = 512M/" /etc/php/7.0/fpm/php.ini
sed -i "s/upload_max_filesize = .*/upload_max_filesize = 128M/" /etc/php/7.0/fpm/php.ini
sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php/7.0/fpm/php.ini
sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php/7.0/fpm/php.ini

sed -i "s/memory_limit = .*/memory_limit = 512M/" /etc/php/7.0/cli/php.ini
sed -i "s/upload_max_filesize = .*/upload_max_filesize = 128M/" /etc/php/7.0/cli/php.ini
sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php/7.0/cli/php.ini
sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php/7.0/cli/php.ini

Now restart the PHP-fpm service and enable it to launch every time at system boot.

systemctl restart php7.0-fpm
systemctl enable php7.0-fpm

The PHP-FPM 7.0 installation and configuration has been completed, check the service using netstat command.

netstat -pl | grep php

And you will get the PHP-fpm socks file as below.

Step 3 – Install and Configure MySQL Server

The Magento software requires MySQL 5.6.x, and the Magento 2.1.2 or
later require the MySQL 5.7.x. In this tutorial, we will install latest
MySQL server 5.8 on the Ubuntu 18.04 system.

Install MySQL 5.8 using the apt command below.

sudo apt install mysql-server mysql-client -y

After the installation is complete, start the MySQL service and enable it to launch every time at system boot.

systemctl start mysql
systemctl enable mysql

Now we will configure the MySQL root password using ‘mysql_secure_installation’ command.

mysql_secure_installation

In this MySQL 5.8 version, there is a security improvement for the
MySQL password policy. You need to choose the password policy – 0 for
the LOW policy, 1 for the MEDIUM policy, and 2 for a STRONG password
policy.

For this guide, we will be using the ‘MEDIUM’ password policy, and it’s recommended to use the ‘STRONG’ password policy on the

Choose number ‘1’ and press Enter, then type your new MySQL ‘root’ password.

 Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y

The MySQL root password has been set up.

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';

Next, we will create a new database for our Magento installation. We
will create a new database named  ‘magentodb’ with user ‘magentouser’
and the password is ‘Magento0463@#’.

Login to the MySQL shell using the root user.

mysql -u root -p

Now run MySQL queries below to create the database and user.

create database magentodb;
create user magentouser@localhost identified by 'Magento0463@#';
grant all privileges on magentodb.* to magentouser@localhost identified by 'Magento0463@#';
flush privileges;

The MySQL server installation and configuration for the Magento installation has been completed.

Step 4 – Install and Configure Magento 2

In this step, we will install Magento 2.2.4 latest version from
Github repository. We will install the PHP composer for installing the
Magento components, download Magento from Github repository, configure
Nginx virtual host for Magento, and install Magento using the web-based
post installation.

– Install PHP Composer

Install PHP Composer on ubuntu 18.04 using the apt command below.

sudo apt install composer -y

After the installation is complete, check the composer version installed on the system.

composer -V

The latest version PHP Composer has been installed.


– Download Magento 2

Go to the ’/var/www’ directory and download the Magento archive source code from Github using wget command.

cd /var/www/
wget https://github.com/magento/magento2/archive/2.2.6.tar.gz

Now extract the Magento archive file and rename the directory to ‘magento2’.

tar -xf 2.2.6.tar.gz;
mv magento2-2.2.6/ magento2/;

The Magento source code has been downloaded, and the ’/var/www/magento2’ directory will be the web root for the Magento site.

– Install Magento Components

Install Magento components using the PHP composer. Go to the
‘magento2’ directory and install all the PHP components needed by
Magento using the ‘composer’ command.

cd /var/www/magento2;
composer install -v;

If you plan to install Sample data – refer to this link for details. As for now Magento 2 is still under development, the best variant is to read the latest documentation at Magento docs.

File permissions

‘Admin’ user is the owner of all magento files on our server, and
Apache web server runs on behalf of ‘www-data’ user. Magento 2
documentation describes
the access permission settings for files and directories on the
principle that the web server runs on behalf of the user which is the
owner of all Magento files. Our opinion is: it’s not the best decision,
as it can lead to security issues (as the web server has permissions to
write and run the code) as well as issues with setting access
permissions for third party developers (as you’re not logging in as
www-data user). We normally use the following access permissions:

cd /var/www/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 {} ;
chmod u+x bin/magento

If you’re going to install Magento from the web interface, then add write permissions for app/etc/ and vendor/:

chmod -R g+w /var/www/magento2/{app/etc,vendor}

 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.1-fpm.sock;
 }

 server {

    listen 80;
    #listen [::]:80;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;
    set $MAGE_ROOT /var/www/magento2;
    #client_max_body_size 200M;
    #set $MAGE_MODE developer;
    include /var/www/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. Delete the default configuration:
  2. sudo rm -f /etc/nginx/sites-enabled/default
  3. Verify if the syntax is correct:

nginx -t

Run the command to restart nginx:

systemctl restart nginx

Verify the installation

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

If you would like to SSL  (click)

Sample DATA INSTALL

install magento 

cd /var/www/

wget https://github.com/magento/magento2/archive/2.2-develop.zip
unzip 2.2-develop.zip
cd magento2-2.2-develop/
mv * ../
wget https://github.com/magento/magento2-sample-data/archive/2.2.6.zip
unzip 2.2.6.zip
cd magento2-sample-data-2.2.6/
mv * ../

Enable sample data modules (it’s important!):

php bin/magento module:enable Magento_CustomerSampleData Magento_MsrpSampleData Magento_CatalogSampleData Magento_DownloadableSampleData Magento_OfflineShippingSampleData Magento_BundleSampleData Magento_ConfigurableSampleData Magento_ThemeSampleData Magento_ProductLinksSampleData Magento_ReviewSampleData Magento_CatalogRuleSampleData Magento_SwatchesSampleData Magento_GroupedProductSampleData Magento_TaxSampleData Magento_CmsSampleData Magento_SalesRuleSampleData Magento_SalesSampleData Magento_WidgetSampleData Magento_WishlistSampleData

Remove old files:

rm -rf var/cache/* var/page_cache/* var/generation/*

Upgrade magento files:

bin/magento setup:upgrade

Recompile files:

php bin/magento setup:di:compile

Do reindex:

php bin/magento indexer:reindex

Deploy static content:

php bin/magento setup:static-content:deploy

FİXED BUGS 

Note: right now (15.05.2015) Magento Sample data can’t be install due to an unknown bug.

[Progress: 269 / 272]
Installing sample data:

[MagentoFrameworkExceptionLocalizedException]
Area code is already set

Thus, we advise to monitor known Magento 2 issues and submit bugs if you notice any.

“””sample data error Fatal error: Cannot use ‘Void’ as class name as it is reserved in ….”

https://magento.stackexchange.com/questions/128460/classnames-void-inside-vendor-magento

magento 2 Error “An error has happened during application run. See exception log for detail

In my case this happens when runing a command as root user which is changes the file permissions.

A simple owner corecction can solve this:

chown -R www-data:www-data /var/www/magento2/

For me this was worked.

———————————————-

php bin/magento maintenance:enable;
php bin/magento setup:upgrade;
php bin/magento setup:di:compile;
php bin/magento setup:static-content:deploy -f;
php bin/magento cache:clean;
php bin/magento maintenance:disable

———

Magento 2 first install –   How to fix Magento 500 Internal Server Errors

chown -R www-data:www-data /var/www/magento2/ 

chmod 777 -R var
chmod 777 -R generated
chmod 777 -R app/etc

or  https://www.simicart.com/blog/magento-2-file-permissions/

———-

Adjusting Child Processes For PHP-FPM Nginx – Fix Server Reached pm.max_children Setting

https://www.tecklyfe.com/adjusting-child-processes-php-fpm-nginx-fix-server-reached-pm-max_children-setting/

magento and varnish install 

https://www.rosehosting.com/blog/how-to-install-magento-2-with-apache-varnish-and-memcache/

PROBLEM  = in correct datetime value:


 SET sql_mode = 'NO_ZERO_DATE';


https://askubuntu.com/questions/928191/how-to-open-a-closed-port-in-ubuntu

sudo bin/magento setup:store-config:set –base-url=”https://ww.Site.com

Source:

https://websiteforstudents.com/a-very-quick-and-easy-way-to-install-magento-2-with-nginx-on-ubuntu/

https://www.howtoforge.com/tutorial/how-to-install-magento-with-nginx-on-ubuntu/

https://www.rosehosting.com/blog/magento-2-with-redis-varnish-and-nginx-as-ssl-termination/

https://www.simicart.com/blog/magento-2-nginx/

https://gist.github.com/okovalov/37fa0ededf8878c616df7faab99afed7

https://doc.zooextension.com/magento/magento-hosting/#/install-configure-php-fpm