Setup Production DB Server: MariaDB + PhpMyAdmin

Standard

Update Centos:

sudo yum update
sudo yum install nano wget

DB Server

sudo yum install httpd php php-mysql

sudo service httpd start
sudo systemctl enable httpd

Install MariaDB
Add MariaDB Repo

sudo nano /etc/yum.repos.d/MariaDB.repo

Put this inside MariaDB.repo


# MariaDB 10.1 CentOS repository list - created 2016-06-07 15:06 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Install and Start Maria DB

sudo yum install MariaDB-server MariaDB-client

sudo service mariadb start
sudo systemctl enable mariadb

Securing Maria DB

sudo /usr/bin/mysql_secure_installation

Installing PHP MyAdmin

sudo yum install epel-release

sudo yum install phpmyadmin

PhpMyadmin Configuration

sudo nano /etc/httpd/conf.d/phpMyAdmin.conf

Change Php MyAdmin URL

Alias /NePhpMyAdminUrl /usr/share/phpMyAdmin

To allow access from All add Require all granted

<RequireAny>
   Require all granted
   Require ip 127.0.0.1
   Require ip ::1
</RequireAny>

Restart Web Server

sudo service httpd restart

URL

http://domain.com/NePhpMyAdminUrl

Setup IP Address and Network Connection on CENTOS 7

Standard

Edit ifcfg file

vi /etc/sysconfig/network-scripts/ifcfg-eth0

Add this

TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=62f47c6d-cf65-409f-bbd6-696db494d74c
DEVICE=eth0
ONBOOT=yes
IPADDR=73.123.136.53
NETMASK=255.255.255.248
GATEWAY=73.123.136.49
DNS1=8.8.8.8
DNS2=8.8.4.4

And then run command

service network restart

ping google.com

[SOLVED] Laravel 5.1 Nginx/Apache: Can’t display errors, 500 internal server error

Standard

Edit index.php in public folder.

put all code inside try .. catch …


<?php

try {

require __DIR__.'/../bootstrap/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php';

$kernel = $app->make('Illuminate\Contracts\Http\Kernel');

$response = $kernel->handle(
	$request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);


} catch(\Exception $e) {
    echo "<pre>";
    echo $e;
    echo "</pre>";
}

[Solved] Laravel Storage Permission Denied even chmod 777

Standard

By default, SELinux will block the ability for Apache/webapps to write to files in RHEL/Centos. This is a security feature to limit what folders Apache can write to. You can notice this issue if you get errors in your Apache/Nginx such as:

PHP Warning:  move_uploaded_file(): Unable to move ‘/path/to/upload/…’ to ‘…….’

Command to set permissions:

chcon -R -t httpd_sys_rw_content_t <folder_name>

You can check the SELinux permissions via the following command:

ls -halZ

If You get Laravel Storage Permission Denied even chmod 777 there is posibility it is caused by SELinux mode in the server. SELinux was enabled which prevent Apache/Nginx to write files even with 777 mode.

Check

 sudo setenforce 0

This should allow writing to storage directory, If permission denied gone, this prove SELinux is the problem. Turn SELinux back

setenforce 1

Then finally use SELinux to allow writing of the file by using this command

chcon -R -t httpd_sys_rw_content_t storage

And you’re off!

Changing Primary Domain Document Root in Cpanel

Standard
  1. Login to ssh.
  2. sudo and edit this file:
    /var/cpanel/userdata/user/domain

    If you have SSL:

    /var/cpanel/userdata/user/domain_SSL

    user is your cpanel username and domain is your domain, _SSL capital leter.

  3. Change these 2 line to your new document root:
    documentroot: /home/USER/public_html

    and

    path: /home/USER/public_html/cgi-bin
  4. Run sudo:
    sudo /scripts/rebuildhttpdconf
    sudo service httpd restart
  5. Done.

[Solved] Composer SSL Certificate Failed

Standard

got this error when running composer on Windows with PHP 5.6

 [Composer\Downloader\TransportException]
 The "https://packagist.org/packages.json" file could not be downloaded: SSL operation failed with code 1. OpenSSL Error messages:
 error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
 Failed to enable crypto
 failed to open stream: operation failed

Run this in console:

php -r "print_r(openssl_get_cert_locations());"

Mine got something like this:

Array
(
    [default_cert_file] => f:\repo\winlibs_openssl_vc11_x86/cert.pem
    [default_cert_file_env] => SSL_CERT_FILE
    [default_cert_dir] => f:\repo\winlibs_openssl_vc11_x86/certs
    [default_cert_dir_env] => SSL_CERT_DIR
    [default_private_dir] => f:\repo\winlibs_openssl_vc11_x86/private
    [default_default_cert_area] => f:\repo\winlibs_openssl_vc11_x86
    [ini_cafile] =>
    [ini_capath] =>
)

but I have no “f:\repo” directory so it was pointing to wrong location.

To fix this, add this line to PHP.ini

openssl.cafile="\xampp\perl\vendor\lib\Mozilla\CA\cacert.pem"

Or download cacert.pem from http://curl.haxx.se/ca/cacert.pem
and add this line to PHP.ini

openssl.cafile="\your\cacert\pem\path\cacert.pem"

Enable or Disable Windows 7 Hibernate

Standard

