Install Magento on CentOS 7 with LEMP

January 8, 2015 12.9k views

I've searched all over Digitalocean and I find a lot of questions about how to install Magento on centos 7 with nginx but non do really explain properly step by step "how to".

It would be nice if we could help eachother out here to write everything that is needed from simple security to how to how to set up proper permissions for Magento.

I could begin and then someone with the knowledge could just fill in further in the comments:

Step 0: Initial Centos 7 setup

Step 1: Install Centos 7 with LEMP, follow this guide:

Step 2: Additional Steps for a new Centos 7 server

Step 3: Download Magento
Please write in the comments how to do this, and then someone else could write Step 4 in the comments. Thank you!

1 comment
2 Answers
  • Run the following:
rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
yum -y upgrade
yum install mariadb mariadb-server mysql vim-enhanced nginx php-fpm php-mysql php-pear php-bcmath php-common php-gd php-pdo php-pdo-mysql php-mcrypt
  • edit /etc/my.cnf.d/server.cnf
    • under [server] heading, add the following:
  bind-address =
  • run the following:
systemctl restart mariadb; systemctl enable mariadb; mysql_secure_installation

  • It will ask you a set of questions. The default MySQL (MariaDB) root password is blank. Set it, and say "Y" to everything else
    • run the following:
cat /proc/cpuinfo |grep processor -c
  • if the number returns is anything other than "1", edit "/etc/nginx/nginx.conf"
    • change worker_processes to the number the last command outputted
  • Run nginx -t
    • If it doesn't give an error, run the following:
  systemctl restart nginx; systemctl enable nginx
  • Edit /etc/php.ini
    • uncomment "date.timezone = " and set it to your Timezone. For example:
  date.timezone = America/Chicago
  • change expose_php to "Off"
  • change user and group:
  user = nginx
  group = nginx
  • Run this to set the logs properly:
  chown nginx:root /var/log/php-fpm -R
  • Start php-fpm:
  systemctl restart php-fpm; systemctl enable php-fpm
  • Create your document root
  mkdir -p /srv/www/YOUR_DOMAIN_GOES_HERE
  chown nginx:nginx -R /srv/www
  • Download Magento (you will need to sign up for their site)
  • SCP/SFTP it to your server (I just dropped it in /root/)
  • Untar it, move the files around, and set ownership:
  tar zxf ~/magento-; mv magento* .
  mv magento/.htaccess* .
  cd /srv/
  chown nginx:nginx * -R
  • Next, we need to set up the hostfile for the site. Edit /etc/nginx/conf.d/default.conf, and put this in there (changing all instances of "YOURDOMAINGOES_HERE" with your domain/sub-domain):
server {
        listen 80 default;
## SSL directives might go here
        server_name YOUR_DOMAIN_GOES_HERE www.YOUR_DOMAIN_GOES_HERE; ## Domain is here twice so server_name_in_redirect will favour the www
        root /srv/www/YOUR_DOMAIN_GOES_HERE;

        location / {
                index index.html index.php; ## Allow a static html file to be shown first
                try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
                expires 30d; ## Assume all files are cachable

        ## These locations would be hidden by .htaccess normally
        location ^~ /app/                { deny all; }
        location ^~ /includes/           { deny all; }
        location ^~ /lib/                { deny all; }
        location ^~ /media/downloadable/ { deny all; }
        location ^~ /pkginfo/            { deny all; }
        location ^~ /report/config.xml   { deny all; }
        location ^~ /var/                { deny all; }

        location /var/export/ { ## Allow admins only to view export folder
                auth_basic           "Restricted"; ## Message shown in login window
                auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
                autoindex            on;

        location  /. { ## Disable .htaccess and other hidden files
                return 404;

        location @handler { ## Magento uses a common front handler
                rewrite / /index.php;

        location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
                rewrite ^(.*.php)/ $1 last;

        location ~ .php$ { ## Execute PHP scripts
                if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss

                expires        off; ## Do not cache dynamic content
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_param  MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
                fastcgi_param  MAGE_RUN_TYPE store;
                include        fastcgi_params; ## See /etc/nginx/fastcgi_params
  • Run this:
  nginx -t
  • If there aren't any errors, restart nginx:
  systemctl restart nginx
  • Next, we need to hop on MariaDB:
  mysql -p
  • Now, to add the database & user/pass (Change the database name, user, and pass to what you want):
  GRANT ALL PRIVILEGES ON magento.* TO 'magento_user'@'localhost' IDENTIFIED BY 'YOUR_PASS_GOES_HERE';
  • Finally, go to the server IP or domain and go through the setup.
  • Great answer on how to install Magento, thank you! Nice to have clear and info on "how to".

Would it be possible if someone could explain how to configure /etc/nginx/conf.d/default.conf to use multistore configuration if I use more than two domains, for example domain.com and domain.net

Have another answer? Share your knowledge.