Koneksi MySQL dengan C++ pada Linux

Pasang libmysql++ dengan cara

sudo apt-get install libmysql++ libmysql++-dev

tambahkan kode berikut pada file test.cpp

#include <iostream>
#include <vector>  
#include <string>  
#include <stdio.h>  
#include <stdlib.h> 
#include <string.h>


#include <cgicc/CgiDefs.h> 
#include <cgicc/Cgicc.h> 
#include <cgicc/HTTPHTMLHeader.h> 
#include <cgicc/HTMLClasses.h>  


#include <mysql.h>


using namespace std;
using namespace cgicc;


#define SERVER "localhost"
#define USER "root"
#define PASSWORD "winda1984"
#define DATABASE "user"


int main()
{


   Cgicc formData;
   
   cout << "Content-type:text/html\r\n\r\n";
   cout << "<html>\n";
   cout << "<head>\n";
   cout << "<title>Using GET Methods</title>\n";
   cout << "</head>\n";
   cout << "<body>\n";


   form_iterator t = formData.getElement("t");  
   if( !t->isEmpty() && t != (*formData).end()) {  
      cout << "Table: " << **t << endl;  
 
      MYSQL *connect;
      connect=mysql_init(NULL);
      if (!connect)
      {
         cout<<"Koneksi MySQL gagal";
         return 1;
      }
      connect=mysql_real_connect
         (connect, "localhost", "user", "password" , "database", 0, NULL, 0);
      if (connect)
      {
         cout<<"connection Succeeded\n";
      }else{
         cout<<"connection failed\n";
      }

      MYSQL_RES *res_set;
      MYSQL_ROW row;
      string str =  "select * from " + **t;

      mysql_query (connect,str.data());
      unsigned int i =0;
      res_set = mysql_store_result(connect);
      unsigned int numrows = mysql_num_rows(res_set);
      while (((row= mysql_fetch_row(res_set)) !=NULL ))
      { 
         cout << "| \t" <<  row[i] <<  "\t |"<< endl;
      }
      mysql_close (connect);
   } else {
      cout << "No text entered for table" << endl;  
   }
   
   cout << "<br/>\n";
   cout << "</body>\n";
   cout << "</html>\n";

   return 0;
}

compile dengan perintah

g++ -o test test.cpp -L/usr/include/mysql -lmysqlclient -lcgicc -I/usr/include/mysql

sumber: https://sandeepghai.wordpress.com/2011/08/07/linking-of-mysql-database-with-c-on-linux-machine/

Mengaktifkan modul mod_rewrite pada Apache2

Pasang symbolic link di /etc/apache2/mods-enabled untuk file rewrite.load
cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/rewrite.load
atau cara cepat dengan aktifkan melalui
sudo a2enmod rewrite
buat file konfigurasi rewrite
sudo leafpad /etc/apache2/conf-available/rewrite.conf
isikan dengan
<IfModule mod_rewrite.c>
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</IfModule>
buat symbolic link di /etc/apache2/conf-enabled
cd /etc/apache2/conf-enabled
sudo ln -s ../conf-available/rewrite.conf
jalankan ulang atau set baca ulang konfigurasi apache2
sudo service apache2 reload

buat file .htaccess

sudo nano /var/www/html/.htaccess

isikan contoh rule

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteRule ^beranda$ main.php?page=beranda [L]

ErrorDocument 404 http://localhost/error-404.html
ErrorDocument 403 http://localhost/error-403.html

Options All -Indexes
</IfModule>

Dari rule di atas, setiap mengarah ke http://localhost/beranda maka akan membuka http://localhost/main.php?page=beranda dan jika ada error, maka akan mengarah ke halaman error yang ditunjuk

^beranda$ adalah kalimat yang akan dicocokan.

  • ^ menyatakan awal dari URL, dimana localhost/ diabaikan.
  • $ menyatakan akhir dari URL
  • beranda string yang dicocokkan

main.php?page=beranda adalah tujuan dari file sesungguhnya.

[NC] ignores capitalization dan [L] menyatakan lowercase

set permision dari .htaccess ke mode read only

sudo chmod 644 /var/www/html/.htaccess

