Tag Archives: Linux

ntp.yaojun.sg – Stratum-1 NTP server on a Raspberry Pi

Posted on by .

I have been running ntp.yaojun.sg for NTP service on a Raspberry PI for a while. This is service is part of pool.ntp.org and responds to sg.pool.ntp.org and asia.pool.ntp.org. However, my SD card decided to give up recently when I wanted to explore SNMP tools for monitoring purposes.

The GPS module I am using is from HAB. I gotten this particular module late last year built specifically for the older Raspberry Pi Model B. My NTP service was made public around March 2014 using a ready made image. However, just looking at how 2014 turn out, it is important to keep both base system and NTP patched. In addition, it is not advisable to just run a “BlackBox” because you never know what’s going on inside it. So here I am restoring the service from a clean Raspbian image.

Do note that the module requires a GPS 3D lock for PPS to work, so remember to connect your antenna. I was scratching my head on why my PPS was not working and had to contact the friendly vendor to ask if I have a dead card.

Installing Raspbian into Raspberry Pi is a straight forward process. After the basic configuration (resizing the File System, locale, timezone and etc), remember to configure Rasbian to disable the serial shell under the advanced settings. Also update and patch the system.

apt-get update
apt-get dist-upgrade
rpi-update

Recent development in Raspbian allows the use of PPS devices without recompiling the kernel. We need to configure the system to use the PPS on the GPIO port.

Edit /boot/config.txt add the following line,

dtoverlay=pps-gpio,gpiopin=18

Add the following line to /etc/modules,

pps-gpio

Install the GPS and PPS tools

apt-get install pps-tools libcap-dev gpsd gpsd-clients python-gps

Configure gpsd to use the GPS sensor at /dev/ttyAMA0 and enable -n as a option using dpkg-reconfigure to allow ntpd to use the GPS clock and remember to enable the gpsd daemon

systemctl enable gpsd.socket
dpkg-reconfigure gpsd

and you will be able to see your GPS data using

cgps -s

Check that your PPS clock is working

ppstest /dev/pps0

Remove default NTP client (which does not work with the GPS and PPS)

apt-get remove ntp

Install NTP dependencies to ensure successful build of NTP from source

apt-get install libbsd-dev libssl-dev

Install checkinstall to compile source to binaries safely

apt-get install checkinstall

Get the latest NTP source code from www.ntp.org, untar and compile. This is not a fancy high speed CPU so compiling will take some time (estimate 30 minutes).

./configure --enable-linuxcaps --with-NMEA --with-ATOM
make 
checkinstall

Follow through checkinstall wizard.

Symbolically link compiled binaries to well defined paths,

ln -s /usr/local/bin/ntp* /usr/bin/
ln -s /usr/local/sbin/ntp* /usr/sbin/

Edit /etc/init.d/ntp to use the compiled binaries,

#PATH=/sbin:/bin:/usr/sbin:/usr/bin
PATH=/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#DAEMON=/usr/sbin/ntpd
DAEMON=/usr/local/sbin/ntpd

Edit /etc/ntp.conf to use PPS and GPS as Stratum-0 and Stratum-1. This will make PPS disciplined by GPS and verified by other NTP servers.

# pps-gpio on /dev/pps0
server 127.127.22.0 minpoll 4 maxpoll 4
fudge 127.127.22.0 refid PPS
fudge 127.127.22.0 flag3 1  # enable kernel PLL/FLL clock discipline
# gpsd shared memory clock
server 127.127.28.0 minpoll 4 maxpoll 4 prefer  # PPS requires at least one preferred peer
fudge 127.127.28.0 refid GPSD
fudge 127.127.28.0 time1 +0.150 stratum 1 # coarse processing delay offset

Lock the ntp binaries to prevent apt from overwriting from packages

apt-make hold ntp

Installing Centos 6 – LNMP

Posted on by .

I’ve decided to re-start blogging again! That is after 1 year of silence…

Well, I guess it’s time to start a Tech blog on what I do as a techie and to document what’s necessary, as a reference for myself and others if they chance into the problem, let me start first by getting LNMP to work on CentOS6. LNMP is otherwise know as Linux Nginx MySQL and PHP.

