Moving to another server

Installation

Please follow these docs to setup the LAMP stack on the server

Initialize the server for the first time

Install Apache v2.4

Install MySQL v5.7

Install PHP v5.6

Install Redis v4

Install Apache PageSpeed module:

wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_amd64.deb

sudo dpkg -i mod-pagespeed-*.deb

sudo apt-get -f install

sudo systemctl restart apache2

Get started

Connect into the OLD server and set the magento site into maintenance mode

Note: Only do this when you’re going to completely move server in on shot! For testing new server only, should keep the old server running until we decide for a final move!

cd /var/www/html/magento

touch maintenance.flag

Then on the NEW server, clone the source code

cd /var/www/html/

git clone https://gitlab.edge-works.net/easyshed/magento.git

sudo chown -R www-data var/ js/ skin/ media/

Sync the latest “media” files

cd /var/www/html/magento

rsync -av root@$OLD_SERVER_IP:/var/www/html/magento/media/* media/

sudo chown -R www-data media/

Copy the latest db dump file into the server, see more about how to backup & restore MySQL

cd ~

scp root@$OLD_SERVER_IP:/root/easyshed_prod.sql.gz .

Create new database and user

mysql -uroot -p -e “CREATE DATABASE easyshed CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;”

mysql -uroot -p -e “GRANT ALL ON easyshed.* TO ‘easyshed’@’localhost’ IDENTIFIED BY ‘passwordsgenerator.net’; FLUSH PRIVILEGES;”

Restore the db dump

gunzip -c ~/easyshed_prod.sql.gz | mysql -uroot -p easyshed

Create local config file, and update the db connection info

cd /var/www/html/magento/

cp app/etc/local.xml.template app/etc/local.xml

vi app/etc/local.xml

Copy Apache config files from old server, or create new one per these documentation:

https://wiki.edge-works.net/books/web-server-administration/page/httpsssl-configuration

https://wiki.edge-works.net/books/web-server-administration/page/pagespeed-module-settings-for-magento-1x

To test the new site first on different domain, e.g: staging.easyshed.com.au

mysql -uroot -p easyshed -e “UPDATE core_config_data SET value = replace(value, ‘https://www.easyshed.com.au’, ‘https://staging.easyshed.com.au’) WHERE path like ‘web/%'”

Remember to revert it back before switching the DNS!

Once everything is tested and ready, go to DNS management site of the domain and point the domain to the new IP address.

The last step is setup the crontab on NEW server. Go to the OLD server, copy the out of this command

crontab -u www-data -l

paste it to the crontab of NEW server

crontab -u www-data -e

You’re all set!

Advertisements

Install http2 – speed up page

  1. Upgrade Apache to Latest
    apt-get install software-properties-common python-software-properties 
    add-apt-repository ppa:ondrej/apache2 
    apt-get update -y 
    apt-get upgrade -y 
    apt-get install apache2 -y 
    apache2 -v 
    

Apache version should be 2.4.25 or later.

  1. Enable it from mod_http2
    a2enmod http2 
    service apache2 restart
    
  2. Add Self-Signed OR Valid SSL to your website to enable http2 on your server.
    Link:- https://www.digitalocean.com/community/tutorials/how-to-create-a-ssl-certificate-on-apache-for-ubuntu-14-04
  3. Add HTTP/2 Support to Apache2 Virtual Host file. Example :
    <VirtualHost *:443>  
        Protocols h2 http/1.1 
        ServerAdmin you@your-site.com 
        ServerName your-site.com 
        ... 
    </VirtualHost>
    

And Restart Apache2 Services

service apache2 restart
  1. Apache2 on HTTP/2 does not support in mpm_prefork mode, change it to mpm_event.PHP version should be php5.6 or greater.
    service apache2 stop 
    apt-get install php7.1-fpm 
    a2enmod proxy_fcgi setenvif 
    a2enconf php7.1-fpm 
    a2dismod php7.1 
    a2dismod mpm_prefork 
    a2enmod mpm_event 
    service apache2 start 

php5.6

sudo apt-get install php5.6-fpm
sudo a2enmod proxy_fcgi setenvif
sudo 2enconf php7.1-fpm
sudo a2enconf php5.6-fpm
sudo a2dismod php5.6
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event

 

https://stackoverflow.com/questions/37103927/why-does-apache2-module-http2-not-exist-on-ubuntu-16-04
https://http2.pro/doc/Apache

 

 

Some config with php 5.6 in .htaccess

https://stackoverflow.com/questions/35265082/equivalent-of-php-value-under-apache-php-fpm

SetEnv PHP_VALUE "error_reporting = 2039"
SetEnv PHP_VALUE "post_max_size = 16M"
SetEnv PHP_ADMIN_VALUE "post_max_size = 8M"

vagrant nginx php

apt-get update -y && apt-get upgrade -y

https://www.howtoforge.com/tutorial/how-to-install-magento-with-nginx-on-ubuntu/

https://www.digitalocean.com/community/tutorials/how-to-create-an-ssl-certificate-on-nginx-for-ubuntu-14-04

add host vi /etc/hosts/

https://github.com/magento/magento2/blob/2.2-develop/nginx.conf.sample

 

 

sudo vim /etc/nginx/sites-available/magento.hakase-labs.pw

upstream fastcgi_backend {
server unix:/run/php/php7.1-fpm.sock;
}
server {
listen 80;
listen [::]:80;

#root /var/www/magento2/;
#index index.php index.htm index.nginx-debian.html;

server_name magento.hakase-labs.pw http://www.magento.hakase-labs.pw;

#location / {
# try_files $uri $uri/ =404;
#}

#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
# fastcgi_pass unix:/run/php/php7.1-fpm.sock;
#}
set $MAGE_ROOT /var/www/magento2;
set $MAGE_MODE developer;
include /var/www/magento2/nginx.conf.sample;
}
server {

listen 443 ssl;
server_name magento.hakase-labs.pw;

ssl on;
#ssl_certificate /etc/letsencrypt/live/magento.hakase-labs.pw/fullchain.pem;
#ssl_certificate_key /etc/letsencrypt/live/magento.hakase-labs.pw/privkey.pem;

ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;

set $MAGE_ROOT /var/www/magento2;
set $MAGE_MODE developer;
include /var/www/magento2/nginx.conf.sample;
}

 

 

## Example configuration:
# upstream fastcgi_backend {
#    # use tcp connection
#    # server  127.0.0.1:9000;
#    # or socket
#    server   unix:/var/run/php5-fpm.sock;
#    server   unix:/var/run/php/php7.0-fpm.sock;
# }
# server {
#    listen 80;
#    server_name mage.dev;
#    set $MAGE_ROOT /var/www/magento2;
#    include /vagrant/magento2/nginx.conf.sample;
# }
#
## Optional override of deployment mode. We recommend you use the
## command 'bin/magento deploy:mode:set' to switch modes instead.
##
## set $MAGE_MODE default; # or production or developer
##
## If you set MAGE_MODE in server config, you must pass the variable into the
## PHP entry point blocks, which are indicated below. You can pass
## it in using:
##
## fastcgi_param  MAGE_MODE $MAGE_MODE;
##
## In production mode, you should uncomment the 'expires' directive in the /static/ location block

root $MAGE_ROOT/pub;

index index.php;
autoindex off;
charset UTF-8;
error_page 404 403 = /errors/404.php;
#add_header "X-UA-Compatible" "IE=Edge";

# PHP entry point for setup application
location ~* ^/setup($|/) {
    root $MAGE_ROOT;
    location ~ ^/setup/index.php {
        fastcgi_pass   fastcgi_backend;

        fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
        fastcgi_param  PHP_VALUE "memory_limit=756M \n max_execution_time=600";
        fastcgi_read_timeout 600s;
        fastcgi_connect_timeout 600s;

        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ ^/setup/(?!pub/). {
        deny all;
    }

    location ~ ^/setup/pub/ {
        add_header X-Frame-Options "SAMEORIGIN";
    }
}

# PHP entry point for update application
location ~* ^/update($|/) {
    root $MAGE_ROOT;

    location ~ ^/update/index.php {
        fastcgi_split_path_info ^(/update/index.php)(/.+)$;
        fastcgi_pass   fastcgi_backend;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        include        fastcgi_params;
    }

    # Deny everything but index.php
    location ~ ^/update/(?!pub/). {
        deny all;
    }

    location ~ ^/update/pub/ {
        add_header X-Frame-Options "SAMEORIGIN";
    }
}

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

location /pub/ {
    location ~ ^/pub/media/(downloadable|customer|import|theme_customization/.*\.xml) {
        deny all;
    }
    alias $MAGE_ROOT/pub/;
    add_header X-Frame-Options "SAMEORIGIN";
}

location /static/ {
    # Uncomment the following line in production mode
    # expires max;

    # Remove signature of the static files that is used to overcome the browser cache
    location ~ ^/static/version {
        rewrite ^/static/(version[^/]+/)?(.*)$ /static/$2 last;
    }

    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
        add_header Cache-Control "public";
        add_header X-Frame-Options "SAMEORIGIN";
        expires +1y;

        if (!-f $request_filename) {
            rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
        }
    }
    location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
        add_header Cache-Control "no-store";
        add_header X-Frame-Options "SAMEORIGIN";
        expires    off;

        if (!-f $request_filename) {
           rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
        }
    }
    if (!-f $request_filename) {
        rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
    }
    add_header X-Frame-Options "SAMEORIGIN";
}

location /media/ {
    try_files $uri $uri/ /get.php$is_args$args;

    location ~ ^/media/theme_customization/.*\.xml {
        deny all;
    }

    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
        add_header Cache-Control "public";
        add_header X-Frame-Options "SAMEORIGIN";
        expires +1y;
        try_files $uri $uri/ /get.php$is_args$args;
    }
    location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
        add_header Cache-Control "no-store";
        add_header X-Frame-Options "SAMEORIGIN";
        expires    off;
        try_files $uri $uri/ /get.php$is_args$args;
    }
    add_header X-Frame-Options "SAMEORIGIN";
}

location /media/customer/ {
    deny all;
}

location /media/downloadable/ {
    deny all;
}

location /media/import/ {
    deny all;
}

# PHP entry point for main application
location ~ (index|get|static|report|404|503|health_check)\.php$ {
    try_files $uri =404;
    fastcgi_pass   fastcgi_backend;
    fastcgi_buffers 1024 4k;

    fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
    fastcgi_param  PHP_VALUE "memory_limit=756M \n max_execution_time=18000";
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;

    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

gzip on;
gzip_disable "msie6";

gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
    text/plain
    text/css
    text/js
    text/xml
    text/javascript
    application/javascript
    application/x-javascript
    application/json
    application/xml
    application/xml+rss
    image/svg+xml;
gzip_vary on;

# Banned locations (only reached if the earlier PHP entry point regexes don't match)
location ~* (\.php$|\.htaccess$|\.git) {
    deny all;
}

Back AWS – S3 file

DATE=”$(date +’%Y%m%d’)” mysqldump -u root -p {PASSWORD} pms 2>> “/var/backup/pms_bk_$DATE.sql” find ./backup -mtime +15 -type f -delete scp -i ~/file.pem username@serverbackup:/var/backup/pms_bk_$DATE.sql /var/backups/

DATE="$(date +'%Y%m%d')"
mysqldump -u root -p {PASSWORD} pms 2>> "/var/backups/pms_bk_$DATE.sql" 
find /var/backups -mtime +15 -type f -delete 
aws s3 cp /var/backups/pms_bk_`date '+%Y-%m-%d'`.sql s3://backups/pmsdb/pms_bk_`date '+%Y-%m-%d'`.sql
Đây là link cài AWS CLI
https://docs.aws.amazon.com/cli/latest/userguide/installing.html
Link command line cho việc thay đổi security group
https://docs.aws.amazon.com/cli/latest/userguide/cli-ec2-sg.html

Magento 2 : Gulp in frontend

  • apt-get install nodejs and tape ln -s /usr/bin/nodejs /usr/bin/node
  • go to root prject and : apt-get install npm
  • npm install –global gulp-cli and tape npm install –save-dev gulp
  • donwload gulp.js from https://github.com/poddubny/magento2-gulp   =>     npm install gulp-less npm install gulp-sourcemaps  npm install gulp-cssmin  npm install gulp-livereload  npm install gulp-if
  • npm install -g graceful-fs
  • bin/magento dev:source-theme:deploy –locale=”en_US” –area=”frontend” –theme=”Ibnab/Salam”
  • php bin/magento setup:static-content:deploy

http://www.ibnab.com/en/blog/magento-2/magento-2-video-introduction-for-using-gulp-in-frontend

Down load to Magento folder

https://github.com/subodha/magento-2-gulp

Run Administration role:

  • bin/magento dev:source-theme:deploy –locale=”en_US” –area=”frontend” –theme=”Ibnab/Salam”
  1. Run : gulp exec --theme ex: gulp exec –luma
    Or: php bin\magento dev:source-theme:deploy --locale="en_AU" --area="frontend" --theme="VendorName/themeName"
  2. Run : gulp deploy --theme ex: gulp deploy –luma
    Or: php bin\magento setup:static-content:deploy en_AU