To make hibernation unavailable, follow these steps:

  1. Click Start, and then type cmd in the Start Search box.
  2. In the search results list, right-click Command Prompt, and then click Run as Administrator.
  3. When you are prompted by User Account Control, click Continue.
  4. At the command prompt, type powercfg.exe /hibernate off, and then press Enter.
  5. Type exit, and then press Enter to close the Command Prompt window.

To make hibernation available, follow these steps:

  1. Click Start, and then type cmd in the Start Search box.
  2. In the search results list, right-click Command Prompt, and then click Run as Administrator.
  3. When you are prompted by User Account Control, click Continue.
  4. At the command prompt, type powercfg.exe /hibernate on, and then press Enter.
  5. Type exit, and then press Enter to close the Command Prompt window.

Convert Javascript Array into PHP Array

Standard

String Javascript Array
For Example: [“A”,2,[[1,1,1,"A","J",1],[1,2,1,"B","J",1],[1,4,1,"C","J",1]]]

$string_array = '["A",2,[[1,1,1,"A","J",1],[1,2,1,"B","J",1],[1,4,1,"C","J",1]]]';

Convert String to Value/array in PHP

$array = eval("return $string_array;");

Result Array

print_r($array);

Full Code:

$string_array = '["A",2,[[1,1,1,"A","J",1],[1,2,1,"B","J",1],[1,4,1,"C","J",1]]]';

$array = eval("return $string_array;");
print_r($array);

Protecting a file using chattr command

Standard

Write protecting a file using chattr command

Let say you want to write protect the file called data.txt so that no other users can change it including root user, enter (you must login as the root user to use chattr command):

# chattr +i data.txt

To remove i attribute, enter:

# chattr -i data.txt

Javascript Ajax

Standard
<script type="text/javascript">
			
		function getHTTPObject(){
			var xmlhttp;
			// Attempt to initialize xmlhttp object
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				// Try to use different activex object
				try {
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (E) {
					xmlhttp = false;
				}
			}
			// If not initialized, create XMLHttpRequest object
			if (!xmlhttp) {
				if (typeof XMLHttpRequest != 'undefined') {
					xmlhttp = new XMLHttpRequest();
				}
				else {
					alert("This browser does not support AJAX.");
					return null;
				}
			}
			return xmlhttp;
		}
		 
		function loadTask (id,list) {
			httpObject = getHTTPObject();
			if (httpObject != null) {
				httpObject.open("GET", "<?php echo base_url(); ?>project/timesheet/task/"+id, true);
				httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				httpObject.setRequestHeader("Connection", "close");
				httpObject.onreadystatechange = function() {
		
						if (httpObject.readyState == 4) {
							if (httpObject.responseText == "") {
								document.getElementById('task'+list).innerHTML = 'No Task';
							}
							else {
								document.getElementById('task'+list).innerHTML = httpObject.responseText;
							}
						}
						else {
							document.getElementById('task'+list).innerHTML = 'Loading Task...';
						}
								
					}
				httpObject.send();
			}
		}

		</script>

Curl Grep Function

Standard
function curl_grap($Url){
 
    // is cURL installed yet?
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }
 
    // OK cool - then let's create a new cURL resource handle
    $ch = curl_init();
 
    // Now set some options (most are optional)
 
    // Set URL to download
    curl_setopt($ch, CURLOPT_URL, $Url);
 
    // Set a referer
    curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
 
    // User agent
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
 
    // Include header in result? (0 = yes, 1 = no)
    curl_setopt($ch, CURLOPT_HEADER, 0);
 
    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
 
    // Download the given URL, and return output
    $output = curl_exec($ch);
 
    // Close the cURL resource, and free system resources
    curl_close($ch);
 
    return $output;
}

Yum Repo Command

Standard

YUM REPO

To list only enabled repos, enter:

# yum repolist enabled

To list only disabled repos, enter:

# yum repolist disabled

To list all repos (default), enter:

# yum repolist all

To list available packages under a repo called ksplice-uptrack, enter:

# yum --disablerepo="<repo name>" --enablerepo="<repo name>" list available

Install PHPUnit in Xampp

Standard

First off I’m assuming xampp is installed to C:\xampp
1. Open a command prompt and go to C:\xampp\php
2. Type “pear update-channels” (updates channel definitions)
3. Type “pear upgrade” (upgrades all existing packages and pear)
4. Type “pear channel-discover components.ez.no” (this is needed for PHPUnit)
5. Type “pear channel-discover pear.symfony-project.com” (also needed by PHPUnit)
6. Type “pear channel-discover pear.phpunit.de” (This IS phpunit)
7. Type “pear install –alldeps phpunit/PHPUnit” (installs PHPUnit and all dependencies)

Special Query 1

Standard
SELECT t1.shade_sku SKU, t3.catalogue_title Product, t1.shade_name Shade, SUM( t1.order_quantity ) Quantity, SUM( t1.order_quantity * ( `item_promo_id` 
OR trans_promo_id ) <>0 ) Promo
FROM p_shop_order t1, p_shop_transaction t2, p_shop_catalogue_data t3
WHERE `order_product_id` = t3.catalogue_id AND t3.catalogue_lang = 'eng'
AND t1.`order_transaction` = t2.trans_id
AND t2.trans_status
IN (
'3', '4', '5', '6'
)
AND t2.trans_date_buy
BETWEEN '2013-03-01 00:00:00'
AND '2013-04-01 00:00:00'
GROUP BY t1.shade_sku
ORDER BY Quantity DESC 
LIMIT 20