Step-by-step setup of Magento 2 on CentOS 7 with Nginx, PHP 7.2 (Remi), MySQL 8, and Let's Encrypt SSL.
Prerequisites
- CentOS 7 server with root access
- Domain name pointed at the server IP
- Magento marketplace credentials (public/private key)
Steps
1. Install Nginx
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install -y nginx
systemctl start nginx && systemctl enable nginx
2. Set up Let's Encrypt
yum install -y certbot
openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
mkdir -p /var/lib/letsencrypt/.well-known
chgrp nginx /var/lib/letsencrypt
chmod g+s /var/lib/letsencrypt
mkdir -p /etc/nginx/snippets
Create ACME challenge snippet at /etc/nginx/snippets/letsencrypt.conf:
location ^~ /.well-known/acme-challenge/ {
allow all;
root /var/lib/letsencrypt/;
default_type "text/plain";
try_files $uri =404;
}
3. Install PHP 7.2 via Remi
yum install -y epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum --enablerepo=remi-php72 install -y php php-xml php-soap php-xmlrpc php-mbstring php-json php-gd php-mcrypt php-mysql php-fpm php-pdo php-opcache php-devel php-iconv php-intl php-bcmath php-zip
yum install -y git zip
Tune PHP settings:
sed -i "s/memory_limit = .*/memory_limit = 756M/" /etc/php.ini
sed -i "s/upload_max_filesize = .*/upload_max_filesize = 256M/" /etc/php.ini
sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php.ini
sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php.ini
sed -i "s/;opcache.save_comments.*/opcache.save_comments = 1/" /etc/php.d/10-opcache.ini
4. Create Magento database
mysql -u root -p
CREATE DATABASE magentodb;
CREATE USER 'magentouser'@'localhost' IDENTIFIED BY 'StrongPassword!';
GRANT ALL PRIVILEGES ON magentodb.* TO 'magentouser'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
5. Install Composer and Magento
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/bin/composer
# Create Magento system user
useradd -m -U -r -d /usr/share/nginx/html magento
usermod -aG nginx magento
chmod 750 /usr/share/nginx/html
# Install Magento (enter Marketplace keys when prompted)
sudo -u magento composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition /usr/share/nginx/html
6. Run Magento installer
php /usr/share/nginx/html/bin/magento setup:install --base-url=https://your-domain.com/ --base-url-secure=https://your-domain.com/ --admin-firstname="Admin" --admin-lastname="User" --admin-email="admin@example.com" --admin-user="admin" --admin-password="SecureAdminPass1!" --db-name="magentodb" --db-host="localhost" --db-user="magentouser" --db-password="StrongPassword!" --currency=USD --timezone=America/Chicago --use-rewrites=1
7. Configure PHP-FPM pool for Magento
Create /etc/php-fpm.d/magento.conf:
[magento]
user = magento
group = nginx
listen.owner = magento
listen.group = nginx
listen = /run/php-fpm/magento.sock
pm = ondemand
pm.max_children = 50
pm.process_idle_timeout = 10s
pm.max_requests = 500
chdir = /
systemctl restart php-fpm
8. Nginx virtual host for Magento
upstream fastcgi_backend {
server unix:/run/php-fpm/magento.sock;
}
# HTTP: redirect to HTTPS and serve ACME challenges
server {
listen 80;
server_name your-domain.com;
include snippets/letsencrypt.conf;
return 301 https://your-domain.com$request_uri;
}
# HTTPS
server {
listen 443 ssl http2;
server_name your-domain.com;
ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
include snippets/ssl.conf;
set $MAGE_ROOT /usr/share/nginx/html;
include /usr/share/nginx/html/nginx.conf.sample;
}
sudo nginx -t && sudo systemctl reload nginx
Notes
After installation, set correct file permissions:
cd /usr/share/nginx/html
find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
chown -R magento:nginx .
chmod u+x bin/magento