Back
Featured image of post Webserver installation on FreeBSD 10.1 (Nginx / PHP / MySQL).

Webserver installation on FreeBSD 10.1 (Nginx / PHP / MySQL).

Webserver installation on FreeBSD 10.1 (Nginx / PHP / MySQL).

Installation

pkg install php56-xml php56-hash php56-gd php56-curl php56-tokenizer php56-zlib php56-zip mysql56-server-5.6.23 nginx

Automatic start

To enable automatic start of services, we edit the rc.conf :

vi /etc/rc.conf

Add these lines :

nginx_enable="YES"
php_fpm_enable="YES"
mysql_enable="YES"

MySQL service configuration

For a safe install, use the build-in script :

mysql_secure_installation

Then we can start the MySQL server :

service mysql-server restart

PHP configuration

As a first step, we will configure php-fpm service :

vi /usr/local/etc/php-fpm.conf

Here are the modifications :

listen = /var/run/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0660

Creation of the php.ini by using the production example :

cp php.ini-production php.ini

We edit this file :

vi php.ini

To change this variable :

cgi.fix_pathinfo=0

Nginx webserver configuration

We edit the configuration file :

vi /usr/local/etc/nginx/nginx.conf

Here is my configuration :

user  www;
worker_processes  4;
error_log /var/log/nginx/error.log info;
events {
    worker_connections  1024;
}
http {
    include            mime.types;
    default_type       application/octet-stream;
    access_log         /var/log/nginx/access.log;
    sendfile           on;
    keepalive_timeout  65;
    server {
        listen 80;
        server_name mondomaine.fr www.mondomaine.fr;
        root /usr/local/www/monrepertoire;
        index index.php index.html index.htm;
        location / {
            try_files $uri $uri/ =404;
        }
        error_page      500 502 503 504  /50x.html;
        location = /50x.html {
            root /usr/local/www/nginx-dist;
        }
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $request_filename;
                include fastcgi_params;
        }
    }
include /usr/local/etc/nginx/vhosts/*;
}

The “worker_processes” parameter must be adjusted according to the number of cores of the server. The following command will give you the information :

sysctl hw.ncpu

Create the directories and files for the logs:

mkdir -p /var/log/nginx
touch /var/log/nginx/access.log
touch /var/log/nginx/error.log

Creation of the folder to store the vhosts configurations :

mkdir vhosts

In this folder, we’ll create one file for each vhost :

vi mydomain.com
server {
	listen  80;
        server_name  mydomain.com foo.mydomain.com;
        location / {
	    root   /usr/local/www/mydomain.com/www/;
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/www/nginx-dist;
        }
        location ~ /\.ht {
            deny  all;
        }
}

We can start Nginx :

service nginx start

Everything should be OK :

Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Starting nginx.
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy