Skip to content

Instantly share code, notes, and snippets.

@Agowan
Last active May 4, 2020 11:04
Show Gist options
  • Save Agowan/a064ff8ae8e48c6aef48d449a9d255d2 to your computer and use it in GitHub Desktop.
Save Agowan/a064ff8ae8e48c6aef48d449a9d255d2 to your computer and use it in GitHub Desktop.
Install magento in production (external MySQL)
  1. Make sure your system is up to date
sudo apt update && sudo apt upgrade -y
  1. Create user
sudo useradd -m -U -r -d /opt/magento magento && sudo chmod 750 /opt/magento

See this tutorial to add repos and upgrade to new versions: https://thishosting.rocks/install-php-on-ubuntu/

  1. Install repository for php
sudo add-apt-repository ppa:ondrej/php && sudo add-apt-repository ppa:ondrej/nginx-mainline
  1. Install PHP
sudo apt install php7.3-common php7.3-cli php7.3-fpm php7.3-opcache php7.3-gd php7.3-mysql php7.3-curl php7.3-intl php7.3-xsl php7.3-mbstring php7.3-zip php7.3-bcmath php7.3-soap -y && sudo systemctl status php7.3-fpm

Should print the following output:

● php7.3-fpm.service - The PHP 7.3 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.3-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-05-04 08:14:51 UTC; 1s ago
     Docs: man:php-fpm7.3(8)
  Process: 15786 ExecStopPost=/usr/lib/php/php-fpm-socket-helper remove /run/php/php-fpm.sock /etc/php/7.3/fpm/pool.d/www.conf 73 (code=exited, status=0/SUCCESS)
  Process: 15809 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.3/fpm/pool.d/www.conf 73 (code=exited, status=0/SUCCESS)
 Main PID: 15788 (php-fpm7.3)
   Status: "Ready to handle connections"
    Tasks: 3 (limit: 4369)
   CGroup: /system.slice/php7.3-fpm.service
           ├─15788 php-fpm: master process (/etc/php/7.3/fpm/php-fpm.conf)
           ├─15803 php-fpm: pool www
           └─15804 php-fpm: pool www
May 04 08:14:51 bn-magento-custom systemd[1]: Stopped The PHP 7.3 FastCGI Process Manager.
May 04 08:14:51 bn-magento-custom systemd[1]: Starting The PHP 7.3 FastCGI Process Manager...
May 04 08:14:51 bn-magento-custom systemd[1]: Started The PHP 7.3 FastCGI Process Manager.
  1. Set php requirements
sudo sed -i "s/memory_limit = .*/memory_limit = 1024M/" /etc/php/7.3/fpm/php.ini && sudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 256M/" /etc/php/7.3/fpm/php.ini && sudo sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php/7.3/fpm/php.ini && sudo sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php/7.3/fpm/php.ini && sudo sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/7.3/fpm/php.ini && sudo sed -i "s/;opcache.save_comments.*/opcache.save_comments = 1/" /etc/php/7.3/fpm/php.ini && sudo bash -c "printf '[magento]\nuser = $(whoami)\ngroup = www-data\nlisten.owner = $(whoami)\nlisten.group = www-data\nlisten = /var/run/php/php7.3-fpm-magento.sock\npm = ondemand\npm.max_children =  50\npm.process_idle_timeout = 10s\npm.max_requests = 500\nchdir = /\n' >> /etc/php/7.3/fpm/pool.d/magento.conf" && sudo systemctl restart php7.3-fpm && sudo ls -al /var/run/php/php7.3-fpm-magento.sock

Output:

srw-rw---- 1 magento www-data 0 Dec 12 16:07 /var/run/php/php7.3-fpm-magento.sock=
  1. Next we need to create a FPM pool for the magento user.
sudo vim /etc/php/7.3/fpm/pool.d/magento.conf

Add the following to the file

[magento]
user = magento
group = www-data
listen.owner = magento
listen.group = www-data
listen = /var/run/php/php7.2-fpm-magento.sock
pm = ondemand
pm.max_children =  50
pm.process_idle_timeout = 10s
pm.max_requests = 500
chdir = /
  1. Restart FPM service
sudo systemctl restart php7.2-fpm
  1. Verify whether the PHP socket was successfully created
sudo ls -al /var/run/php/php7.3-fpm-magento.sock

