Monday, July 15, 2019

Enable MySQL Log Rotation on CentOS 6.6

Create /root/.my.cnf configuration file with the following content

Content:
[mysqladmin]
password = <password>
user= root

Change the access permissions

chmod 600 /root/.my.cnf


Create mysql file under /etc/logrotate.d (Or edit file if exists)

vim /etc/logrotate.d/mysql 

Content:

/var/log/mysqld/*.log {
        # create 600 mysql mysql
        notifempty
    daily
        rotate 30
        missingok
        compress
        postrotate
    # just if mysqld is really running
    if test -x /usr/bin/mysqladmin && \
       /usr/bin/mysqladmin ping &>/dev/null
    then
       /usr/bin/mysqladmin  flush-general-log flush-slow-log flush-error-log
    fi
    endscript
}





Debug:

logrotate -df /etc/logrotate.d/mysql

Try:

logrotate -df /etc/logrotate.d/mysql



Ref:









Tuesday, December 12, 2017

CentOS Timezone Configuration

/usr/share/zoneinfo/ - The system time-zone information directory.
/etc/localtime - The symlink to the timezone file

Image: Update Timezone to Asia/Colombo

Friday, March 27, 2015

Create a web application using Yii2

The latest version of Yii 2 is 2.0.3, released on March 1, 2015.

Related Links


Environment

  • OS: Ubuntu 14.04

Installation of an application template

  • Find / Create a suitable directory in your hard drive.
  • Install Composer 
    • $ curl -sS https://getcomposer.org/installer | php
  • Install the Composer Asset Plugin
    • $ php composer.phar global require "fxp/composer-asset-plugin:1.0.0"
  • To install the basic application template, run the command below: 
    •  php composer.phar create-project yiisoft/yii2-app-basic basic 2.0.3
  •  To install the advanced application template, run the command below: 
    •  php composer.phar create-project yiisoft/yii2-app-advanced advanced 2.0.3
$ curl -sS https://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /Data/nuwan/Research/yii2/app1/composer.phar
Use it: php composer.phar
$ php composer.phar global require "fxp/composer-asset-plugin:1.0.0"
Changed current directory to /home/nuwan/.composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Removing fxp/composer-asset-plugin (1.0.x-dev dc24e62)
  - Installing fxp/composer-asset-plugin (v1.0.0)
    Downloading: 100%         

Writing lock file
Generating autoload files
$ php composer.phar create-project yiisoft/yii2-app-basic basic 2.0.3
Installing yiisoft/yii2-app-basic (2.0.3)
  - Installing yiisoft/yii2-app-basic (2.0.3)
    Downloading: 100%         

Created project in basic
Loading composer repositories with package information
Installing dependencies (including require-dev)
Reading bower.json of bower-asset/jquery.inputmask (3.1.51)
Could not fetch https://api.github.com/repos/RobinHerbots/jquery.inputmask/contents/bower.json?ref=f29e2b535028bde8e1135deb65cb5ed2e081cf39, enter your GitHub credentials to go over the API rate limit
A token will be created and stored in "/home/nuwan/.composer/auth.json", your password will never be stored
To revoke access to this token you can visit https://github.com/settings/applications
Username: srinuwanperera@gmail.com
Password: 
Token successfully created
Reading bower.json of bower-asset/bootstrap (v3.1.1)       
Reading bower.json of bower-asset/bootstrap (v3.3.1)
  - Installing yiisoft/yii2-composer (2.0.3)            
    Downloading: 100%         

  - Installing ezyang/htmlpurifier (v4.6.0)
    Loading from cache

  - Installing swiftmailer/swiftmailer (v5.4.0)
    Downloading: 100%         

  - Installing bower-asset/jquery (2.1.3)
    Downloading: 100%         

  - Installing bower-asset/yii2-pjax (v2.0.4)
    Downloading: 100%         

  - Installing bower-asset/punycode (v1.3.2)
    Downloading: 100%         

  - Installing bower-asset/jquery.inputmask (3.1.62)
    Downloading: 100%         

  - Installing cebe/markdown (1.0.2)
    Downloading: 100%         

  - Installing yiisoft/yii2 (2.0.3)
    Downloading: 100%         

  - Installing yiisoft/yii2-swiftmailer (2.0.3)
    Downloading: 100%         

  - Installing yiisoft/yii2-codeception (2.0.3)
    Downloading: 100%         

  - Installing bower-asset/bootstrap (v3.3.4)
    Downloading: 100%         

  - Installing yiisoft/yii2-bootstrap (2.0.3)
    Downloading: 100%         

  - Installing yiisoft/yii2-debug (2.0.3)
    Downloading: 100%         

  - Installing bower-asset/typeahead.js (v0.10.5)
    Downloading: 100%         

  - Installing phpspec/php-diff (v1.0.2)
    Loading from cache

  - Installing yiisoft/yii2-gii (2.0.3)
    Downloading: 100%         

  - Installing fzaninotto/faker (v1.4.0)
    Downloading: 100%         

  - Installing yiisoft/yii2-faker (2.0.3)
    Downloading: 100%         

Writing lock file
Generating autoload files
chmod('runtime', 0777)...done.
chmod('web/assets', 0777)...done.
chmod('yii', 0755)...done.
$
$ php composer.phar create-project yiisoft/yii2-app-advanced advanced 2.0.3
Installing yiisoft/yii2-app-advanced (2.0.3)
  - Installing yiisoft/yii2-app-advanced (2.0.3)
    Downloading: 100%         

Created project in advanced
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing yiisoft/yii2-composer (2.0.3)               
    Loading from cache

  - Installing ezyang/htmlpurifier (v4.6.0)
    Loading from cache

  - Installing swiftmailer/swiftmailer (v5.4.0)
    Loading from cache

  - Installing bower-asset/jquery (2.1.3)
    Loading from cache

  - Installing bower-asset/yii2-pjax (v2.0.4)
    Loading from cache

  - Installing bower-asset/punycode (v1.3.2)
    Loading from cache

  - Installing bower-asset/jquery.inputmask (3.1.62)
    Loading from cache

  - Installing cebe/markdown (1.0.2)
    Loading from cache

  - Installing yiisoft/yii2 (2.0.3)
    Loading from cache

  - Installing yiisoft/yii2-swiftmailer (2.0.3)
    Loading from cache

  - Installing yiisoft/yii2-codeception (2.0.3)
    Loading from cache

  - Installing bower-asset/bootstrap (v3.3.4)
    Loading from cache

  - Installing yiisoft/yii2-bootstrap (2.0.3)
    Loading from cache

  - Installing yiisoft/yii2-debug (2.0.3)
    Loading from cache

  - Installing bower-asset/typeahead.js (v0.10.5)
    Loading from cache

  - Installing phpspec/php-diff (v1.0.2)
    Loading from cache

  - Installing yiisoft/yii2-gii (2.0.3)
    Loading from cache

  - Installing fzaninotto/faker (v1.4.0)
    Loading from cache

  - Installing yiisoft/yii2-faker (2.0.3)
    Loading from cache

Writing lock file
Generating autoload files
$
$ ll
total 1068
drwxrwxr-x  4 nuwan nuwan    4096 මාර්  27 13:03 ./
drwxrwxr-x  3 nuwan nuwan    4096 මාර්  27 12:10 ../
drwxrwxr-x  9 nuwan nuwan    4096 මාර්  27 13:08 advanced/
drwxrwxr-x 13 nuwan nuwan    4096 මාර්  27 12:50 basic/
-rwxr-xr-x  1 nuwan nuwan 1075141 මාර්  27 12:12 composer.phar*
$

Initialize the application (For advanced template)

  • ./init
$ ./init
Yii Application Initialization Tool v1.0

Which environment do you want the application to be initialized in?

  [0] Development
  [1] Production

  Your choice [0-1, or "q" to quit] 0

  Initialize the application under 'Development' environment? [yes|no] yes

  Start initialization ...

   generate common/config/main-local.php
   generate common/config/params-local.php
   generate frontend/web/index-test.php
   generate frontend/web/index.php
   generate frontend/config/main-local.php
   generate frontend/config/params-local.php
   generate backend/web/index-test.php
   generate backend/web/index.php
   generate backend/config/main-local.php
   generate backend/config/params-local.php
   generate yii
   generate console/config/main-local.php
   generate console/config/params-local.php
   generate cookie validation key in backend/config/main-local.php
   generate cookie validation key in frontend/config/main-local.php
      chmod 0777 backend/runtime
      chmod 0777 backend/web/assets
      chmod 0777 frontend/runtime
      chmod 0777 frontend/web/assets
      chmod 0755 yii

  ... initialization completed.
$

Test the application using php server.

  • Basic Application
    • php -S localhost:8000 -t basic/web
  • Advanced Application
    • php -S localhost:8000 -t advanced/frontend/web/
    •  php -S localhost:8000 -t advanced/backend/web/
  • Once the php server started, open a web browser and visit:
    •  http://localhost:8000/
$ php -S localhost:8000 -t advanced/frontend/web/
PHP 5.5.9-1ubuntu4.7 Development Server started at Fri Mar 27 13:28:22 2015
Listening on http://localhost:8000
Document root is /*******/advanced/frontend/web
Press Ctrl-C to quit.





