Friday, 17 May 2019

Difference between restart reload for apache

service apache2 restart
service apache2 reload
service apache2 graceful

  1. Difference between “restart” and “reload”
    • Restart= stop + start
    • Reload = remain running + re-read configuration files.
  2. Normal restart and graceful restart, you can reference article:

Wednesday, 15 May 2019

MYSQL Delete all duplicate rows except one

  1. If you want to keep the row with the lowest id value:
    DELETE n1 FROM <table> n1, <table> n2 WHERE n1.id > n2.id AND n1.<column>= n2.<column>
  2. If you want to keep the row with the highest id value:
    DELETE n1 FROM <table> n1, <table> n2 WHERE n1.id < n2.id AND n1.<column> = n2.<column

Tuesday, 14 May 2019

PHP SFTP library && MSSQL guide

SFTP library: https://github.com/phpseclib/phpseclib
MSSQL - PHP guide:  (After installation, sqlsrv.so is a dynamic driver located in /usr/lib/php/20170718/, read permission need to be grante)
https://www.microsoft.com/en-us/sql-server/developer-get-started/php/ubuntu



PHP ini file

// Not recommended to pass parameter via command line to PHP script
// because when a user do: ps auxww . They can see script parameters
// Use php ini file instead https://php.net/manual/en/function.parse-ini-file.php
// Ignore this ini file when uploading to git(upload a sample.ini with new value to git)
// Or use toml for putting arrays, JSON in config file. https://github.com/healthservices-io/wolfdash/blob/master/config/config.toml.example

Cron Job

// Not Recommended: Opens cron job list for current logged in user [Not recommended, very hard to manage]
crontab -e 
// Add this line to cron job file (Run a job every 5 minutes - run by current logged in user)
*/5 * * * * /path/to/script-or-program >> log_file.txt

// Recommended : Open cron job list for root
vim /etc/crontab
// Specify a user to run the cron job can be root, user_a, user_b
*/5 * * * * <user> /path/to/script-or-program >> log_file.txt



// Check whether cron job has run or not
sudo vim /var/log/syslog



# m h dom mon dow user  command
│17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
│25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
│47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )                                                
 │52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

when you scheduled cron job make sure to schedule after these time, or else cant see log

 +---------------- minute (0 - 59)
 |  +------------- hour (0 - 23)
 |  |  +---------- day of month (1 - 31)
 |  |  |  +------- month (1 - 12)
 |  |  |  |  +---- day of week (0 - 6) (Sunday=0 or 7)
 |  |  |  |  |
 *  *  *  *  *  command to be executed
  00 19 * * 1,3,5 /home/user/somejob.sh







Thursday, 9 May 2019

Change .htaccess file so some routes are handled by vue-router and some are handled by laravel



// Changes to laravel default .htaccess file

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]


    # When domain is accessed, index.html page is autmotically served because of apache2 default DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm     ( /etc/apache2/mods-enabled/dir.conf)
    # If (url contains route_1, or route_2, and if file not found, we let vue-router to serve) 
    RewriteCond %{REQUEST_URI} ^/<route_1>[NC,OR]
    RewriteCond %{REQUEST_URI} ^/<route_2>[NC]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . /index.html [L]

    # IF !(url contains route_1, or route_2, and if file not found, we let laravel-router to serve) 
    RewriteCond %{REQUEST_URI} !^/<route_1>[NC]
    RewriteCond %{REQUEST_URI} !^/<route_2>[NC]
    # make sure we haven't already rewritten this URI
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    # Rewrite everything else to laravel router in index.php
    RewriteRule ^ index.php [L]

</IfModule>





https://stackoverflow.com/questions/11906060/is-there-a-way-to-if-else-a-rewrite-in-apache-htaccess
https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations
https://www.adayinthelifeof.nl/2012/01/21/apaches-fallbackresource-your-new-htaccess-command/
https://stackoverflow.com/questions/7873634/why-does-index-html-have-priority-over-index-php

Remove .html extension, redirect HTTP to HTTPS in .htaccess file

In .htaccess file change the following :


#example.com/page will display the contents of example.com/page.html

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.html -f

RewriteRule ^(.+)$ $1.html [L,QSA]



#301 from example.com/page.html to example.com/page

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/

RewriteRule ^(.*)\.html$ /$1 [R=301,L]




For Godaddy add:



 Options -MultiViews



For http to https redirect



RewriteCond %{SERVER_PORT} 80

RewriteCond %{HTTP_HOST} ^(www\.)?citycentreupcc\.ca

RewriteRule ^(.*)$ https://www.citycentreupcc.ca/$1 [R,L]




Full code 

Options -MultiViews

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?citycentreupcc\.ca
RewriteRule ^(.*)$ https://www.citycentreupcc.ca/$1 [R,L]


#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/

RewriteRule ^(.*)\.html$ /$1 [R=301,L]


https://stackoverflow.com/questions/5730092/how-to-remove-html-from-url