CGI menggunakan C++

sudo apt-get install m4 autoconf perl
cd ~
mkdir cgicc
wget ftp://ftp.gnu.org/gnu/cgicc/cgicc-3.2.16.tar.gz
tar xvf cgicc-3.2.16.tar.gz
cd cgicc-3.2.16
./configure --prefix=/usr
make
sudo make install
cd /usr/lib
ls libcgi*

source: http://derekmolloy.ie/beaglebone-cgicc/

sudo apt-get install libmysql++ libmysql++-dev

source: https://sandeepghai.wordpress.com/2011/08/07/linking-of-mysql-database-with-c-on-linux-machine/

buat file cgi dengan nama test.cpp  dan isikan kode berikut:

#include <iostream>
#include <vector> 
#include <string> 
#include <stdio.h> 
#include <stdlib.h>

#include <cgicc/CgiDefs.h> 
#include <cgicc/Cgicc.h> 
#include <cgicc/HTTPHTMLHeader.h> 
#include <cgicc/HTMLClasses.h>

using namespace std;
using namespace cgicc;

int main () {
Cgicc formData;

cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>Menggunakan GET</title>\n";
cout << "</head>\n";
cout << "<body>\n";

form_iterator nama = formData.getElement("nama"); 
if( !nama->isEmpty() && nama != (*formData).end()) { 
cout << "Halo " << **nama << endl; 
} else {
cout << "Nama tidak diisikan" << endl; 
}

cout << "<br/>\n";
cout << "</body>\n";
cout << "</html>\n";

return 0;
}

lalu compile dengan perintah

g++ -o test test.cpp -lcgicc

coba buka dengan

test?nama=latiful

sumber: https://www.tutorialspoint.com/cplusplus/cpp_web_programming.htm

Mengaktifkan CGI pada Apache

Konfigurasi apache pada linux ada di direktori /etc/apache2. Konfigurasi CGI dapat ditemukan di /etc/apache2/conf-available/serve-cgi-bin.conf yang merupakan symbolic link dari /etc/apache2/conf-enabled/serve-cgi-bin.conf. Terdapat bagian yang memetakan /cgi-bin pada URL ke direktori /usr/lib/cgi-bin/ dan memungkinkan untuk mengeksekusi CGI pada direktori ini.

Ganti bagian /cgi-bin/ menjadi path URL untuk CGI dan ganti  /usr/lib/cgi-bin untuk disesuaikan pada direktori tempat menyimpan script CGI:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">

Modul yang sudah terpasang pada Apache dapat ditemukan di direktori /etc/apache2/mods-available. Script  untuk menjalankan CGI ada di script pada file cgi.load

Untuk mengaktifkan modul ini, buat symbolic link pada /etc/apache2/mods-enabled, karena modul CGI secara default tidak aktif.

$ cd /etc/apache2/mods-enabled
$ sudo ln -s ../mods-available/cgi.load

Load kembali konfigurasi Apache dengan:

$ sudo service apache2 reload

source: https://code-maven.com/set-up-cgi-with-apache

Membuat HTPC dengan Linux Ubuntu

So, what is HTPC?

HTPC is Home Theater PC, from the name it can be guess that HTPC is a Home Theater using PC (Personal Computer). This is how to make it work:

Install XBMCBuntu

Setup LIRC

Keluar dari xbmc, login pilih ubuntu

klik Dash Home (logo ubuntu kiri atas) lalu ketik term, pilih UXterm

jika belum punya lirc, ketik:

sudo apt-get install lirc setserial

cari daftar remote yang support, di http://lirc.sourceforge.net/remotes/ misalnya dipilih remote TV Sharp http://lirc.sourceforge.net/remotes/sharp/G1324SA download dan copy ke share, ketik:

wget http://lirc.sourceforge.net/remotes/sharp/G1324SA

sudo mkdir /usr/share/lirc/remotes/sharp

sudo cp G1324SA /usr/share/lirc/remotes/sharp

perbaiki file hardware.conf dengan ketik:

sudo nano /etc/lirc/hardware.conf

perbaiki dengan:

