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’re using:
Help if you’re just starting out
If you’re new to all this and need some help getting started, we suggest the following:
- Is the Magento software installed already?
- What is the software that the Magento server needs to run?
- What operating system is my server running?
- How do I log in to my Magento server using a terminal, command prompt, or SSH?
Ubuntu 16
The following section describes how to install Magento 2.x on Ubuntu 16 using nginx, PHP, and MySQL.
Install nginx
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’re using nginx.
To install and configure php-fpm
:
-
Install
php-fpm
andphp-cli
:apt-get -y install php7.0-fpm php7.0-cli
-
Open the
php.ini
files in an editor:vim /etc/php/7.0/fpm/php.ini vim /etc/php/7.0/cli/php.ini
-
Edit both files to match the following lines:
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.
-
Save and exit the editor.
-
Restart the
php-fpm
service:systemctl restart php7.0-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 download and extract an archive.
-
Change to the web server docroot directory, or to a directory you’ve configured as a virtual host docroot. For this example, we’re using the Ubuntu default
/var/www/html
.cd /var/www/html
-
Download the Magento archive, extract it, and rename the folder
magento2/
:wget https://github.com/magento/magento2/archive/2.1.tar.gz tar -xzvf 2.1.tar.gz mv magento2-2.1/ magento2/
-
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
-
Install Composer globally. You’ll need Composer to update dependencies before installing Magento:
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer
-
Update Magento dependencies:
cd /var/www/html/magento2 composer install -v
-
If prompted, enter your Magento authentication keys.
-
Install Magento from the command line.
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
Replace
http://www.magento-dev.com
with your domain name.You cannot use the Web Setup Wizard when installing Magento on nginx. You must use the command line.
-
Switch to developer mode:
cd /var/www/html/magento2/bin ./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.
-
Create a new virtual host for your Magento site:
vim /etc/nginx/sites-available/magento
-
Add 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; }
The
include
directive must point to the sample nginx configuration file in your Magento installation directory. -
Replace
www.magento-dev.com
with your domain name. This must match the base URL you specified when installing Magento. -
Save and exit the editor.
-
Activate the newly created virtual host by creating a symlink to it in the
/etc/nginx/sites-enabled
directory:ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled
-
Verify that the syntax is correct:
nginx -t
-
Restart nginx:
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
yum -y install epel-release
yum -y install nginx
After installation is complete, start nginx and configure it to start at boot time:
systemctl start nginx
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.
-
Install
php-fpm
:yum -y install php70w-fpm
-
Open the
/etc/php.ini
file in an editor. -
Uncomment the
cgi.fix_pathinfo
line and change the value to0
. -
Edit the file to match the following lines:
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.
-
Uncomment the session path directory and set the path:
session.save_path = "/var/lib/php/session"
-
Save and exit the editor.
-
Open
/etc/php-fpm.d/www.conf
in an editor. -
Edit the file to match the following lines:
user = nginx group = nginx listen = /run/php-fpm/php-fpm.sock listen.owner = nginx listen.group = nginx listen.mode = 0660
-
Uncomment the environment lines:
env[HOSTNAME] = $HOSTNAME env[PATH] = /usr/local/bin:/usr/bin:/bin env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp
-
Save and exit the editor.
-
Create a new directory for the PHP session path and change the owner to the
apache
user and group:mkdir -p /var/lib/php/session/ chown -R apache:apache /var/lib/php/
-
Create a new directory for the PHP session path and change the owner to the
apache
user and group:mkdir -p /run/php-fpm/ chown -R apache:apache /run/php-fpm/
-
Start the
php-fpm
service and configure it to start at boot time:systemctl start php-fpm systemctl enable php-fpm
-
Verify that the
php-fpm
service is running: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 download and extract an archive.
-
Change to the web server docroot directory, or to a directory you’ve configured as a virtual host docroot. For this example, we’re using the CentoOS default
/usr/share/nginx/html
.cd /usr/share/nginx/html
-
Download the Magento archive, extract it, and rename the folder
magento2/
:wget https://github.com/magento/magento2/archive/2.1.tar.gz tar -xzvf 2.1.tar.gz mv magento2-2.1/ magento2/
-
Set directory ownership and file permissions.
cd /usr/share/nginx/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 :apache . chmod u+x bin/magento
-
Install Composer globally. You’ll need Composer to update dependencies before installing Magento:
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer
-
Update Magento dependencies:
cd /usr/share/nginx/html/magento2 composer install
-
If prompted, enter your Magento authentication keys.
-
Install Magento from the command line.
cd /usr/share/nginx/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=Magento --admin-lastname=User --admin-email=user@example.com \ --admin-user=admin --admin-password=admin123 --language=en_US \ --currency=USD --timezone=America/Chicago --use-rewrites=1
Replace
http://www.magento-dev.com
with your domain name.You cannot use the Web Setup Wizard when installing Magento on nginx. You must use the command line.
-
Switch Magento to developer mode:
cd /usr/share/nginx/html/magento2/bin ./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.
-
Create a new virtual host for your Magento site:
vim /etc/nginx/conf.d/magento.conf
-
Add the following configuration:
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. -
Replace
www.magento-dev.com
with your domain name. -
Save and exit the editor.
-
Verify that the syntax is correct:
nginx -t
-
Restart nginx:
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:
sestatus
To configure SELinux and firewalld:
-
Install SELinux management tools:
yum -y install policycoreutils-python
-
Run the following commands to change the security context for the Magento installation directory:
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/magento2/app/etc(/.*)?' semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/magento2/var(/.*)?' semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/magento2/pub/media(/.*)?' semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/magento2/pub/static(/.*)?' restorecon -Rv '/usr/share/nginx/html/magento2/'
-
Install the firewalld package:
yum -y install firewalld
-
Start the firewall service and configure it to start at boot time:
systemctl start firewalld systemctl enable firewalld
-
Run the following commands to open ports for HTTP and HTTPS so you can access the Magento base URL from a web browser:
firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --reload
Verify the installation
Open a web browser and navigate to your site’s base URL to verify the installation.