Installing Nginx, MySQL 5.6, PHP 5.6 on FreeBSD 10.3

This article explains how to install the FEMP (FreeBSD, Nginx, MySQL, PHP) stack using FreeBSD 10.3 as your base system. Please start with a freshly-built system to avoid bugs and conflicts.

1. Preliminary steps

Perform an update on your system by using the following commands:

freebsd-update fetch
freebsd-update install

2. Install and configure Nginx

You may install Nginx from either pkg or the ports tree; the ports tree will most likely contain a different (and possibly newer) version of Nginx so this is your choice.

2a. Installing Nginx using pkg

To install from pkg, first search pkg to find the available versions:

pkg search nginx

If this is your first time running the pkg command, you will be prompted to install the pkg tool. Enter "y" to continue.

You will get some results like:

nginx-1.8.1,2                  Robust and small WWW server

If you want to install Nginx from pkg, run the following command using the version from pkg search:

pkg install nginx-1.8.1,2

When prompted, enter "y" to continue with the installation.

Once the installation is complete, edit /etc/rc.conf and add the following line to start Nginx on reboot:

nginx_enable="YES"

Now issue the following command to start the Nginx http server:

service nginx start

Navigate to your server's IP address in your browser and you should see the default "Welcome to nginx!" page.

2b. Installing Nginx using the ports tree

Alternatively, you may install Nginx from the ports tree. If you haven't set up the ports tree yet, you will need to do so at this time. Instructions for setting up the ports tree may be found in FreeBSD's ports tree guide.

Navigate to the Nginx directory in the ports tree:

cd /usr/ports/www/nginx

Use the following command from within the /usr/ports/www/nginx directory to install Nginx:

make install clean

This will also install several other packages. During the installation process you will be presented with configuration options for these packages. Defaults are fine for most installations.

Once the installation is complete, edit /etc/rc.conf and add the following line to start Nginx on reboot:

nginx_enable="YES"

Now issue the following command to start the Nginx http server:

service nginx start

Navigate to your server's IP address in your browser and you should see the default "Welcome to nginx!" page.

2c. Configure Nginx using basic options

Create a directory in /var/log to store your Nginx logs if it doesn't already exist:

mkdir /var/log/nginx

Now edit /usr/local/etc/nginx/nginx.conf and change all instances of logs/error.log to /var/log/nginx/error.log, all instances of logs/access.log to /var/log/access.log, and all instances of logs/host.access.log to /var/log/host.access.log . These changes keep your log files in a central location (/var/log) so they can be easily found and accessed later.

This step is not critical, however, and you may keep your Nginx logs in the default location at /usr/local/etc/nginx/logs if you choose.

Uncomment the log lines so Nginx will store logs, whether you've chosen to keep them in the default location or in /var/log/nginx .

We'll leave the remainder of /usr/local/etc/nginx/nginx.conf alone for now and move on to installing MySQL 5.6.

3. Install and configure MySQL 5.6

The next step to install is MySQL 5.6. Do that now by running the following command:

pkg install mysql56-server

When asked to proceed with installing mysql56-server (and a few other packages, including mysql56-client), enter "y" here.

Edit /etc/rc.conf again and add the following line to start MySQL on reboot:

mysql_enable="YES"

Now you may start MySQL with:

service mysql-server start

At this point you will need to make some security modifications to your MySQL installation, particularly setting a root password and removing test and anonymous data. Do that with the following command:

mysql_secure_installation
  1. Press "Enter" at Enter current password for root (enter for none)
  2. Enter "y" at Set root password? [Y/n]
  3. Now enter a new secure password for the root MySQL user
  4. At Remove anonymous users? [Y/n], enter "y"
  5. Enter "y" at Disallow root login remotely? [Y/n]
  6. Remove the test database by entering "y" at Remove test database and access to it? [Y/n]
  7. Reload the privilege tables so your changes take effect by entering "y" at Reload privilege tables now? [Y/n]

Try accessing your MySQL databases now on the command line by using:

mysql -u root -p

That command should drop you into the MySQL shell. Congratulations, you've successfully installed MySQL 5.6 on FreeBSD 10.3! Now it's time to install PHP 5.6.

4. Install and configure PHP 5.6

Again, we will use the pkg tool to install PHP 5.6. Do so with the following command:

pkg install php56 php56-mysql php56-mysqli

Enter "y" when asked to proceed with installing the php56 packages.

Copy the sample php.ini file to php.ini:

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

Now we need to further customize /usr/local/etc/nginx/nginx.conf and set up php-fpm.

First, set up php-fpm correctly by editing /usr/local/etc/php-fpm.conf and making the following changes:

Replace:

listen = 127.0.0.1:9000

with:

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

Uncomment the following lines (remove the semicolons):

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

Next, edit /usr/local/etc/nginx/nginx.conf again. Locate the following block (inside the http > server block) and change it from:

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

to (comment it out):

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

Now add the following block under it:

location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include fastcgi_params;
}

Locate the following lines inside the http > server block:

listen       80;
server_name  localhost;

Add the following lines under them:

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

Restart Nginx:

service nginx restart

Edit /etc/rc.conf once again and add the following line:

php_fpm_enable="YES"

Then use the following command to start php-fpm:

service php-fpm start

Navigate to /usr/local/www/nginx and create a file called "index.php." Paste in the code below:

<?php
echo "Hello world!";
?>

Save and exit the file.

Navigate to your server's IP address in your browser and you should see the text "Hello world!"

That's it! You now have a fully functioning FEMP server ready to host your website.

Instantly deploy a BSD cloud VPS with RootBSD

deploy instantly

  • Fast SSD hosting
  • Full root access
  • IPv6 and IPv4 enabled hosting
  • Over 25 data center locations worldwide
  • Friendly and knowledgeable support
  • Build with current FreeBSD, OpenBSD, NetBSD, Linux releases