nginx

Magento supports nginx 1.8 (or the latest mainline version). You must also install the latest version of php-fpm.

Installation instructions vary based on which operating system you are using. See PHP for for information.

Help if you’re just starting out

If you’re new to all this and need some help getting started, we suggest the following:

Ubuntu 16

The following section describes how to install Magento 2.x on Ubuntu 16 using nginx, PHP, and MySQL.

Install nginx

1
apt-get -y install nginx

After completing the following sections and installing Magento, we’ll use a sample configuration file to configure nginx.

Install and configure php-fpm

Magento requires several PHP extensions to function properly. In addition to these extensions, you must also install and configure the php-fpm extension if you are using nginx.

To install and configure php-fpm:

  1. Install php-fpm and php-cli:

    1
    
    apt-get -y install php7.2-fpm php7.2-cli
    

    This command installs the latest available version of PHP 7.2.X. See Magento 2.3.x technology stack requirements for supported PHP versions.

  2. Open the php.ini files in an editor:

    1
    
    vim /etc/php/7.2/fpm/php.ini
    
    1
    
    vim /etc/php/7.2/cli/php.ini
    
  3. Edit both files to match the following lines:

    1
    2
    3
    
    memory_limit = 2G
    max_execution_time = 1800
    zlib.output_compression = On
    

    We recommend setting the memory limit to 2G when testing Magento. Refer to Required PHP settings for more information.

  4. Save and exit the editor.

  5. Restart the php-fpm service:

    1
    
    systemctl restart php7.2-fpm
    

Install and configure MySQL

Refer to MySQL for more information.

Install and configure Magento2

There are several ways to download the Magento software, including:

For this example, we’ll install using Composer and the command line.

You cannot use the Web Setup Wizard when installing Magento on nginx. You must use the command line.

  1. As the Magento file system owner, log in to your Magento server.

  2. Change to the web server docroot directory or a directory that you have configured as a virtual host docroot. For this example, we’re using the Ubuntu default /var/www/html.

    1
    
    cd /var/www/html
    
  3. Install Composer globally. You’ll need Composer to update dependencies before installing Magento:

    1
    
    curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer
    
  4. Create a new Composer project using the Magento Open Source or Magento Commerce metapackage.

    Magento Open Source

    1
    
    composer create-project --repository=https://repo.magento.com/ magento/project-community-edition <install-directory-name>
    

    Magento Commerce

    1
    
    composer create-project --repository=https://repo.magento.com/ magento/project-enterprise-edition <install-directory-name>
    

    When prompted, enter your Magento authentication keys. Your public key is your username; your private key is your password.

  5. Set read-write permissions for the web server group before you install the Magento software. This is necessary so that the Setup Wizard and command line can write files to the Magento file system.

    1
    
    cd /var/www/html/<magento install directory>
    
    1
    
    find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
    
    1
    
    find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
    
    1
    
    chown -R :www-data . # Ubuntu
    
    1
    
    chmod u+x bin/magento
    
  6. Install Magento from the command line. This example assumes that the Magento install directory is named magento2ee, the db-host is on the same machine (localhost), and that the db-name, db-user, and db-password are all magento:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    bin/magento setup:install \
    --base-url=http://localhost/magento2ee \
    --db-host=localhost \
    --db-name=magento \
    --db-user=magento \
    --db-password=magento \
    --backend-frontname=admin \
    --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
    
  7. Switch to developer mode:

    1
    
    cd /var/www/html/magento2/bin
    
    1
    
    ./magento deploy:mode:set developer
    

Configure nginx

We recommend configuring nginx using the nginx.conf.sample configuration file provided in the Magento installation directory and an nginx virtual host.

