Jump to content
×
×
  • Create New...
  • 0

How to set up a webserver with nginx and php-fpm


Shogun

Question

  • Premium

Today I will explain you how to set up the nginx webserver in FreeBSD. While Apache has a long tradition, it has been overtaken performance wise by newer, more robust software like nginx, as can be seen in this comparisongraph:

 

nginx-apache-reqs-sec.png

 

Setting up is even simpler than Apache. First we will build the nginx binaries. We have 2 choices, the stable version, and the development or beta version ("nginx-devel"). We will install the stable version in this tutorial. The first thing is downloading the FreeBSD ports collection to our server:

portsnap fetch extract
Or if we already ran this command earlier we can just update the ports that have been changed with:

portsnap fetch update
 

Then we navigate to the folder and build nginx:

 

cd /usr/ports/www/nginx
make config install clean
 

Make sure PHP-FPM and CLI versions are marked here. You can use up/down arrow and space to navigate through the list of options. When we are done, all we have to do is just press enter and wait for the package to be compiled, which may take some minutes.

 

If you want nginx to be started automatically on reboot, add the following lines to /etc/rc.conf:

nginx_enable="YES"
Next we need to configure nginx. The configuration file is found in /usr/local/etc/nginx/nginx.conf and you can overwrite the default with the one I'm providing next:

user  www;
worker_processes  1;

events {
    	worker_connections  1024;
}

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

	server {
		listen       80;
		server_name  mydomain.com;
		root /usr/local/www/nginx;

		index  index.php;

		location @missing {
			rewrite ^ $scheme://$host/index.php permanent;
		}

		location ~*  .(jpg|jpeg|png|gif|ico|css|js)$ {
			expires 7d;
		}
		
		location ~ .php$ {
			fastcgi_pass   127.0.0.1:9000;
			fastcgi_index  index.php;
			fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
			fastcgi_buffer_size 128k;
			fastcgi_buffers 256 16k;
			fastcgi_busy_buffers_size 256k;
			fastcgi_temp_file_write_size 256k;
			include        fastcgi_params;
		}

		location ~ /. {
			deny all;
		}

	}
}
The only thing you need to change here (in principle) is the ServerName directive you must enter your domain name here instead of mydomain.com

 

Now we are ready to serve pages, but since we will not be serving a static website, we need also php-fpm. We will be installing version 5.5 of the PHP language which is the latest available at this moment in the ports.

pkg install lang/php55
Besides php, we will most likely need some extensions for our website. We already updated our ports collection earlier so we will just navigate to the php55-extensions folder:

cd /usr/ports/lang/php55-extensions
make config install clean
We will be shown a long list of modules we can install. For example, we will be most likely using MySQL, so mark this module (SPACE key). PDO, MYSQLi and CURL are also used quite often.

 

8rJhF.png

 

After we chose our desired modules, we can press enter and wait for all the modules to build. This can take quite a while!

 

Finally, we set PHP-FPM to be started on reboot as well adding the following line to rc.conf

php_fpm_enable="YES"
That's all! Now it's time to upload our website to the /usr/local/www/nginx folder and start our webserver:

service nginx start
service php-fpm start
If there is anything wrong, we can check the access and error logs:

tail -f /var/log/nginx-error.log
  • Love 10

 

 

Link to comment

11 answers to this question

Recommended Posts

  • 0

You forgot a lot of things. On a fresh and new system there is no pkg with the name "dialog4ports" so there can't be "a list of options".

pkg_add -r php55

This will not longer work. So install this from ports is correct. (cd /usr/ports/lang/php55 && make install clean)

cd /usr/ports/devel/php55-extensions

is also not correct. The extensions are placed in /usr/ports/lang/php55-extensions.

You should try to follow your guide with a fresh system. (maybe with a VM?) However, it's no offense.

 

I am using nginx for a long time so i can say it's really better than apache. The graphs are telling the rest.

Link to comment
  • 0
  • Premium

You forgot a lot of things. On a fresh and new system there is no pkg with the name "dialog4ports" so there can't be "a list of options".

pkg_add -r php55

This will not longer work. So install this from ports is correct. (cd /usr/ports/lang/php55 && make install clean)

cd /usr/ports/devel/php55-extensions

is also not correct. The extensions are placed in /usr/ports/lang/php55-extensions.

You should try to follow your guide with a fresh system. (maybe with a VM?) However, it's no offense.

 

I am using nginx for a long time so i can say it's really better than apache. The graphs are telling the rest.

 

Thansks for pointing out the error in the extensions path. But I would say that dialog4ports stuff is installed automatically.

 

 

Link to comment
  • 0
  • Premium

Where is the main Directory to view webpages?

 

In nginx.conf:

 

root /usr/local/www/nginx;

 

But you can change this to whatever you want. Just make sure it's readable by the www user.

 

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.