# /etc/lirc/hardware.conf
#
#Chosen Remote Control
REMOTE="sharp" #isi dengan sharp atau nama remote apapun terserah
REMOTE_MODULES="lirc_dev lirc_serial" #tambahkan modul dev dan serial
REMOTE_DRIVER="default" #isi dengan default
REMOTE_DEVICE="/dev/lirc0" #defaultnya, cek dengan: ps ax | grep lirc
REMOTE_LIRCD_CONF="sharp/G1324SA" #isikan alamat conf remote yg tlh dicopy
REMOTE_LIRCD_ARGS=""

#tambahkan konfigurasi berikut
#ttyS0 berarti COM1 atau serial pertama, isikan ttyS1 jika COM2 dst
#lihat di ls /dev/ttyS*
#serial
COM_PORT=/dev/ttyS0  
DRIVER_OPTS="irq=4 io=0x3f8" #tergantung alamat dari serial
setserial /dev/ttyS0 uart none 

#Chosen IR Transmitter
TRANSMITTER="None"
TRANSMITTER_MODULES=""
TRANSMITTER_DRIVER=""
TRANSMITTER_DEVICE=""
TRANSMITTER_LIRCD_CONF=""
TRANSMITTER_LIRCD_ARGS=""

#Enable lircd
START_LIRCD="true"

#Don't start lircmd even if there seems to be a good config file
#START_LIRCMD="false"

#Try to load appropriate kernel modules
LOAD_MODULES="true"

# Default configuration files for your hardware if any
LIRCMD_CONF=""

#Forcing noninteractive reconfiguration
#If lirc is to be reconfigured by an external application
#that doesn't have a debconf frontend available, the noninteractive
#frontend can be invoked and set to parse REMOTE and TRANSMITTER
#It will then populate all other variables without any user input
#If you would like to configure lirc via standard methods, be sure
#to leave this set to "false"
FORCE_NONINTERACTIVE_RECONFIGURATION="false"
START_LIRCMD=""

perbaiki file lircd, ketik:

sudo nano /etc/lircd.conf

tambahkan baris:

include “/usr/share/lirc/remotes/sharp/G1324SA” #sesuaikan dengan letak file conf remote

OK, sekarang reboot Lanjutkan dengan cek modul, ketik:

lsmod | grep lirc

ps ax | grep lirc

jika ada tulisan: /usr/sbin/lircd berarti sudah ok lakukan cek dengan irw, ketik:

irw

lalu pencet remote. Jika ada keterangan tombol yang dipencet berarti OK.

Saran:

Instal gedit, editor text grafis,biar edit file conf mudah (perlu konesi internet)  ketik:

sudo apt-get update

sudo apt-get install gedit

Troubleshoot:

1. Jika ada tulisan Cannot open /dev/ttyS0: permision denied berarti seting tty salah

2. Jika pada saat irw tidak keluar apa-apa, file conf remote salah, buat baru: Matikan lirc, ketik:

sudo -s

/etc/init.d/lirc stop

irrrecord –driver=default –device=/dev/lircd0 Remotku –disable-namespace

lalu ikuti petunjuk, hingga diminta set nama tombol pada saat tulisan please enter name bla bla bla. isikan nama tombol, misalnya POWER dan tekan tombol power pada remote. Ikuti dengan tombol lain dan terakhir tekan enter saja. copy hasil konfigurasi ke dir remote

cp Remotku /usr/share/lirc/remotes/

ganti alamat konfigurasi file hardware.conf dan lircd.conf dari /sharp/G1324SA menjadi Remoteku jalankan lirc kembali, ketik

/etc/init.d/lirv start -v

exit

irw

harusnya sudah muncul tombolnya! tekan ctrl+c untuk keluar

Konfigurasi XBMC

edit file lirc, ketik:

sudo nano /usr/share/xbmc/system/Lircmap.xml

Tambahkan:

<remote device=”sharp”>
<power>POWER</power>
<select>TV/VIDEO</select>
<one>1</one>
<two>2</two>
<three>3</three>
<four>4</four>
<five>5</five>
<six>6</six>
<seven>7</seven>
<eight>8</eight>
<nine>9</nine>
<zero>0</zero>
<up>VOL+</up>
<down>VOL-</down>
<left>CH-</left>
<right>CH+</right>
<play>MENU</play>
<mute>MUTE</mute>
</remote>

 

