Tag Archives: nginx

nginx, php, php-fpm and postgresql – Gentoo.

Today we’re going to install nginx, php (with php-fpm) and postrgesql. I need to set up a new development environment locally so I figured I may as well just document it as well. I will be doing this on Gentoo.

First lets setup some useflags.

www-servers/nginx fastcgi nginx_modules_http_auth_basic nginx_modules_http_rewrite vim-syntax
dev-lang/php bzip2 cgi crypt curl gd hash json postgres sqlite3 unicode xml fpm
dev-db/postgresql-base readline

I took the doc useflag out because it has a lot of dependancies, and the docs are the same as the ones online! Now get on with some emerging (make sure you’ve syncd too!).

# emerge -a nginx php postgresql-server
 
These are the packages that would be merged, in order:
 
Calculating dependencies... done!
[ebuild  N    ] dev-libs/geoip-1.4.6  USE="-perl-geoipupdate"
[ebuild  N    ] dev-python/egenix-mx-base-3.1.3
[ebuild  N    ] app-admin/eselect-postgresql-0.3
[ebuild  N    ] app-vim/nginx-syntax-0.3.1
[ebuild  N    ] dev-db/postgresql-base-9.0.3  USE="nls pam readline ssl zlib -doc -kerberos -ldap -pg_legacytimestamp -threads" LINGUAS="-af -cs -de -es -fa -fr -hr -hu -it -ko -nb -pl -pt_BR -ro -ru -sk -sl -sv -tr -zh_CN -zh_TW"
[ebuild  N    ] www-servers/nginx-1.0.0-r1  USE="http http-cache ipv6 pcre ssl vim-syntax -aio -debug -libatomic" NGINX_MODULES_HTTP="access auth_basic autoindex browser charset empty_gif fastcgi geo gzip limit_req limit_zone map memcached proxy referer rewrite scgi split_clients ssi upstream_ip_hash userid uwsgi -addition -cache_purge -dav -degradation -ey_balancer -flv -geoip -gzip_static -headers_more -image_filter -passenger -perl -push -random_index -realip -secure_link -slowfs_cache -stub_status -sub -upload -xslt" NGINX_MODULES_MAIL="-imap -pop3 -smtp"
[ebuild   R   ] dev-lang/php-5.3.6  USE="postgres* -mysql*"
[ebuild  N    ] dev-db/postgresql-server-9.0.3  USE="nls perl python -doc -pg_legacytimestamp (-selinux) -tcl -uuid -xml" LINGUAS="-af -cs -de -es -fa -fr -hr -hu -it -ko -nb -pl -pt_BR -ro -ru -sk -sl -sv -tr -zh_CN -zh_TW"
 
Would you like to merge these packages? [Yes/No]

Before we do anything else, you must set the locale for postgresql. Edit /etc/conf.d/postgresql-9.0 and set PG_INITDB_OPTS appropriately! I used –locale=en_GB.UTF-8.
Now we must setup the initial database environment. emerge will tell you what command to run. For this version I must run

#  emerge --config =dev-db/postgresql-server-9.0.3

Start the daemon.

#  /etc/init.d/postgresql-9.0 start

Finally, set up our users. Connect as root and set the password.

# psql -U postgres
psql (9.0.3)
Type "help" for help.
 
postgres=# \password
Enter new password:
Enter it again:
postgres=# \q

Edit vim /var/lib/postgresql/9.0/data/pg_hba.conf and set “trust” to “password”.
Add users to postgres group, if they will be using the PostgreSQL server. I added nginx.

# gpasswd -a nginx postgres

Finally restart the server

# /etc/init.d/postgresql-9.0 restart

Let’s add a user and a database to play with

# psql -U postgres
Password for user postgres:
psql (9.0.3)
Type "help" for help.
 
postgres=# CREATE USER nginx WITH PASSWORD 'safepass';
CREATE ROLE
postgres=# CREATE DATABASE ourdb;
CREATE DATABASE
postgres=# GRANT ALL PRIVILEGES ON DATABASE ourdb to nginx;
GRANT
postgres=# \q

We’re done here, lets move on!

Open php-fpm’s config file, found here: /etc/php/fpm-php5.3/php-fpm.conf. You want to set the user to nginx, and make note of the port php-fpm will listen for FastCGI connections. Here we see php-fpm will listen on port 9000. (this should be default) Finally, since this server will not take much load we want to lower the amount of processes it starts by default, we will set 5 since 20 (default) is rather a lot!

listen = 127.0.0.1:9000
user = nginx
pm.start_servers = 5

Now open up the nginx config file /etc/nginx/nginx.conf. We need to configure nginx to use php-fpm. Inside the server { tag you will want to add

listen 80;
 
location ~ .php$ {
    fastcgi_pass 127.0.0.1:9000;
}

And inside the http { tag you should add:

include /etc/nginx/fastcgi.conf;
include /etc/nginx/fastcgi_params;
 
index index.php;

I will not cover SSL here. This is only a local dev server, so there is no need for SSL right now. Set the server names and log files as you wish. Make sure the user is nginx.

You should now be ready to start the server. Go ahead and fire them up.

r00ph ~ # /etc/init.d/php-fpm start
 * Starting PHP FastCGI Process Manager ...                                       [ ok ]
r00ph ~ # /etc/init.d/nginx start
 * Checking nginx' configuration ...
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful           [ ok ]
 * Starting nginx ...                                                                          [ ok ]
r00ph ~ #

Lets test it out:

" > /var/www/localhost/htdocs/index.php
# chown nginx:nginx /var/www/localhost/htdocs/index.php

And head over to http://localhost/ (or where ever it is hosted). If everything is okay you should see the phpinfo information! You can check here that PostgreSQL is enabled in the PHP config. (it should be)

Now everything should be working perfectly! Finally you’ll want to use rc-update to get the init script to run automatically on boot.

# rc-update add nginx default
# rc-update add php-fpm default
# rc-update add postgresql-9.0 default

You should be good to go!