Tuesday, September 16, 2014

cannot enable executable stack as shared object requires: Permission denied

Story:

Today, One of our team member has been installed a clean installation of RHEL 6.3 (RedHat) and once we try to start the Apache web server, it went bad due to following error.

Error:


Starting httpd: httpd: Syntax error on line 218 of /etc/httpd/conf/httpd.conf: Cannot load /etc/httpd/modules/libphp5.so into server: /etc/httpd/modules/libphp5.so: cannot enable executable stack as shared object requires: Permission denied 

Solution:


  1. Log into the CLI - Command Line interface (As root user)
  2. Run Following Command
[root@new ~]# execstack -c /etc/httpd/modules/libphp5.so

Image:


Tuesday, August 5, 2014

Abstract classes in PHP

Related Links

  • http://php.net/manual/en/language.oop5.interfaces.php
  • http://www.techflirt.com/tutorials/oop-in-php/abstract-classes-interface.html

What is class abstraction?

  • PHP 5 introduces abstract classes and methods.
  • Usually abstract class are also known as base class.
  • Abstract classes are those classes which can not be directly initialized. Or in other word we can say that you can not create object of abstract classes.
  • Any class that contains at least one abstract method must also be abstract.
  • Abstract classes always created for inheritance purpose. You can only inherit abstract class in your child class.
  • It can only act as parent class of any normal class. You can use abstract class in class hierarchy. Mean one abstract class can inherit another abstract class also.
  • A protected abstract function can be implemented as either protected or public but not private.

