Basic PHP

การแสดงค่าตัวแปร

<?

$para1 = “12345”;

$para2[] = “1111”;

$para2[] = “2222”;

$para2[] = “3333”;

echo $para1;   /// 12345

echo $para2[0]; /// 1111

echo $para2[2]; /// 3333

print_r($para2); /// Echo all parameter in array (For debug mode)

การ Echo รูปแบบตัวเลข

echo number_format($para1); /// 12,345

echo number_format($para1,2); /// 12,345.00

//// การ Echo ตัวแปรต้องใส่ . คั่น

echo $para1.$para2[1] /// 123452222

//// การ Echo หลายตัวแปรแบบมีข้อความคั่น

echo $para1.’-‘.$para2[1] /// 12345-2222

การรับค่าตัวแปรจาก URL เช่น  www.test.com/test.php?id=1234&pass=abcd

file : test.php

<?

$id = $_REQUEST[‘id’];

$pass = $_REQUEST[‘pass’];

echo $id.’/’.$pass; /// 1234/abcd

?>

การดัก if else และการบวก ลบ คูณ หาร

<?

$data1 = “123”;

$data2 = “10”;

$value = $data1*$data2;

echo $value; /// 1230

if($value > 1000){

echo 2;

}else if($value > 500){

echo 1;

}else{

echo 0;

}

?>

Basic PHP Mysql – Connect Database

<?php
$strUser=”mysqluser”;
$strPassword=”mysqlpass”;
$strHost=”localhost”;
$strDB=”databasename”;
$con = mysql_connect($strHost,$strUser,$strPassword);
mysql_select_db ($strDB,$con);
mysql_query(“SET NAMES UTF8”);

?>

เรียกข้อมูลเดี่ยวจากฐานข้อมูล

$rs = mysql_query(“select * from table where id=’123′  “) or die (“Err Can not to result”) ;
$rc = mysql_fetch_array($up_sql);
$id = $rc[‘id’];

เรียกข้อมูลหลายแถวจากฐานข้อมูล

$rs = mysql_query(“select * from table  “) or die (“Err Can not to result”) ;
while($rc = mysql_fetch_array($up_sql)){

$id = $rc[‘id’];

}

เขียนเมื่อ หมวดหมู่ PHP

Set USB Static ports numbers

root@cmd1:~# udevadm info -a -n /dev/ttyUSB0 | grep ‘KERNELS’
KERNELS==”ttyUSB0″
KERNELS==”3-1.2.1:1.0″
KERNELS==”3-1.2.1″
KERNELS==”3-1.2″
KERNELS==”3-1″
KERNELS==”usb3″
KERNELS==”0000:00:1a.0″
KERNELS==”pci0000:00″
root@cmd1:~#nano /etc/udev/rules.d/99-usb-serial.rules

SUBSYSTEM==”tty”, KERNELS==”3-1.3.1:1.0″, SYMLINK+=”ttyUSB.Port1″
SUBSYSTEM==”tty”, KERNELS==”3-1.3.4:1.0″, SYMLINK+=”ttyUSB.Port2″
SUBSYSTEM==”tty”, KERNELS==”3-1.2.1:1.0″, SYMLINK+=”ttyUSB.Port3″
SUBSYSTEM==”tty”, KERNELS==”3-1.2.2:1.0″, SYMLINK+=”ttyUSB.Port4″
SUBSYSTEM==”tty”, KERNELS==”3-1.4.1:1.0″, SYMLINK+=”ttyUSB.Port5″
SUBSYSTEM==”tty”, KERNELS==”3-1.2.3:1.0″, SYMLINK+=”ttyUSB.Port6″
SUBSYSTEM==”tty”, KERNELS==”3-1.4.2:1.0″, SYMLINK+=”ttyUSB.Port7″
SUBSYSTEM==”tty”, KERNELS==”3-1.2.4:1.0″, SYMLINK+=”ttyUSB.Port8″
SUBSYSTEM==”tty”, KERNELS==”3-1.4.3:1.0″, SYMLINK+=”ttyUSB.Port9″
SUBSYSTEM==”tty”, KERNELS==”3-1.4.4:1.0″, SYMLINK+=”ttyUSB.Port10″
SUBSYSTEM==”tty”, KERNELS==”3-1.3.3:1.0″, SYMLINK+=”ttyUSB.Port11″

Fixing permission errors with vsftpd on Ubuntu

nginx needs read access only, so you can safely chown the directory to your FTP user. However, that won’t be as secure because your password is sent as plaintext over the internet when you use FTP — consider using SFTP instead 🙂.

sudo chown -R youruser:youruser /var/www

If you’re running a web application that needs write access to a specific directory, you will need to change the said directory’s group ownership to www-data and set group write permissions:

sudo chgrp -R www-data /var/www/directory
sudo chmod -R g+w /var/www/directory

 

Building Elastix MT via RPM Repo CentOS 6.7 for this build.

#!/bin/bash

