Debian Server Setup & Magento 2 Installation

How to setup a Debian server on GCP and then install and configure Nginx and PHP to run Magento.

Debian Server Setup & Magento 2 Installation
Photo by Lukas / Unsplash

Create a new GCP instance of e2-medium or larger size. When creating the instance I use standard Debian and open HTTPS and give it a static internal and external IP. For SSL I use Cloudflare or Letsencrypt.

Install Nginx

sudo apt update
sudo apt upgrade
sudo apt -y install nginx wget

Install php7.4

sudo apt -y install lsb-release apt-transport-https ca-certificates 
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
sudo apt update
sudo apt -y install php7.4-fpm php7.4-cli
sudo apt -y install php7.4-{bcmath,ctype,bz2,curl,dom,fileinfo,intl,gd,iconv,json,mbstring,mysql,simplexml,soap,sockets,tokenizer,xmlwriter,xsl,zip}

Set php.ini for Magento

sudo nano /etc/php/7.4/fpm/php.ini
sudo nano /etc/php/7.4/cli/php.ini
date.timezone = Europe/London
realpath_cache_size = 10M
realpath_cache_ttl = 7200
opcache.enable = 1
opcache.save_comments = 1

Install MariaDB / MySQL

sudo apt install mariadb-server
sudo mysql_secure_installation

Log Into MariaDB & create the Magento database & user

sudo mysql -u root -p
create database magento;
create user 'magento'@'localhost' IDENTIFIED BY 'magento';
GRANT ALL ON magento.* TO 'magento'@'localhost';
flush privileges;

Find the my.cnf file location

mysql --help | grep /my.cnf | xargs ls
##############################################################################################
##  http://dev.mysql.com/doc/refman/8.0/en/innodb-parameters.html                           ##
##  https://raw.githubusercontent.com/rackerhacker/MySQLTuner-perl/master/mysqltuner.pl     ##
##  https://launchpadlibrarian.net/78745738/tuning-primer.sh                                ##
##  yum install mytop  /  apt-get install mytop                                             ##
##############################################################################################

[mysqld]

### MyISAM #
key_buffer_size = 16M # keep it low if no myisam data
myisam-recover-options = FORCE,BACKUP

### SAFETY #
innodb = force
max_allowed_packet = 250M
max_connect_errors = 1000
bind-address = 127.0.0.1
skip-name-resolve

### LANGUAGE #
#init_connect='SET collation_connection = utf8_unicode_ci'
#init_connect='SET NAMES utf8'
#character-set-server=utf8
#collation-server=utf8_unicode_ci
#skip-character-set-client-handshake

### CACHES AND LIMITS #
back_log = 20
interactive_timeout = 7200
wait_timeout = 7200
net_read_timeout = 120
net_write_timeout = 300
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
join_buffer_size = 2M
tmp_table_size = 128M
max_heap_table_size = 128M
#query_cache_type = 2
#query_cache_size = 128M
max_connections = 150
thread_cache_size = 32
thread_pool_size = 16
open_files_limit = 65535
table_definition_cache = 4000
table_open_cache = 4000

### INNODB_ #
#innodb_thread_concurrency = 0
innodb_lock_wait_timeout = 7200
innodb_flush_method = O_DIRECT
innodb_flush_log_at_trx_commit = 2
#innodb_log_files_in_group = 2
innodb_log_file_size = 512M
innodb_log_buffer_size = 32M
innodb_file_per_table = 1
innodb_read_io_threads = 8
innodb_write_io_threads = 8
#innodb_buffer_pool_instances = 4
innodb_buffer_pool_size = 4G

### LOGGING #
log_error = /var/log/mysqld.log
#log_queries_not_using_indexes = 1
#log_slow_admin_statements = 1
#slow_query_log = 1
#long_query_time = 1
#slow_query_log_file = /var/log/mysql-slow.log

### BINARY LOGGING #
skip_log_bin
disable_log_bin
#log_bin = /var/lib//mysql-bin.log
#expire_logs_days = 14
#sync_binlog = 1

Install Magento 2

Install Composer

Install composer 1 and move into Magento bin so it can be called from the Magento root folder as bin/composer, as Composer 2 seems to error https://getcomposer.org/download/. In the Magento root dir:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --version 1.9.3
php -r "unlink('composer-setup.php');"
mv composer.phar bin/composer

Magento 2 Build Process

Add an extension through composer ignoring any composer errors

eg symphony needing php8 errors

composer require amasty/blog --ignore-platform-reqs --with-all-dependencies

Update Magento after making changes (production mode)

bin/magento setup:upgrade &&
bin/magento setup:di:compile &&
bin/magento setup:static-content:deploy -f &&
bin/magento cache:flush &&
bin/magento indexer:reindex