How to make USBasp work with Arduino in Linux

Make sure USBasb plugged into USB Port and check whenever its conected or not by typing lsusb in terminal.

stufi1983@latif-laptop:~$ lsusb
Bus 001 Device 003: ID 0db0:6877 Micro Star International RT2573
Bus 001 Device 004: ID 058f:6387 Alcor Micro Corp. Flash Drive
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 003: ID 16c0:05dc Van Ooijen Technische Informatica shared ID for use with libusb
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

USBasp should detected with VendorID 16c0 and ProductID 05dc. Look at bus address. It says Bus 002 Device 003. See permission on Bus 002 Device 001 by typing ls -l /dev/bus/usb/002/003 in terminal.

stufi1983@latif-laptop:~$ ls -l /dev/bus/usb/002/003
crw-rw-r-- 1 root root 189, 130 Sep  6 10:05 /dev/bus/usb/002/003

Current permission is crw-rw-r– mean user dont have permission to write at the bus. So, change permission to 777 with chmod command.

stufi1983@latif-laptop:~$ sudo chmod 777 /dev/bus/usb/002/003
[sudo] password for stufi1983:

OK, Done. USBasp now already to use with Arduino. Select in Arduino  menu Tool>Progammer>USBasp. Make new file New>Examples>Basic>Blink. Then upload by using menu: File>Upload using Programer

Unable login 16.04

Press Ctrl+Alt+F3 and login into the shell.

Now run ls -lah. If in the output the line

-rw-------  1 root root   53 Nov 29 10:19 .Xauthority

then you need to do chown username:username .Xauthority and try logging in.

Else, do ls -ld /tmp. Check for the first 10 letters in the left: they should read exactly so: drwxrwxrwt.

drwxrwxrwt 15 root root 4096 Nov 30 04:17 /tmp

Else, you need to do sudo chmod a+wt /tmp and check again.

If not both, I’d recommend you either

  1. dpkg-reconfigure lightdm
  2. or uninstall, reinstall it.

Now press Alt+-> until you reach the login screen again, and restart.

After installing lubuntu

Installing packages

Untuk desktop

$ sudo apt-get install lubuntu-restricted-extras libdvd-pkg gnome-screenshot gksu leafpad default-jre wine winetricks playonlinux samba ufw

Untuk webserver

$ sudo apt-get install apache2 php libapache2-mod-php mysql-server php-mysql phpmyadmin yaz filezilla

$ sudo leafpad /etc/apache2/apache2.conf add: Include /etc/phpmyadmin/apache.conf

$ sudo service apache2 restart

$ sudo chown group:username /var/www/html

$ leafpad /var/www/html/index.php edit:

<?php
$servername = "localhost";
$username = "root";
$password = "123456";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
phpinfo();

?>

Test in browser: http://localhost/index.php

Untuk programming

$sudo apt-get install codeblock androidstudio monodevelop virtualbox

Enable Firewall

$ sudo ufw enable

$ sudo ufw status verbose

$ sudo ufw allow Samba

$ gksu leafpad /etc/default/ufw  modify: IPT_MODULES=”nf_conntrack_ftp nf_nat_ftp nf_conntrack_irc nf_nat_irc nf_conntrack_netbios_ns”

$ sudo ufw reload

 

Enable SSH (windows: xming dan putty)

$ sudo apt-get install openssh-server

$ sudo ufw allow 22

 

Optimize SWAP

$ gksu leafpad /etc/sysctl.conf

add:

vm.swappiness=1
vm.vfs_cache_pressure=50

Cek OpenGL

$ glxinfo |grep direct

Install samba

Add myusername to the samba

$ sudo smbpasswd -a myusername

Export the drive I needed (no guest user)

$ gksu leafpad /etc/samba/smb.conf

to add the following export (no real option needed here)

[files]
path = /home/myusername/sharingfolder
writeable = yes

save and Restart samba

$ sudo service smbd restart

VNC Server

source: http://homecircuits.eu/blog/lubuntu-installing-vnc-server/