These instructions assume you’re using the Ubuntu default location for the nginx virtual host (e.g., /etc/nginx/sites-available) and Ubuntu default docroot (e.g., /var/www/html), however, you can change these locations to suit your environment.

  1. Create a new virtual host for your Magento site:

    1
    
    vim /etc/nginx/sites-available/magento
    
  2. Add the following configuration:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    upstream fastcgi_backend {
      server  unix:/run/php/php7.2-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;
    }
    

The include directive must point to the sample nginx configuration file in your Magento installation directory.

  1. Replace www.magento-dev.com with your domain name. This must match the base URL you specified when installing Magento.

  2. Save and exit the editor.

  3. Activate the newly created virtual host by creating a symlink to it in the /etc/nginx/sites-enabled directory:

    1
    
    ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled
    
  4. Verify that the syntax is correct:

    1
    
    nginx -t
    
  5. Restart nginx:

    1
    
    systemctl restart nginx
    

Verify the installation

Open a web browser and navigate to your site’s base URL to verify the installation.

CentOS 7

The following section describes how to install Magento 2.x on CentOS 7 using nginx, PHP, and MySQL.

Install nginx

1
yum -y install epel-release
1
yum -y install nginx

After installation is complete, start nginx and configure it to start at boot time:

1
systemctl start nginx
1
systemctl enable nginx

After completing the following sections and installing Magento, we’ll use a sample configuration file to configure nginx.

Install and configure php-fpm

Magento requires several PHP extensions to function properly. In addition to these extensions, you must also install and configure the php-fpm extension if you’re using nginx.

  1. Install php-fpm:

    1
    
    yum -y install php70w-fpm
    
  2. Open the /etc/php.ini file in an editor.

  3. Uncomment the cgi.fix_pathinfo line and change the value to 0.

  4. Edit the file to match the following lines:

    1
    2
    3
    
    memory_limit = 2G
    max_execution_time = 1800
    zlib.output_compression = On
    

We recommend setting the memory limit to 2G when testing Magento. Refer to Required PHP settings for more information.

  1. Uncomment the session path directory and set the path:

    1
    
    session.save_path = "/var/lib/php/session"
    
  2. Save and exit the editor.

  3. Open /etc/php-fpm.d/www.conf in an editor.

  4. Edit the file to match the following lines:

    1
    2
    3
    4
    5
    6
    
    user = nginx
    group = nginx
    listen = /run/php-fpm/php-fpm.sock
    listen.owner = nginx
    listen.group = nginx
    listen.mode = 0660
    
  5. Uncomment the environment lines:

    1
    2
    3
    4
    5
    
    env[HOSTNAME] = $HOSTNAME
    env[PATH] = /usr/local/bin:/usr/bin:/bin
    env[TMP] = /tmp
    env[TMPDIR] = /tmp
    env[TEMP] = /tmp
    
  6. Save and exit the editor.

  7. Create a new directory for the PHP session path and change the owner to the apache user and group:

    1
    
    mkdir -p /var/lib/php/session/
    
    1
    
    chown -R apache:apache /var/lib/php/
    
  8. Create a new directory for the PHP session path and change the owner to the apache user and group:

    1
    
    mkdir -p /run/php-fpm/
    
    1
    
    chown -R apache:apache /run/php-fpm/
    
  9. Start the php-fpm service and configure it to start at boot time:

    1
    
    systemctl start php-fpm
    
    1
    
    systemctl enable php-fpm
    
  10. Verify that the php-fpm service is running:

    1
    
    netstat -pl | grep php-fpm.sock
    

Install and configure MySQL

Refer to MySQL for more information.

Install and configure Magento2

There are several ways to download the Magento software, including:

For this example, we’ll install using Composer and the command line.

