Install nginx + django + php + fastcgi on FreeBSD

Unix

23 Feb 2015

Install nginx + django + php + fastcgi on FreeBSD

Preston Garrison 0 Comments

pkg install py27-pip
pkg install uwsgi
pkg install nginx
pkg install py27-django18
pkg install php71

#first time only
portsnap fetch
portsnap extract

portsnap update

Now install php5, make sure you fpn is selected when it asks you

cd /usr/ports/lang/php5
make install
#make sure fpn is selected

Add items to startup, create php.ini, and start the servers

echo 'nginx_enable="YES"' >> /etc/rc.conf
echo 'php_fpm_enable="YES"' >> /etc/rc.conf

cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
/usr/local/etc/rc.d/php-fpm start
/usr/local/etc/rc.d/nginx start
mkdir -p /var/www/default

Now create /usr/local/etc/nginx/nginx.conf with only the following, this uses 8 processes for dual quad core cpus, its best to do 1 per core

#user  nobody;
worker_processes  8;
error_log /var/log/nginx-error.log;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;


    server {
#        listen       199.199.199.199:80;
		listen 80;
        server_name  beta.postingstats.com;

        location /media/ {
                alias /var/www/default/media/;
        }

        location /static {
                alias /var/www/default/static/;
        }

        location ~ \.html$ {
            root           /var/www/default/email_templates/;

        }

        location / {
                proxy_pass      http://127.0.0.1:9001;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/www/nginx-dist;
        }



        location ~ \.php$ {
            root           /var/www/default/;
            include fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /var/www/default$fastcgi_script_name;
            fastcgi_pass   127.0.0.1:9000;
        }
    }
}

Now create /usr/local/etc/uwsgi.ini

[uwsgi]
pythonpath = /var/www/default
module = wsgihandler
socket = 127.0.0.1:9001
master = true
chmod-socket = 664
uid = www
gid = www
#enable-threads = true
processes = 4
socket-timeout = 180
post-buffering = 8192
max-requests = 1000
buffer-size = 32768

Now add the appropriate start up items

echo 'uwsgi_enable="YES"' >> /etc/rc.conf
echo 'uwsgi_flags="-T --ini /usr/local/etc/uwsgi.ini"' >> /etc/rc.conf

Now create your django project

cd /var/www/default
django-admin.py startproject mysite

Restart

/usr/local/etc/rc.d/uwsgi restart
/usr/local/etc/rc.d/nginx restart

Now create a dummy php file for testing

echo '<?php phpinfo(); ?>' > /var/www/default/info.php

now test with http://ip/info.php

 

 

Preston Garrison