Setup python app in centos from scratch (centos 6.9+uwsgi+nginx+flask+mysql)

Initial setup

$ sudo yum update
$ sudo yum install epel-release
$ sudo yum groupinstall "Development tools"
$ sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel telnet htop
$ sudo yum install python-devel python-virtualenv
$ sudo yum install mysql-connector-python mysql-devel mysql-server

Install Python

Download and install Python : https://www.python.org/

./configure && make && make altinstall

Install uWSGI

$ wget https://bootstrap.pypa.io/get-pip.py
$ which python2.7
$ sudo /usr/local/bin/python2.7 get-pip.py
$ which pip2.7
$ sudo /usr/local/bin/pip2.7 install uWSGI
$ which uwsgi
$ uwsgi --version

Setup vassels

$ sudo mkdir -p /etc/uwsgi/vassels

Setup Emperor service

$ sudo vim /etc/init.d/emperor
#!/bin/sh
# chkconfig: 2345 99 10
# Description: Starts and stops the emperor-uwsgi
# See how we were called.
RUNEMPEROR="/usr/local/bin/uwsgi --emperor=/etc/uwsgi/vassels"
PIDFILE=/var/run/emperor-uwsgi.pid
LOGFILE=/var/log/uwsgi/emperor.log
start() {
if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
echo 'Service emperor-uwsgi already running' >&2
return 1
fi
echo 'Starting Emperor...' >&2
local CMD="$RUNEMPEROR &> \"$LOGFILE\" & echo \$!"
su -c "$CMD" > "$PIDFILE"
echo 'Service started' >&2
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service emperor-uwsgi not running' >&2
return 1
fi
echo 'Stopping emperor-uwsgi' >&2
kill -7 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}
status() {
if [ ! -f "$PIDFILE" ]; then
echo "Emperor is not running." >&2
return 1
else
echo "Emperor (pid  `cat ${PIDFILE}`) is running..."
ps -ef |grep `cat $PIDFILE`| grep -v grep
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage: emperor {start|stop|restart}"
exit 1
esac

 Setup app user & environment

$ useradd foobar
$ usermod -md /srv/foobar foobar
$ chmod 755 /srv/foobar
$ sudo su - foobar
foobar@local~$ virtualenv --python=python2.7 ~/venv
foobar@local~$ mkdir www
foobar@local~$ mkdir logs
foobar@local~$ touch logs/uwsgi.log
foobar@local~$ touch uwsgi.ini
foobar@local~$ echo "source ~/venv/bin/activate" >> ~/.bashrc
foobar@local~$ source ~/venv/bin/activate
(venv)foobar@local~$ vim uwsgi.ini
[uwsgi]
master = true
processes = 2
socket = /tmp/foobar.sock
chdir = /srv/foobar/www
virtualenv = /srv/foobar/venv
module = app:app
uid = foobar
chown-socket = foobar:nginx
chmod-socket = 660
vacuum = true
die-on-term = true
python-autoreload = 3
py-autoreload = 1
logger = file:/srv/foobar/logs/uwsgi.log

Exit from foobar user & create uwsgi symlink

(venv)foobar@local~$ exit
$ sudo ln -s /srv/foobar/uwsgi.ini /etc/uwsgi/vassels/foobar.ini

Start emperor service & setup set the startup

$ sudo service emperor start
$ sudo chkconfig emperor on

Renew ssl certificate let’s encrypt with nginx

When you hate to see your website has ssl invalid certificate and it crossed out like this:

That means you need to update ssl certificate, in this case I use let’s encrypt. I just want to get rid of the invalid ssl certificate logo that make your website looks very unprofessional 🙂

Navigate to path where you place let’s encrypt directory:

$ cd ~/letsencrypt
~/letsencrypt$ sudo ./certbot-auto renew

Finally, restart the nginx, This is using centos, so it’s gonna be like this:

$ sudo /etc/init.d/nginx restart

Done

WordPress still cannot Establishing a Database Connection – Error

Have you ever experiencing this problem like this? after your install wordpress, setup anything, webserver, database, everything is completed. Yet, wordpress still cannot establishing your database connection.

Eventhough you’ve already make sure that your database is up and running (I’m using MySQL) and your port is already open, though.

Trust me! I’ve done anything properly, I spent hours just to figure out what the root cause was and after some googling and stackoverflowing, I found this problem was due to SE in linux.

$ sudo setsebool -P httpd_can_network_connect_db=1

Thanks in advance SElinux!

Configure php-fpm and nginx to run in low memory server

It was pain in the ass to have php-fpm and nginx together to serve php app, especially when you’re running on low-memory server, especially when you’re running cms like wordpress which basically heavy duty. I had a Centos server running with memory only 1024Mb (1Gb).

My web kept crushed every single time. And the problem stil remains, memory leak.

I don’t know what the root cause was. I still don’t know what that is but I think it’s something to do with php-fpm configuration or even nginx.

Just several days ago I had my blog up and running with wordpress in the same type of server, same OS (Centos) with the same memory (1Gb) and since I didn’t want to use apache for some reason, guess I had to get my nginx and php-fpm working together again.

I thought my web would be running very smoothly since the blog was still unlikely to receive much traffic. But I was wrong, somehow the memory was leaked again. My server only running with 1024Mb, but I thought It didn’t matter. The only way to get this problem mitigated is to tune up both the php-fpm and nginx configurations which I never liked this. But I need this, so let’s get this done.

Continue reading “Configure php-fpm and nginx to run in low memory server”

Run Ruby on Rails App for the first time only with nginx

So I’m just lazy, I know there is better way to serve Ruby on Rails with unicorn or uWSGI, but I will try later.

This time I just want a have a quick look how’s the RoR running on the browser only with nginx, just a quick guide, no bullshit. so currently I have rails installed on my ubuntu machine 14.04 with mysql database configured and nginx. So make sure you have those things installed properly to follow this guide.

I usually create my local domain first to navigate it easier:

$ sudo echo -e "127.0.0.1\tmyapp.local" >> /etc/hosts

Create the nginx config for your web app :

$ sudo vim /etc/nginx/conf.d/myapp.conf

very simple myapp.conf looks like:

server {
listen 80;
server_name www.myapp.local myapp.local;
charset utf-8;
access_log /var/log/nginx/myapp.access.log;
error_log /var/log/nginx/myapp.error.log;
location / {
proxy_pass http://127.0.0.1:3000;
}
}

Check nginx config and restart

$ sudo nginx -t
$ sudo service nginx restart

Run the rails (I told you, I’m just lazy) :

$ cd /myapp
$ rails server &

And now, Rails is running with port 3000 which nginx already addressed to myapp.local domain. Open up the browser and navigate to http://myapp.local

Setup docker + jenkins + nginx

Let’s setup jenkins with docker with local url : jenkins.local

First, add local url to /etc/hosts

$ sudo echo -e "127.0.0.1\tjenkins.local" >> /etc/hosts

Install docker and pull newest jenkins image

$ sudo apt-get install docker.io
$ sudo docker pull jenkins
$ sudo docker run -itd --name jenkins --publish 8080:8080 --publish 50000:50000 jenkins

Setup nginx

$ sudo vim /etc/nginx/conf.d/jenkins.conf

nginx conf:

server {
listen 80;
server_name jenkins.local;
charset utf-8;
gzip_vary on;
access_log /var/log/nginx/jenkins.access.log;
error_log /var/log/nginx/jenkins.error.log;
location / {
proxy_pass http://127.0.0.1:8080;
}
}

Save config and restart nginx

$ sudo service nginx restart

Open browser, and navigate to http://jenkins.local

Setup nginx, php, wordpress in ubuntu

So after all these years I’ve had using apache for run something that runs with php, but since I know about nginx, I’ve been using this webserver instead of apache.

This wordpress blog is running with nginx and php-fpm, because not like apache that you can run anything about php without install the additional package. With nginx, you’ll need php-fpm to run php behind nginx.

This is the steps that I always use for make my php run behind nginx, especially when I setup this wordpress blog. Since I’m using mysql for database, you might want to install php package for mysql as well.

apt-get install php5-common php5-fpm php5-mysql

create nginx configuration for php website

vim /etc/nginx/conf.d/myweb.conf

myweb.conf

server {
listen 80;
server_name www.pulpn.com pulpn.com;
charset utf-8;
gzip_vary on;
access_log /var/log/nginx/myweb.access.log;
error_log /var/log/nginx/myweb.error.log;
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
location / {
root /var/www/pulpn;
index index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
location ~* \.php {
try_files $uri =404;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass  unix:/var/run/php5-fpm.sock;
}
}
}

Restart your nginx :

service nginx restart

Or

/etc/init.d/nginx restart