Installing CentOS6 is pretty straight forward. Pop the disc/USB in, follow the instructions and voilla, you have a working machine. There are some variations also like installing using the network method, but I won’t cover that here.

Nginx. Nginx is an awesome piece of web server. It’s lightweight and fast. Much lighter than the world famous Apache. Installing it in today’s context is rather easy. With the package already compiled for you.

Firstly, you have to add the Nginx repo into your repo list. And CentOS uses yum. So, you have to create a “nginx.repo” in “/etc/yum.repo./” with this following code

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

After that, it’s just run

yum install nginx

And nginx will be installed. Next, Centos usually has iptables enabled, iptables is a firewall, so you’ll have to add in a rule to allow 80 (http) or 443 (https) into the Allow rules.

vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
service nginx start

Then you restart iptables and http(s) should work.

Then for nginx to load everytime the machine boots

chkconfig --level 345 nginx on

MySQL, the lovely database magic box. Installing a updated copy is not that straight forward as it seems. I have to add additional repos, in this case “remi”‘s repo, set of repo maintained by this french guy. But you also have to get “epel” repo also. So here are the commands:

rpm -Uvh http://mirror.nus.edu.sg/fedora/epel/6/i386/epel-release-6-5.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

Then you install MySQL as such

yum --enablerepo=remi install mysql mysql-server

After installing the binaries, you have to install the database

mysql_install_db --user=mysql

After which start MySQL

mysqld_safe &

Change the default password using

mysql_secure_installation

verify that the change was correct by properly shutting down MySQL

mysqladmin shutdown -p

Similarly for load on reboot

chkconfig --levels 235 mysqld on

Note: unless necessary, remember to add additional firewall rule if you want your MySQL to be remotely accessible. There are also some configuration changes to me made to make that work which I will not cover here.


Now PHP. Pre-Hypertext Processor and my favourite language. We will still be using “remi” repo. So,

yum --enablerepo=remi install php php-fpm
yum --enablerepo=remi install php-gd php-mysql php-mbstring php-xml php-mcrypt php-pecl-apc php-pecl-memcache php-xmlrpc

After installation

service php-fpm start

To check whether PHP is working, you have to configure some things.

Firstly, edit nginx config file.

vim /etc/nginx/conf.d/default.conf
location / {
 root /usr/share/nginx/html;
 index index.html index.htm index.php;
 }

Uncomment the php-pfm codes and input the root of the web file directory.

location ~ .php$ {
 root html;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
 include fastcgi_params;
 }

Reload nginx

service nginx reload

And create a test file at the document root

vim /usr/share/nginx/html/info.php

Append the following

<?php phpinfo(); ?>

Go to the website

http://serverNameOrIPAddressHere/info.php

And you should see information related to PHP.

A recommendation by me will be to move PHP-FPM from the TCP socket to the Unix socket and it can be done in such a manner.

Firstly edit

vim /etc/php-fpm.d/www.conf

comment/delete and change the following lines to

;listen = 127.0.0.1:9000
listen = /tmp/php-fpm.sock
;listen.allowed_clients = 127.0.0.1

Change your user and group to

user = nginx
group = nginx

You can verify the changes using

netstat -l -n

Now if you reload your info.php() it will not work, you have to go and adjust the nginx server configuration file. Comment away the old TCP socket and replace with the Unix socket.

vim /etc/nginx/conf.d/default.conf
location ~ .php$ {
 root html;
 #fastcgi_pass 127.0.0.1:9000;
 fastcgi_pass unix:/tmp/php-fpm.sock;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
 include fastcgi_params;
 }

Restart nginx

service nginx restart

And reload your info.php test page to make sure that it’s running.

To make sure php-fpm runs every time the machine boots

chkconfig --levels 235 php-fpm on

An there as you have it, a Linux Nginx MySQL PHP machine is ready for you to use, be it for WordPress, OwnCloud, phpMyAdmin or other PHP based software.

Hope you had fun configuring it. 🙂