# Setting-up NGINX for several webapplications



## Anoniem (Oct 4, 2013)

I recently rebuilt my FreeBSD instance and tried to switch from Apache to NGINX. 

Now I followed the following great tutorial:
http://forums.freebsd.org/showthread.php?t=30268

But as mentioned there the servers are being configured from the nginx.conf file.

Now I entered the following in the nginx.conf file:


```
include /etc/nginx/conf.d/*.conf;
```

Created two folders (sites-available and sites-enabled), created two .conf files for both of my websites (Wordpress and OpenCart) and created the symbolic links in sites-enabled.

But it doesn't work like it should (can't get the configuration working). 

Is there / are there any examples for this:

1. What a default nginx.conf should look like
2. How websitename.conf should look (in sites-available) for OpenCart (http://stackoverflow.com/questions/15625928/opencart-on-nginx)
3. How websitename.conf should look (in sites-available) for Wordpress

Any help would be great

My current configuration ->

nginx.conf


```
user  www www;
worker_processes  4;
pid   /var/run/nginx.pid;

events {
    worker_connections  1024;
    use kqueue;
}
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request "'
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
    autoindex off;
    map $scheme $fastcgi_https { ## Detect when HTTPS is used
        default off;
        https on;
    }
 
    keepalive_timeout  10;
 
    #gzip  on;
    #gzip_comp_level 2;
    #gzip_proxied any;
    #gzip_types      text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
 
    # Load config files from the /etc/nginx/conf.d directory
    include /etc/nginx/conf.d/*.conf;
 
}
```

*website 1*


```
server {
    listen 80;
    server_name  URL.com;
    
    root /var/www/opencart;
    index index.php index.html;

    location /image/data {
        autoindex on;
    }
    location /admin {
        index index.php;
    }
    location / {
        try_files $uri @opencart;
    }
    location @opencart {
        rewrite ^/(.+)$ /index.php?_route_=$1 last;
    }
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    # Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS!
    location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
        deny all;
    }
    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
    location ~*  \.(jpg|jpeg|png|gif|css|js|ico)$ {
        expires max;
        log_not_found off;
    }
    location ~ \.php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
```

*website 2*


```
server {
        server_name URL.com;

	access_log   /var/log/nginx/URL.com.access.log;
	error_log    /var/log/nginx/URL.com.error.log;

        root /var/www/wordpress;
        index index.php;

        location / {
                try_files $uri $uri/ /index.php?$args; 
        }

        location ~ \.php$ {
                try_files $uri =404;
                include fastcgi_params;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
}
```

What am I doing wrong?


----------



## Rame (Oct 5, 2013)

From what I can see one of your two websites PHP configuration is broken. But from I can tell the firts one could be the cause.

The fist websites tries to pass any php command to localhost on port 9000


```
include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
```

Firs of all the path where the fastcgi_params file is from a linux distribution, in FreeBSD is should be /usr/local/etc/nginx. So you should edits that line with either include /usr/local/etc/nginx/fastcgi_params; or just include fastcgi_params;.

In the same time the second one passes php commands via unix sockets:

```
include fastcgi_params;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
```


----------



## Rame (Oct 5, 2013)

Sorry for the double post. But I can't edit my posts yet ..

After changing the PHP path in the first website, you will need to look into the /usr/local/etc/php-fpm.conf  and see how php-fpm is listening, either via the default TCP connection listen=127.0.0.1:9000  or via unix socket listen = /var/run/php-fpm.sock.

If php-fpm listens via TCP socket (listen=127.0.0.1:9000) you need to change the second website to reflect this. Change  in the second website from fastcgi_pass unix:/var/run/php5-fpm.sock; to fastcgi_pass 127.0.0.1:9000;

If it listens via unix socket (listen = /var/run/php-fpm.sock) change the first website from fastcgi_pass 127.0.0.1:9000; to unix:/var/run/php5-fpm.sock;.

Personnaly I recomend if both nginx and php-fpm are inside the same machine/jail is ussing unix scokets in php-fpm because it will tend to be a bit fastwer via unix sockets rather than TCP.


----------



## ShelLuser (Oct 5, 2013)

Anoniem said:
			
		

> Is there / are there any examples for this:
> 
> 1. What a default nginx.conf should look like
> 2. How websitename.conf should look (in sites-available) for OpenCart (stackoverflow.com/questions/15625928/opencart-on-nginx)
> 3. How websitename.conf should look (in sites-available) for Wordpress


All these questions are basically answered by going over the default files. On FreeBSD these are located in /usr/local/etc/nginx (as mentioned by a previous poster already) and if you look there you'll find files such as nginx.conf-dist which provides a very specific example for both a default config file but also how you should specify several websites.

Another thing, also mentioned by the previous poster, is that you should be very careful not to mix up the way Linux configures it's software and FreeBSD. Generally speaking a Linux distribution will heavily alter the way a software component gets configured for the sole intent of making the configuration process "fit in" with the distribution. Even if this means going direct against the way the software is normally configured.

Also actually adding stuff to /etc which doesn't involve the base system but a port configuration is in general a very bad idea. Because the moment you need to upgrade your FreeBSD version then there is always a risk that this directory gets whacked simply because it doesn't belong there.

Leave /etc alone and only use it to configure the base system. If you want to extend on something then /usr/local/etc is the place to be.

Hope this can give you some ideas too.


----------



## freebuser (Oct 5, 2013)

In your nginx.conf file you have to include the sites-enabled path

```
include path/to/sites-enabled
```

if didn't work also try

```
include path/to/sites-enabled/site1.conf
```


----------



## Anoniem (Oct 27, 2013)

So, after trying and working out things a bit it does work now. It was indeed an issue with TCP socket. 
I had to set them all on the "unix socket". As for now I have the following config:


```
user  www www;
worker_processes  4;
pid /var/run/nginx.pid;
error_log  /var/log/nginx.error_log  info;


events {
    worker_connections  1024;
    use kqueue;
}
 
http {
    server_names_hash_bucket_size  64;
    include       mime.types;
    include /usr/local/etc/nginx/sites-enabled/*; 
    default_type  application/octet-stream;
 
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request "'
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
    map $scheme $fastcgi_https { ## Detect when HTTPS is used
        default off;
        https on;
    }
 
    keepalive_timeout  65;
 
    gzip  on;
    #gzip_comp_level 2;
    #gzip_proxied any;
    #gzip_types      text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
 
    # Load config files from the /etc/nginx/conf.d directory
    # include /etc/nginx/conf.d/*.conf;

 
}
```

My wordpress-website:


```
server {
        listen 80;
        server_name URL.nl;
        root /usr/local/www/wordpress;
        index index.php;

        #charset koi8-r;

        location / {
           # If requested URI does not match any existing file, directory or symbolic link, rewrite the URL to index.php
           if (!-e $request_filename) {
               rewrite ^ /index.php last;
           }
        }

        # For all PHP requests, pass them on to PHP-FPM via FastCGI
        location ~ \.php$ {
           fastcgi_pass unix:/var/run/php-fpm.sock;
           fastcgi_param SCRIPT_FILENAME /usr/local/www/wordpress$fastcgi_script_name;
           fastcgi_param PATH_INFO $fastcgi_script_name;
           include fastcgi_params; # include extra FCGI params
        }
}
```

And my Opencart-website:


```
# FORCE WWW
server {
    server_name  URL.com;
    rewrite ^(.*) http://www.URL.com$1 permanent;
}
# MAIN SERVER
server {
    server_name  URL.com;
    listen 80;
    root /usr/local/www/opencart;
    index index.php index.html;

    location /image/data {
        autoindex on;
    }
    location /admin {
        index index.php;
    }
    location / {
        try_files $uri @opencart;
    }
    location @opencart {
        rewrite ^/(.+)$ /index.php?_route_=$1 last;
    }
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    # Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS!
    location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
        deny all;
    }
    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
    location ~*  \.(jpg|jpeg|png|gif|css|js|ico)$ {
        expires max;
        log_not_found off;
    }
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        include /usr/local/etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /usr/local/www/opencart$fastcgi_script_name;
    }
}
```

There's a but, now when I enter my Wordpress website I looks like everything is redirected to my Opencart-website.
Did I make a strange mistake somewhere here? Except from an error opening my Opencart-website (created a ticket at Opencart) the /admin is working like a charm - so I suppose it has to be something in the redirection- or PHP.


----------



## junovitch@ (Oct 28, 2013)

Since you are rewriting URL.com to be http://www.URL.com, shouldn't your second server block say server_name http://www.URL.com?


----------