Monday, October 7, 2013

How to set "auto_increment_increment" and "auto_increment_offset" - MySQL

Referred web sites:

What is "auto_increment_increment" and "auto_increment_offset" ?

Both "auto_increment_increment" and "auto_increment_offset" are system variables which required in MySQL Master-Master replication. By changing above variables you will be able to manage auto increment columns and avoid from data conflict issues in your database.

What does it do ?

  • "auto_increment_increment" controls the interval between successive column values
  • auto_increment_offset determines the starting point for the AUTO_INCREMENT column value

How to access MySQL monitor (Ubuntu 12.04)

  • Open Terminal window using "Dash Home".
  • Log into MySQL Monitor (You may need a valid username and password)
  • mysql
    or
    mysql -uroot
    or
    mysql -uroot -p


  • View existing(default) system variables ("auto_increment_increment" and "auto_increment_offset")
  • mysql> SHOW VARIABLES LIKE 'auto_inc%';
     


  • Create a Database (You will be able to test this using your existing Database)
  • mysql> CREATE DATABASE TEST_DB;
  • Select created Database to use
  • mysql> USE TEST_DB;
  • Create a table
  • mysql> CREATE TABLE TestTable
        -> (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
  • Insert sample records
  • mysql> INSERT INTO TestTable VALUES (NULL), (NULL), (NULL), (NULL);
  • View inserted records
  • mysql> SELECT * FROM TestTable;


  • Set "auto_increment_increment" to 5 and insert another sample records
  • mysql> SET @@auto_increment_increment=5;
    mysql> INSERT INTO TestTable VALUES (NULL), (NULL), (NULL), (NULL);
    mysql> SELECT * FROM TestTable;


  • Now you know how it works. You can create another table to test "auto_increment_offset"
    • You can drop your "TestTable" and follow above instructions. But make sure to keep the table without any data.
    • Also do not forget to set "auto_increment_increment"
  • Now you can set "auto_increment_offset"
  • mysql> SET @@auto_increment_offset=5;
  • Then insert few sample records
  • mysql> INSERT INTO TestTable VALUES (NULL), (NULL), (NULL), (NULL);



  • This is how "auto_increment_increment" and "auto_increment_offset" works.

Something to remember.

  • You will be able to set "auto_increment_offset" greater then (>) "auto_increment_increment" and test again. But the value of "auto_increment_offset" will be ignore by the system.


  • Because if the value of "auto_increment_offset" is greater than that of "auto_increment_increment", the value of auto_increment_offset is ignored.

Hardcode variables in my.cnf:

Also you can hardcode followings settings in your my.cnf file (/etc/my.cnf)
# The MySQL server
[mysqld]

auto-increment-increment = 2 # No of replication servers
auto-increment-offset = 1 # For first replication server

Tuesday, October 1, 2013

How to fix "It is not safe to rely on the system's timezone settings"

If you see one of following warnings when you run a PHP script:


  • PHP Warning:  mktime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Calcutta' for 'IST/5.0/no DST' instead in /opt/websites/www/sample.php on line 10
  • PHP Warning:  date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Calcutta' for 'IST/5.0/no DST' instead in  /opt/websites/www/sample.php on line 11
PHP Warning:  mktime(): It is not safe to rely on the system's timezone settings.
PHP Warning:  date(): It is not safe to rely on the system's timezone settings.

You can try following solutions:

Solution 01

  1. Find your timezone settings

    (For Ubuntu)
    cat /etc/timezone 

    (For Fedora/CentOS/RedHat)
    cat /etc/sysconfig/clock 

     

    Figure:  Ubuntu 12.04 Terminal

    or simply select your timezone from http://php.net/manual/en/timezones.php
  2. Find your php.ini file to edit

    (For Ubuntu)
    cat /etc/php5/apache2/php.ini

    (For Fedora/CentOS/RedHat)
    cat /etc/php.ini

    or You can locate all php.ini files using
    locate php.ini

  3. Edit php.ini

    Now insert following date-time settings into php.ini (replace "Asia/Colombo" using your timezone settings)
    [Date]
    ; Defines the default timezone used by the date functions
    ; http://php.net/date.timezone
    date.timezone = Asia/Colombo
  4. Restart Apache

    (For Ubuntu)
    /etc/init.d/apache2 restart
    or
    service apache2 restart
    (For Fedora/CentOS/RedHat)
    /etc/init.d/httpd restart
    or
    service httpd restart

Solution 02

  1. Edit your php script

    if you are unable to access your php.ini, can set the default timezone at the beginning of your PHP scripts.
    <?php
    date_default_timezone_set("Asia/Colombo");