Jump to content

How to setup nginx and php on FreeBSD to host your website


Recommended Posts

There instructions have been tested on FreeBSD 12.1!

 

First of all we want to install update the package repository:

pkg update

 

If you have never used pkg probably you will be asked to install it.

 

Now run this command to install php7.4 and nginx

 

pkg install php74 nginx

 

We can install the most common php extensions running this command:

 

 pkg install php74-extensions php74-mysqli php74-mbstring php74-curl php74-gd

 

The following extensions will be installed:

		php74-ctype: 7.4.14
        php74-curl: 7.4.14
        php74-dom: 7.4.14
        php74-extensions: 1.0
        php74-filter: 7.4.14
        php74-gd: 7.4.14
        php74-iconv: 7.4.14
        php74-json: 7.4.14
        php74-mbstring: 7.4.14
        php74-mysqli: 7.4.14
        php74-opcache: 7.4.14
        php74-pdo: 7.4.14
        php74-pdo_sqlite: 7.4.14
        php74-phar: 7.4.14
        php74-posix: 7.4.14
        php74-session: 7.4.14
        php74-simplexml: 7.4.14
        php74-sqlite3: 7.4.14
        php74-tokenizer: 7.4.14
        php74-xml: 7.4.14
        php74-xmlreader: 7.4.14
        php74-xmlwriter: 7.4.14

 

Enable nginx and php service to automatically start

sysrc nginx_enable=yes
sysrc php_fpm_enable=YES

 

We can start nginx and php for the first time by running

service nginx start
service php-fpm start

 

You can open now your web browser and type the server ip in the address bar.

You should see a page like this

spacer.png

 

We are going to configure nginx now. Open the file nginx.conf located in /usr/local/etc/nginx and modify the section "server":

 

    server {
        listen       80;
        server_name  _;
		
		root   /var/www;
        index  index.php index.html index.htm;

        location / {
			try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
			try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $request_filename;
            include        fastcgi_params;
        }
		
        location ~ /\.ht {
            deny  all;
        }
    }

 

Change mydomainname.com with your domain  (or server ip).

 

We can test now if php is working correctly.

 

The files of your webiste will be hosted in the folder /var/www (you can change it in the configuration above). Create a new file inside that folder and call it index.php with this content:

<?php
phpinfo();

 

Now open again the browser and type the ip address/domain name and you should see something like this:

 

spacer.png

 

The server is working properly and you can upload now your script :)

 

You might need to install more php extentions according to the requirements of  your script

  • Love 2
  • Good 2
Link to post
  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



Shoutbox

Shoutbox

Chatroom Rules

 

Join our Discord

A request for help = Shoutbox Ban

Be respectful & Respect the rules

 

×
×
  • Create New...

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.