[FreeBSD] Nginx Setup - BSD

Users browsing this thread: 1 Guest(s)
yrmt
Grey Hair Nixers
Web Server
¯¯¯¯¯¯¯¯¯¯¯¯¯
Now that you've got your jail configured, let's install the Web Server !

You can achieve that either by the ports or the precompiled packages.
Personally, I prefer using the ports, so in /usr:

PHP Code:
-> portsnap fetch extract 

When it's finished, install nginx:

PHP Code:
-> /usr/ports/www/nginx 

or: pkg_add -r -v nginx

Select the options you need.

You can add this in the jail's /etc/rc.conf: nginx_enable="YES"

Or, run it directly with: /usr/local/etc/rc.d/nginx start. Test the nginx is running by going on 192.168.1.10
in your browser. It should say: "Welcome to nginx !"

Nginx's default root directory is in /usr/local/www/nginx and Nginx's configuration is /usr/local/etc/nginx/nginx.conf

Install PHP5, the extensions, spawn-fcgi and mysql

PHP Code:
-> /usr/ports/lang/php5
 
-> /usr/ports/lang/php5-extensions
 
-> /usr/ports/www/spawn-fcgi
 
-> /usr/ports/databases/mysql55-server 

Again, you can use pkg_add if you need this to be done quickly.

Now add these lines in te jail's rc.conf:

PHP Code:
-> mysql_enable="YES"
 
-> nginx_enable="YES"
 
-> spawn_fcgi_enable="YES" 

Nginx's config file needs to be edited, you can use mine:

PHP Code:
user  www;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid  logs/nginx.pid;

events {
    
worker_connections  1024;
}

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  logs/access.log  main;

    
sendfile  on;
    
#tcp_nopush  on;

    #keepalive_timeout  0;
    
keepalive_timeout  65;

    
#gzip  on;

    
server {
  
listen  80;
  
server_name  localhost;

  
#charset koi8-r;

  #access_log  logs/host.access.log  main;

  
location / {
    
root   /usr/local/www/nginx;
    
index  index.php index.html index.htm;
  }

  
#error_page  404    /404.html;

  # redirect server error pages to the static page /50x.html
  #
  
error_page   500 502 503 504  /50x.html;
  
location = /50x.html {
    
root   /usr/local/www/nginx-dist;
  }

  
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #    proxy_pass   [url]http://127.0.0.1[/url];
  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  
location ~ \.php$ {
    
root  html;
    
fastcgi_pass   127.0.0.1:9000;
    
fastcgi_index  index.php;
    
fastcgi_param  SCRIPT_FILENAME  /usr/local/www/nginx$fastcgi_script_name;
    include  
fastcgi_params;
  }

  
# deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #    deny  all;
  #}
    
}

    
# another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen  8000;
    #    listen  somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #  root   html;
    #  index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen  443;
    #    server_name  localhost;

    #    ssl    on;
    #    ssl_certificate  cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #  root   html;
    #  index  index.html index.htm;
    #    }
    #}



Once everything is done, restart the jail and test that Nginx works.

Let's test if PHP works ! Write a little php file in Nginx's root directory.

/usr/local/www/nginx/phpinfo.php

the php code:

PHP Code:
<?php  
   phpinfo
();  
   
?>


Run 192.168.1.10/phpinfo.php in your browser, if it shows something, you have php working.
This might also be a good time to test that you have the options you need with php.

If it doesn't work, try running this in the jail:

PHP Code:
-> /usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -g www -C 4 -/usr/local/bin/php-cgi
 
-> /usr/local/sbin/nginx -/usr/local/etc/nginx/nginx.conf 

Now that PHP works, let's take care of mysql. I like to install phpmyadmin to manage sql databases.
But you need to create a mysql account first.

-> make a user account in your jail by running adduser.
-> run mysql as root in your jail.

To make a mysql user with all privileges, run this:
( my username is beastie, replace some_pass with the password you want )

PHP Code:
mysqlGRANT ALL PRIVILEGES ON *.* TO 'beastie'@'localhost'
    
->  IDENTIFIED BY 'some_pass' WITH GRANT OPTION;
mysqlGRANT ALL PRIVILEGES ON *.* TO 'beastie'@'%'
    
->  IDENTIFIED BY 'some_pass' WITH GRANT OPTION;
mysqlGRANT RELOAD,PROCESS ON *.* TO 'admin'@'localhost';
mysqlGRANT USAGE ON *.* TO 'dummy'@'localhost'

The user is created. Now let's install phpmyadmin, you need to fetch it first. You can also install it by ports.

Install wget or curl before, it can be useful: ( copy paste, heh :P )

PHP Code:
-> /usr/ports/ftp/wget

 
-> wget http://sourceforge.net/projects/phpmyadmin/files/phpMyAdmin/3.5.1/phpMyAdmin-3.5.1-all-languages.tar.gz/download#!md5!28c3c913785ae201af269089b699141a; mv download pma.tar.gz; tar -xvzf pma.tar.gz; mv phpMyAdmin-3.5.1-all-languages pma

Move the "pma" folder in Nginx's root directory. In my case, /usr/local/www/nginx. 

Access 192.168.1.10/pma is a browser and use your new user and pass from mysql. If everything works you should be logged in
and able to make new databases and tables...

You might get a few warnings from phpmyadmin, just recompile php5-extensions with the appropriate options.

If you want, you may now allow incoming port 80 from your router and forward port 80 to 192.168.1.10 ;), register a hostname
and bam, you have a home server \o/.


Messages In This Thread
[FreeBSD] Nginx Setup - by yrmt - 22-07-2012, 07:10 PM