# SAM's Elastix MT on CentOS 6 Installation and Build Script
# http://mangolassi.it/topic/6243/building-elastix-mt-via-rpm-repo
# Report any issues or questions on that thread.

yum -y install epel-release
yum -y install fail2ban htop sysstat glances wget screen
yum -y update
cd /tmp
wget http://sourceforge.net/projects/elastix/files/Elastix%20PBX%20Appliance%20Software/3.0.0/latest/Elastix-3.0.0-Stable-x86_64-bin-10nov2014.iso/download
mv download elastixmt.iso
mkdir -p /mnt/elastixmt
mount -o loop /tmp/elastixmt.iso /mnt/elastixmt
cat > /etc/yum.repos.d/Elastix.repo <<EOF
[Elastix-cd]
name=Elastix-cd
baseurl=file:///mnt/elastixmt/
gpgcheck=0
enabled=1
EOF
cd /mnt/elastixmt/Elastix
for i in $(ls); do yum -y install $i; done
for i in $(ls); do yum -y install $i; done
/etc/init.d/mysqld start
/etc/init.d/elastix-firstboot start && reboot

 

Install Cacti on CentOS 6/7

Step 1 – Prerequisites

First we need to install some of the software packages needed for Cacti to run properly. Software which is not included or enabled in the base CentOS 6 installation are:

  • rrdtool
  • apache
  • mysql
  • cron
  • gcc

Let’s use yum to get these installed.

Centos 6:
yum -y install mysql-server php php-cli php-mysql net-snmp-utils rrdtool \
  php-snmp gcc mysql-devel net-snmp-devel autoconf automake libtool dos2unix wget help2man

Centos 7:
yum -y install mariadb-server php php-cli php-mysql net-snmp-utils rrdtool \
  php-snmp gcc mariadb-devel net-snmp-devel autoconf automake libtool dos2unix wget help2man

gcc and the devel packages are required for the installation of spine, hence that’s why we include it here.

Now let’s make sure that our webserver and the database are automatically starting up after a reboot. Use the following commands to enable these:

CentOS 6:
chkconfig httpd on
chkconfig mysqld on
chkconfig crond on

CentOS 7:
systemctl enable httpd.service
systemctl enable mariadb.service
systemctl enable crond.service

Now that we did make sure that these services start after a reboot, let’s start them manually now in order to continue the installation. Cron may already be running so don’t panic if you don’t see the usual start message:

CentOS 6:
service httpd restart
service mariadb restart
service crond restart

CentOS 7:
systemctl restart httpd.service
systemctl restart mariadb.service
systemctl restart crond.service

 

Step 2 – Cacti Files

Let’s now move to the actualy installation of Cacti. First we need to download and extract it. As of version 0.8.8, a fully patched Cacti including the Plugin Architecture (PIA) is officially available, so we’re downloading that one:

cd /var/www/html
wget http://www.cacti.net/downloads/cacti-0.8.8g.tar.gz
tar -xzvf cacti-0.8.8g.tar.gz

I usually suggest to create a symbolic link to the newly created directory “cacti-0.8.8b”. This will make upgrades to never Cacti versions easier:

ln -s cacti-0.8.8g cacti

Step 3 – Cron and file permissions

Cacti uses cron (scheduled task) in order to execute its polling process.  It’s always a good idea to run this under a special user. Let’s create the system “cacti” user now:

adduser -d /var/www/html/cacti -s /sbin/nologin cacti

Having done that, we can now  add a new cron entry to your system for a 5 minute polling interval using the following command:

echo "*/5 * * * * cacti php /var/www/html/cacti/poller.php &>/dev/null" >> /etc/cron.d/cacti

Finally, we also need to make sure that the permissions on the log and rra directories are set correctly:

cd /var/www/html/cacti
chown -R cacti.apache rra log  
chmod 775 rra log

 

Step 4 – Cacti Database

Now that we have extracted the cacti files, we can move on preparing the database for the final installation step. Your first step should be securing the mysql database. The following command will help you with this task on a CentOS system. Make sure to select a strong password for root, e.g. MyN3wpassw0rd

/usr/bin/mysql_secure_installation

Let’s create a new database and assign a special user to it:

mysqladmin -u root -p create cacti
mysql -p cacti < /var/www/html/cacti/cacti.sql
mysql -u root -p

With the last command, you should be seing a mysql prompt where you can enter mysql commands. Here we are going to create the special cacti user. That user only needs to be able to connect from the local system and should have a strong password as well. Enter the following commands and make sure to replace the password:

GRANT ALL ON cacti.* TO cactiuser@localhost IDENTIFIED BY 'MyV3ryStr0ngPassword';
flush privileges;
exit

We now have the cacti files and the cacti database setup. The last step before moving to the web-based installer is setting the database credentials within the Cacti config file:

cd /var/www/html/cacti/include/
vi config.php

Change the $database_ lines to fit your new settings:

$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cactiuser";
$database_password = "MyV3ryStr0ngPassword";
$database_port = "3306";
$database_ssl = false;

Depending on your installation, you should also uncomment the following line. In our example we have to make sure the following line is there:

