CentoOS 7 – color terminal

There’s no magical 1 command, you need to do this in steps for each program you would like to use.

Color nano

root@lj: /# git clone https://github.com/nanorc/nanorc.git
root@lj: /# cd nanorc
root@lj: /# make install

And add into ~/.nanorc

include ~/.nano/syntax/html.nanorc
include ~/.nano/syntax/css.nanorc
include ~/.nano/syntax/php.nanorc
include ~/.nano/syntax/ALL.nanorc

Bash color

Add into ~/.bashrc

PS1='\e[33;1m\u@\h: \e[31m\W\e[0m\$ '

Git color

root@lj: /# git config --global color.ui auto

Cat color

This unfortunately isn’t possible. There’s a way around it though.

root@lj: /# yum install python-pygments
root@lj: /# echo "alias dog='pygmentize -g -O style=colorful,linenos=1'" > ~/.bashrc

mongodb.so: undefined symbol: php_json_serializable_ce in Unknown on line 0

I’ve encountered following error when running mongodb php module

Dec 12 09:56:02 lj php-fpm[3142]: [12-Dec-2016 09:56:02] NOTICE: PHP message: PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mongodb.so' - /usr/lib64/php/modules/mongodb.so: undefined symbol: php_json_serializable_ce in Unknown on line 0

Few google searches reveal this can be fixed by simply making sure json is loaded prior to mongodb.so – which by default isn’t.
There are many ways to do that, for example find how your json is loaded

[root@lj php.d]# php -i | grep "json"
/etc/php.d/json.ini,

Now open json.ini and add

extension=mongodb.so

at the end – and remove mongodb.ini from the folder.

Or rename json.ini to 20-json.ini and mongodb.ini to 30-mongodb.ini
You can also add priority at top of the ini file priority=30

Don’t forget to restart php after each change, if you are running mod_php, just do systemctl restart httpd, for php-fpm it’s systemctl restart php-fpm.

Now the weird thing is – none of this worked for me.
No matter how I loaded mongodb, it always failed with this error. I got the latest pecl packages, everything up to date – it simply didn’t work.
The only solution for me was to roll back to previous version of mongodb

[root@lj php.d]# pecl uninstall mongodb
[root@lj php.d]# pecl install mongodb-1.1.9
[root@lj php.d]# echo "extension=mongodb.so" > /etc/php.d/zz-99-mongo.ini

I called the file zz-99 to make sure it’s always loaded last. My ini files do not start with 20-*.ini to set priority.
Good luck.

CentOS7 php redis server – permission denied

Fresh installation of redis, out of the box and it doesn’t work.
Never had this issue on Ubuntu ..

Warning: Redis::connect(): connect() failed: Permission denied in /var/www/vhosts/xxx/httpdocs/_/_.redis.php on line 12

We need to enable httpd to use redis’ default port 6379:

yum install policycoreutils-python
semanage port -m -t http_port_t -p tcp 6379
semanage port -l | egrep '(^http_port_t|6379)'
http_port_t tcp 6379, 80, 81, 443, 488, 8008, 8009, 8443, 9000
redis_port_t tcp 6379

No need to restart redis or httpd after this, it should work right away.

Run nodejs application in background

There are several ways to do this, you can either use nohup, or append & at the end of command – personally, I’ve found forever to do the job perfectly fine.
Assuming you already have npm installed, if not, just do

sudo apt-get install npm

Then install forever

npm install forever --global

Now you can run it like this

forever start app.js

And it will take care of everything.

/usr/bin/env: node: No such file or directory

Can be fixed by creating symlink.

cd /usr/bin
sudo ln -s nodejs node

View list of running nodejs apps in forever

root@ubuntu:/# forever list
info:    Forever processes running
data:        uid  command         script forever pid   id logfile                 uptime       
data:    [0] mqgC /usr/bin/nodejs app.js 29570   31398    /root/.forever/mqgC.log 0:1:25:6.344 

To see logs for your application, do forever logs + ID of the app ( in our list it’s [0] – 0 )

root@ubuntu:/# forever logs 0
data:    app.js:31398 - Node app is running on port 5000

Convert unix timestamp to human readable date in MySQL select

If you are using INT for storing timestamps in your mysql database, very often you are dealing with weird numbers that don’t tell you much, e.g.

mysql> select time from example LIMIT 0,5;
+------------+
| time       |
+------------+
| 1480496034 |
| 1477817634 |
| 1472547234 |
| 1469868834 |
| 1467276834 |
+------------+
5 rows in set (0.00 sec)

To convert it into human readable format, use

mysql> select FROM_UNIXTIME(time) from example LIMIT 0,5;
+---------------------+
| FROM_UNIXTIME(time) |
+---------------------+
| 2016-11-30 09:53:54 |
| 2016-10-30 09:53:54 |
| 2016-08-30 10:53:54 |
| 2016-07-30 10:53:54 |
| 2016-06-30 10:53:54 |
+---------------------+
5 rows in set (0.00 sec)

You can even change the date format freely using DATE_FORMAT function ( www.w3schools.com/sql/func_date_format.asp )

mysql> select FROM_UNIXTIME(time), DATE_FORMAT(FROM_UNIXTIME(time), '%j. day of year on %a') from example LIMIT 0,5;
+---------------------+-----------------------------------------------------------+
| FROM_UNIXTIME(time) | DATE_FORMAT(FROM_UNIXTIME(time), '%j. day of year on %a') |
+---------------------+-----------------------------------------------------------+
| 2016-11-30 09:53:54 | 335. day of year on Wed                                   |
| 2016-10-30 09:53:54 | 304. day of year on Sun                                   |
| 2016-08-30 10:53:54 | 243. day of year on Tue                                   |
| 2016-07-30 10:53:54 | 212. day of year on Sat                                   |
| 2016-06-30 10:53:54 | 182. day of year on Thu                                   |
+---------------------+-----------------------------------------------------------+
5 rows in set (0.00 sec)