Output

srw-rw---- 1 magento www-data 0 May  4 08:27 /var/run/php/php7.3-fpm-magento.sock
  1. Install Composer For freshly installed debian based os do not forget to install curl: sudo apt install curl
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer && composer --version

Output:

Composer version 1.10.5 2020-04-10 11:44:22
  1. Set user on mysql & create database
CREATE USER 'magento'@'%' IDENTIFIED BY 'secure-password';
GRANT ALL PRIVILEGES ON magento.* TO 'magento'@'%';

Also set the flag

log_bin_trust_function_creators
  1. Switch to the magento user
sudo su - magento
  1. Install Magento
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition /opt/magento/public_html

To find the credentials login to the following url: https://marketplace.magento.com/customer/accessKeys/

  1. Setup Magento Make sure to change to correct values before you past this in to the terminal
cd ~/public_html && php bin/magento setup:install --base-url=http://127.0.0.1 --base-url-secure=https://127.0.0.1 --admin-firstname="John" --admin-lastname="Doe"    --admin-email="john@example.com" --admin-user="john" --admin-password="j0hnP4ssvv0rD" --db-name="magento" --db-host="localhost" --db-user="magento" --currency=SEK --timezone=Europe/Stockholm --use-rewrites=1 --db-password="change-with-strong-password" 

Output:

[Progress: 833 / 833]
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_vvj3ad
Nothing to import.
Crontab has been generated and saved
#~ MAGENTO START b2ed17f426f54e03f3a7c3189c34dcf7
* * * * * /usr/bin/php7.2 /home/username/www-dev/magento/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /home/username/www-dev/magento/var/log/magento.cron.log
* * * * * /usr/bin/php7.2 /home/username/www-dev/magento/update/cron.php >> /home/username/www-dev/magento/var/log/update.cron.log
* * * * * /usr/bin/php7.2 /home/username/www-dev/magento/bin/magento setup:cron:run >> /home/username/www-dev/magento/var/log/setup.cron.log
#~ MAGENTO END b2ed17f426f54e03f3a7c3189c34dcf7
  1. Crontab
php ~/public_html/bin/magento cron:install && crontab -l

Output:

#~ MAGENTO START adc062915d7b30804a2b340095af072d
* * * * * /usr/bin/php7.3 /opt/magento/public_html/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /opt/magento/public_html/var/log/magento.cron.log
* * * * * /usr/bin/php7.3 /opt/magento/public_html/update/cron.php >> /opt/magento/public_html/var/log/update.cron.log
* * * * * /usr/bin/php7.3 /opt/magento/public_html/bin/magento setup:cron:run >> /opt/magento/public_html/var/log/setup.cron.log
#~ MAGENTO END adc062915d7b30804a2b340095af072d
  1. Install Nginx
sudo apt install nginx -y && sudo systemctl status nginx && sudo ufw allow 'Nginx Full' && sudo ufw status

Output

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-05-22 05:54:52 PDT; 3s ago
     Docs: man:nginx(8)
  Process: 5666 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 5665 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 5667 (nginx)
    Tasks: 3 (limit: 4652)
   CGroup: /system.slice/nginx.service
           ├─5667 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ├─5668 nginx: worker process
           └─5669 nginx: worker process

May 22 05:54:52 ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server...
May 22 05:54:52 ubuntu systemd[1]: Started A high performance web server and a reverse proxy server.
Rules updated
Rules updated (v6)
Status: inactive
  1. Add nginx config Edit and paste the following in /etc/nginx/sites-available/magento
upstream fastcgi_backend {
  server   unix:/var/run/php/php7.3-fpm-magento.sock;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name store.bnmarin.se;

    set $MAGE_ROOT /opt/magneto/public_html;
    set $MAGE_MODE production;
    include /opt/magento/public_html/nginx.conf.sample;
}
  1. Symlink
sudo ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/
  1. Now edit (with sudo) /etc/nginx/sites-available/default in order to remove the default_server mention as
server {
  listen 80;
  listen [::]:80;
  # your others line below
}
  1. Install certbot
sudo add-apt-repository ppa:certbot/certbot && sudo apt-get update && sudo apt-get install python-certbot-nginx
  1. Obtain ssl cert
sudo certbot --nginx -d example.com -d www.example.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment