Tag Archives: mysql

Basic PHP PDO Extension Usage & Syntax

PDO Extension defines a consistent interface for accessing databases in PHP.

What does that mean? Well, it means programmers can write PHP codes which can be easily ported from one database type to another; let’s say you write your codes for MS SQL but then suddenly the project takes a turn and you need to adapt your codes for MySQL … would be great to be able to make the transition easy, wouldn’t be?

Now, you can read all about it on the Official PHP Manual and also you can read tutorials about it, but I’m about to give you below a simple example of the basic PDO Syntax.

$db = new PDO(
	'mysql:host = localhost; dbname=someDbName; charset=utf8', 

	$res = $db->query('err');
catch(PDOException $ex){
	echo "error";

//other code examples:
$stmt = $db->query('select * from table');
$results = $stmt-?fetchAll(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();

//here's how to get the ID of the last inserted row
//when you're doing an insert before
$lastId = $db->lastInsertId();

//preparing queries
$stmt = $db->prepare("select * from table where id=:id and name=:name");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

Setup new Centos VPS with LAMP, Git and gitolite

Recently I had to install and maintain several Centos servers and I had to repeat the same setup instructions multiple times.

I thought it’d be nice to have the following list of to-do steps which needed to be repeated on every server install, especially so other employees could take on this job after me.

The scenario I had to work on required the setup of a new Centos 6 server with LAMP (php 5.5 at least) and configuring Git and gitolite for git user-managing access.

So the first thing was to install a Centos 6.5 minimal distribution and then all the work was done through Putty and heavy-usage of yum.

To avoid conflicts later-on because of repository packages, one of my colleagues recommended me to use yum-plugin-priorities. (thanks, Madalin 😉 )

Now, although this plugin is a bit controversial, we found that during our exploitation it was really helpful in preventing our servers to become a packages-mess in no-time. So I’m gonna go along and recommend it further. You should go however to the link and read the documentation for yum-plugin-priorities to see if it suits your needs and to learn how to proper configure it.

So here’s the list. It’s not something new or original content, it’s more of a compilation list to have all the info in one place. I have included links to the sites from where the info was used.

  1. Install yum-plugin-priorities (say yes if you’re asked about a key, it’s because you’re using the repo’s for the first time
    yum install yum-plugin-priorities
  1. install wget:
    yum install wget
  2. cd to user’s home:
    cd ~
  3. install remi repo:
    wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmwget
    rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm

    You can read more about the remi repo here.

  4. install webtatic repo:
    rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm

    You can read more about the webtatic repo here.

  5. Important! edit repo files located in /etc/yum.repos.d/ and enable the ones you want and add priority=N, according to yum-priorities plugin required settings (http://wiki.centos.org/PackageManagement/Yum/Priorities)
  6. update your system
    yum update
  7. install sort-of lamp, but don’t install php, since we need php 5.5 at least which we’ll get from the webtatic repo one step later:
    Install Apache and start it:

    yum install httpd
    service httpd start

    Install MySQL and start it:

    yum install mysql, mysql-server
    service mysql start

    Secure your fresh MySQL installation by running this script:


    You can read more about the default configuration for securing your mysql on this tutorial from digitalocean.com

  8. Install PHP 5.5 and the modules you want (we installed them all)
    yum install php55w  php55w-bcmath  php55w-cli  php55w-common  php55w-dba  php55w-devel  php55w-embedded  php55w-enchant  php55w-fpm  php55w-gd  php55w-imap  php55w-interbase  php55w-intl  php55w-ldap  php55w-mbstring  php55w-mcrypt  php55w-mssql  php55w-mysqlnd  php55w-odbc  php55w-opcache  php55w-pdo  php55w-pear.noarch  php55w-pecl-apcu  php55w-pecl-apcu-devel  php55w-pecl-memcache  php55w-pecl-xdebug  php55w-pgsql  php55w-process  php55w-pspell  php55w-recode  php55w-snmp  php55w-soap  php55w-tidy  php55w-xml  php55w-xmlrpc
  9. don’t forget to add chkconfig setting so the services would start automatically
    chkconfig --levels 235 httpd on
    chkconfig --levels 235 mysqld on
  10. Optionally, install nodejs, if you need it (we did)
    yum install nodejs
  11. Install the dev-tools
    yum groupinstall "Development tools"
  12. Install Git and create user/group
    yum install git
    useradd git
    usermod -u 600 git
    groupmod -g 600 git
    passwd git
  13. Install gitolite on the server by loging in as git user and cloning the gitolite repo
    git clone https://github.com/sitaramc/gitolite
  14. Since gitolite administration is based on keys and we used windows machines to connect to the linux servers, we used putty to generate keys for every user. Copy-paste the text from puttygen as USERNAME.pub and save the private key as USERNAME.ppkThe .pub key will have to be uploaded by gitolite admin to the gitolite repos which the USERNAME will have access.

    We used this tutorial for our first install, what’s really important to keep in mind is that there are two machines in use: one is the server (linux server) and the other is the client (in the tutorial I’ve linked the client is also a linux box; in our scenario the clients were windows machines).

    It’s important to note that for the first use of gitolite, you have to create the .pub/.ppk pair of keys for the client user which will be the gitolite admin (so you’ll create this keys on the client machine, may that be linux, windows, w/e) and copy the .pub key into the git folder from the Server machine and give it proper permissions. Let’s say it will be git-admin.pub and git-admin.ppk like in the tutorial link above.
    Then, after you’ll install gitolite by logging in as user git onto the server machine, you’ll use the git-admin.pub key as argument to the initial setup of gitolite, like this:

    gitolite/install -ln
    gitolite setup -pk Git-Admin.pub
  15. Since we used windows machines to develop on, we installed git client on them and gitextensions also.For the git installer setup  we used the following options:
    – advanced context menu, associate .git
    – use git bash only
    – use plink
    – checkout windows style, commit linux style

    And for the gitextensions setup we used the following options:
    – install kdiff only
    – use putty
    – (dumb observation: sometimes the kdiff installer windows pops under the main gitextensions installer window and at first sight it seems the main installer hanged; it didn’t, it just awaits for your action on the windows behind)

  16. We also had an interesting setup where our git server would automatically deploy upon receive, on the httpd test server installed on the same machine. We used a script placed in hooks/post-receive folder with the following content:
    export GIT_WORK_TREE
    umask 002
    git checkout -f

    The script has to have proper permissions.
    Also, the www folder should be owned by apache user and have the group set to apache group and the git user should be added to the apache group.

    chmod +x hooks/post-receive
    chown apache:apache /home/path/to/your/www
    usermod --groups apache git

    Also, you have to init a new empty repo into the www folder

    cd /home/path/to/your/www
    mkdir newfolder
    cd newfolder
    git init
  17. After this, you can create new repos on the git server machine, from the client machine, by using the gitolite-admin repo.You must clone first the admin repo (git clone gitolite-admin.git) on your client machine and then use this repo as a setup tool for the repos you wanna create and manage.You can create new git repos, add users to those repos by putting their pub keys into the keys folder and setting up the gitolite.conf file.

    Every time you push the changes for the gitolite-admin repo, the new keys will be uploaded to server and gitolite.conf changes taken into consideration and this will basically be your tool to manage the git repos on the (test) server machine.

    A very good tutorial which we initially used when setting gitolite can be found here: http://sachinsharm.wordpress.com/2013/10/04/installsetup-and-configure-git-server-with-gitolite-and-gitweb-on-centosrhel-6-4/ 



External links for reference, used in this article:

remi repo: http://www.rackspace.com/knowledge_center/article/installing-rhel-epel-repo-on-centos-5x-or-6x
webtatic repo: http://webtatic.com/projects/yum-repository/
yum-priorities: http://wiki.centos.org/PackageManagement/Yum/Priorities
simple tutorial on lamp install: https://www.digitalocean.com/community/articles/how-to-install-linux-apache-mysql-php-lamp-stack-on-centos-6
about gitolite: http://gitolite.com/gitolite/index.html
large tutorial on git and gitolite: http://sachinsharm.wordpress.com/2013/10/04/installsetup-and-configure-git-server-with-gitolite-and-gitweb-on-centosrhel-6-4/
git clients: http://git-scm.com/downloads
gitextensions for windows: https://code.google.com/p/gitextensions/
good info on how to auto-deploy from git server: http://stackoverflow.com/questions/9132144/how-can-i-automatically-deploy-my-app-after-a-git-push-github-and-node-js
if you want to install phpmyadmin via yum: http://tecadmin.net/how-to-install-phpmyadmin-on-centos-using-yum/
in case you get httpd dead but subsys locked error: http://sandzoctanium.com/upgrade-from-php-5-3-to-php-5-5-and-get-error-httpd-dead-but-subsys-locked/