$url_path = "/cacti/";

Step 5 – Adding firewall rules

The following settings will add access rules to http and https from outside:

Centos 7:
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --reload

Step 6 – Important PHP Settings

The default PHP installation usually has not configured the correct timezone or php error reporting. While not required to run Cacti, it’s highly recommended to enable error reporting to syslog for troubleshooting issues with plugins or other scripts.

The following lines need to be enabled/configued in your /etc/php.ini file:

; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Europe/Berlin

and

; Log errors to syslog (Event Log on NT, not valid in Windows 95).
error_log = syslog

Step 7 – Running the Web-based installer

Let’s move on to the web-based installer.

Login with admin/admin and you’re ready to go !

Please go to “Console -> System Utilities” and click on “Rebuild Poller Cache” after the first login!

 

How To Install and Configure SNMP on CentOS

Introduction

SNMP, or Simple Network Management Protocol, is widely used to communicate with and monitor network devices, servers, and more, all via IP. In this case, we’ll be installing an SNMP agent on a CentOS 6.5 server, which will allow for collection of data from our server, and make the information available to a remote SNMP manager.

Pre-Flight Check

  • These instructions are intended for installing SNMP and doing a very basic configuration.
  • I’ll be working from a Liquid Web Core Managed CentOS 6.5 server, and I’ll be logged in as root.

Install SNMP and SNMP Utilities

Installing SNMP and some optional SNMP utilities is as simple as running one command:

yum -y install net-snmp net-snmp-utils

Add a Basic Configuration for SNMP

Now, let’s take the default SNMP configuration file, /etc/snmp/snmpd.conf and move it to an alternate location, /etc/snmp/snmpd.conf.orig.

mv /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.orig

And now we’ll create a new /etc/snmp/snmpd.conf:

vim /etc/snmp/snmpd.conf

For a refresher on editing files with vim see: New User Tutorial: Overview of the Vim Text Editor

Insert the following text into the new /etc/snmp/snmpd.conf

# Map 'idv90we3rnov90wer' community to the 'ConfigUser'
# Map '209ijvfwer0df92jd' community to the 'AllUser'
#       sec.name        source          community
com2sec ConfigUser      default         idv90we3rnov90wer
com2sec AllUser         default         209ijvfwer0df92jd
# Map 'ConfigUser' to 'ConfigGroup' for SNMP Version 2c
# Map 'AllUser' to 'AllGroup' for SNMP Version 2c
#                       sec.model       sec.name
group   ConfigGroup     v2c             ConfigUser
group   AllGroup        v2c             AllUser
# Define 'SystemView', which includes everything under .1.3.6.1.2.1.1 (or .1.3.6.1.2.1.25.1)
# Define 'AllView', which includes everything under .1
#                       incl/excl       subtree
view    SystemView      included        .1.3.6.1.2.1.1
view    SystemView      included        .1.3.6.1.2.1.25.1.1
view    AllView         included        .1
# Give 'ConfigGroup' read access to objects in the view 'SystemView'
# Give 'AllGroup' read access to objects in the view 'AllView'
#                       context model   level   prefix  read            write   notify
access  ConfigGroup     ""      any     noauth  exact   SystemView      none    none
access  AllGroup        ""      any     noauth  exact   AllView         none    none

The above text is noted with basic information on the function of each configuration line. In short, we’re creating two scenarios for polling information from SNMP version 2c.

Note: SNMPv2c contains some security enhancements over SNMPv1 but uses the existing SNMPv1 administration structure, which is “community” based. Areas of improvement include: transport mappings, protocol packet types, and MIB structure elements.

In the first scenario: ConfigUser is assigned to ConfigGroup and may only use SNMP security model 2c,ConfigGroup can use the SystemView, SystemView is assigned to two OID sub-trees, and all of this is referenced in an SNMP poll by the secret, and unique community string idv90we3rnov90wer.

In the second scenario: AllUser is assigned to AllGroup and may only use SNMP security model 2c,AllGroup can use the AllView, AllView is assigned to the entire OID tree, and all of this is referenced in an SNMP poll by the secret, and unique community string 209ijvfwer0df92jd.

Important Tip: Be ABSOLUTELY SURE that you choose a unique community string and replace the community strings in the above examples. Keep each secret, and keep each safe.

Exit vim, and restart the SNMP service to reload the new configuration file:

service snmpd restart

Configure SNMP to start when the server boots:

chkconfig snmpd on

Test the SNMP Configuration

Now let’s test the SNMP configuration… try running the following two commands:

snmpwalk -v 2c -c idv90we3rnov90wer -O e 127.0.0.1
snmpwalk -v 2c -c 209ijvfwer0df92jd -O e 127.0.0.1

Note: The default port for SNMP is 161 and 162. If you’re going to connect to SNMP from a remote server, be sure your server’s firewall has the appropriate ports open.

The result for your first command should be about 33 lines, and contain some basic system information. The result for the second command should contain a lot more information about your system, and will likely be thousands of lines.