You cannot use the Web Setup Wizard when installing Magento on nginx. You must use the command line.

  1. As the Magento file system owner, log in to your Magento server.

  2. Change to the web server docroot directory or a directory that you have configured as a virtual host docroot. For this example, we’re using the Ubuntu default /var/www/html.

    1
    
    cd /var/www/html
    
  3. Install Composer globally. You’ll need Composer to update dependencies before installing Magento:

    1
    
    curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer
    
  4. Create a new Composer project using the Magento Open Source or Magento Commerce metapackage.

    Magento Open Source

    1
    
    composer create-project --repository=https://repo.magento.com/ magento/project-community-edition <install-directory-name>
    

    Magento Commerce

    1
    
    composer create-project --repository=https://repo.magento.com/ magento/project-enterprise-edition <install-directory-name>
    

    When prompted, enter your Magento authentication keys. Your public key is your username; your private key is your password.

  5. Set read-write permissions for the web server group before you install the Magento software. This is necessary so that the Setup Wizard and command line can write files to the Magento file system.

    1
    
    cd /var/www/html/<magento install directory>
    
    1
    
    find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
    
    1
    
    find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
    
    1
    
    chown -R :www-data . # Ubuntu
    
    1
    
    chmod u+x bin/magento
    
  6. Install Magento from the command line. This example assumes that the Magento install directory is named magento2ee, the db-host is on the same machine (localhost), and that the db-name, db-user, and db-password are all magento:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    bin/magento setup:install \
    --base-url=http://localhost/magento2ee \
    --db-host=localhost \
    --db-name=magento \
    --db-user=magento \
    --db-password=magento \
    --backend-frontname=admin \
    --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
    
  7. Switch to developer mode:

    1
    
    cd /var/www/html/magento2/bin
    
    1
    
    ./magento deploy:mode:set developer
    

Configure nginx

We recommend configuring nginx using the nginx.conf.sample configuration file provided in the Magento installation directory and an nginx virtual host.

These instructions assume you’re using the CentOS default location for the nginx virtual host (e.g., /etc/nginx/conf.d) and default docroot (e.g., /usr/share/nginx/html), however, you can change these locations to suit your environment.

  1. Create a new virtual host for your Magento site:

    1
    
    vim /etc/nginx/conf.d/magento.conf
    
  2. Add the following configuration:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    upstream fastcgi_backend {
      server  unix:/run/php-fpm/php-fpm.sock;
    }
    
    server {
    
      listen 80;
      server_name www.magento-dev.com;
      set $MAGE_ROOT /usr/share/nginx/html/magento2;
      include /usr/share/nginx/html/magento2/nginx.conf.sample;
    }
    

The include directive must point to the sample nginx configuration file in your Magento installation directory.

  1. Replace www.magento-dev.com with your domain name.

  2. Save and exit the editor.

  3. Verify that the syntax is correct:

    1
    
    nginx -t
    
  4. Restart nginx:

    1
    
    systemctl restart nginx
    

Configure SELinux and Firewalld

SELinux is enabled by default on CentOS 7. Use the following command to see if it’s running:

1
sestatus

To configure SELinux and firewalld:

  1. Install SELinux management tools:

    1
    
    yum -y install policycoreutils-python
    
  2. Run the following commands to change the security context for the Magento installation directory:

    1
    
    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/magento2/app/etc(/.*)?'
    
    1
    
    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/magento2/var(/.*)?'
    
    1
    
    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/magento2/pub/media(/.*)?'
    
    1
    
    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/magento2/pub/static(/.*)?'
    
    1
    
    restorecon -Rv '/usr/share/nginx/html/magento2/'
    
  3. Install the firewalld package:

    1
    
    yum -y install firewalld
    
  4. Start the firewall service and configure it to start at boot time:

    1
    
    systemctl start firewalld
    
    1
    
    systemctl enable firewalld
    
  5. Run the following commands to open ports for HTTP and HTTPS so you can access the Magento base URL from a web browser:

    1
    
    firewall-cmd --permanent --add-service=http
    
    1
    
    firewall-cmd --permanent --add-service=https
    
    1
    
    firewall-cmd --reload
    

Verify the installation

Open a web browser and navigate to your site’s base URL to verify the installation.

Related topics