17 C
Texas
angeloma
Senior Writer and partner

How to install Nginx and PHP on FreeBSD?

Hello, friends. FreeBSD is a secure system even more than Linux, that’s why it’s used as the main server. And a server has to be able to be web too, so in this post, you will learn how to install Nginx and PHP on FreeBSD 12.

Nginx and PHP is a very strong combination for a web server. If we add FreeBSD to them then it’s a safe bet that will give us a lot of satisfaction.

To complete this post, you need a root user or one who has sudo access.

So, let’s get started.

Install Nginx and PHP on FreeBSD 12

1.- Install Nginx on FreeBSD 12

- Advertisement -

We have to start with the webserver. So open a terminal or start an SSH session on your server and update the whole system:

sudo pkg update
sudo pkg upgrade

Afterward, install some packages needed to continue the tutorial:

sudo pkg install nano bash curl

Then, install Nginx from the official Nginx repositories. This is the most secure and stable way to proceed.

sudo pkg install nginx
Updating FreeBSD repository catalogue…
FreeBSD repository is up to date.
All repositories are up to date.
The following 2 package(s) will be affected (of 0 checked):
New packages to be INSTALLED:
nginx: 1.18.0_25,2
pcre: 8.44
Number of packages to be installed: 2
The process will require 8 MiB more space.
2 MiB to be downloaded.
Proceed with this action? [y/N]:
1.- Install Nginx on FreeBSD
1.- Install Nginx on FreeBSD

After that, enable the Nginx service:

sudo sysrc nginx_enable=yes
nginx_enable: -> yes

Start it:

sudo service nginx start

And check the status of the service to verify that everything is going well:

sudo service nginx status
nginx is running as pid 882.

Also, you can check the installed version:

nginx -v
nginx version: nginx/1.18.0

2.- Install PHP on FreeBSD

It is now time to install PHP which is available from the official FreeBSD repositories.

So, the whole installation is summarized by running the following command:

sudo pkg install php74
Updating FreeBSD repository catalogue…
FreeBSD repository is up to date.
All repositories are up to date.
The following 4 package(s) will be affected (of 0 checked):
New packages to be INSTALLED:
libargon2: 20190702
libxml2: 2.9.10_1
pcre2: 10.35
php74: 7.4.12
Number of packages to be installed: 4
The process will require 40 MiB more space.
6 MiB to be downloaded.
Proceed with this action? [y/N]:

Also, you can install other modules that are required by web applications. One way to know which modules are available is to run them:

sudo pkg search ^php74-*

And there you can choose which ones to install or not.

3.- Configuring PHP to work with Nginx

Now we have to make some arrangements to make PHP work with Nginx.

First, copy the example configuration file and rename it to the real one.

sudo cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

Edit this file with nano:

sudo nano /usr/local/etc/php.ini

And it leaves the parameter of the following form:

cgi.fix_pathinfo=0

You can use CTRL + W to search within the archive and save time.

Save the changes and close the editor

Now edit the file /usr/local/etc/php-fpm.d/www.conf

sudo nano /usr/local/etc/php-fpm.d/www.conf

And uncomment the following lines:

listen.owner = www
listen.group = www
listen.mode = 0660

And it replaces:

;listen = 127.0.0.1:9000

To:

listen = /var/run/php-fpm.sock;

Again, save the changes and close the file.

Now enable and start php_fpm service

4.- Configuring Nginx

Now it is necessary to edit the default server block configuration file:

sudo nano /usr/local/etc/nginx/example.conf

And add the following

server {
listen 80;
server_name your-server;
root /usr/local/www/nginx-dist;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
2.- Creating a new Serverblock on Nginx
2.- Creating a new Serverblock on Nginx

Remember to change server_name to yours

Save the changes and close the file.

Then, in the main configuration file of Nginx, you have to add the previous file:

sudo nano /usr/local/etc/nginx/nginx.conf

and within the http section add the following:

include example.conf;
3.- Configuring Nginx and PHP on FreeBSD
3.- Configuring Nginx and PHP on FreeBSD

Save the changes and close the file

Then check the syntax of Nginx

sudo nginx -t
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

Then restart Nginx

sudo service nginx reload

5.- Testing Nginx and PHP on FreeBSD

To check the results, just create a PHP file with some code in the

sudo nano /usr/local/www/nginx-dist/info.php

For example, the phpinfo method

<?php
phpinfo();
?>

Save the changes and close the editor.

Now open it from a web browser.

4.- PHP working with Nginx
4.- PHP working with Nginx

So, Nginx and PHP are working on FreeBSD

Conclusion

In this post, you learned how to install Nginx on PHP on FreeBSD quickly and easily. This combination is a guarantee of speed, efficiency, and stability.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article