Skip to content

Install Prestashop on FreeBSD 10.2 with Nginx, MySQL and PHP

Background:

One of my friends has a webshop through a proprietary webshop hosting company. Which
on paper seems to be nice. They are really easy to use, stable and very locked down.
My friend where beginning to be in that situation that he wanted more. He wanted to be
able to customize the looks beyond the borders of the proprietary system.
He wanted to be able to do stuff other than the defaults of the proprietary shop. Well he
could do that. They where offering some of those functionality as extra add-on modules,
which when they add up cost where outrageous.
So I talked to him about Prestashop, showed him my demo installation that I played around
with, and he liked what he saw.
We agreed that I should begin making him a shop based on Prestashop, and for that i thought
that the hosting provider one.com, where the go-to guys. But they did not offer a temporary
domainname or subdomain for us to build up the shop before we could make the DNS switch to
the new shop.

What to do. I wanted it to be easy, stable and reliable. I wanted a backend that supported
PHP, MySQL and Nginx. Easy to manage and the cost had to be in the low end. But if demands
where present it should be easy to grow in terms of server power.

My experience with FreeBSD has been really good, so that part would take care of stability
and reliability, the easy part is not present when looking from a none IT person. FreeBSD
is easy to administer from an IT admin perspective, but from an end-user perspective its
really geeky. But for my part I am an IT admin, and stability and reliability is the major
selling point here.
The prestashop back-end is easy to use, so that it what the end-user will see and care
about.
So for server. I thought about hosting this myself. But that really contradicts the stability
and reliability, because I dont want to invest several thousand DKR. on this project.
First a server that is powerfull enough. Well that is actually not that expensive. Then I
need a backup server for that. Then I need a UPS for that. Then I need a internet wire, and a
backup for that too. Plus a decent router or small computer running PFSense or OpnSense.
And then I have to stuff it all someplace where the wife don’t complain. And then when
everything is setup, comes the electrical bill flying through. Wow its really expensive
to have your own datacenter that can sustain a decent uptime.

So what are the alternatives. Getting a rack space in the hosting center nearby? Well that
is really expensive here in Denmark, plus you still need to purchase that nice server
hardware with backup.

I watch BSDNow.tv every week, and they have a sponsor called DigitalOcean which offers
virtual server hosting for really low cost. So I tried to investigate a bit by reading
reviews about DigitalOcean which where rather old. People had trouble with growing the VM,
and retaining the IP address. People had troubles moving the image from hosting center to
hostingcenter and also retaining IP adresses. Plus the support has been not usefull for
many. I dont know if this has changes since the reviews, so please dont take my opinion as pure facts.
But they offered FreeBSD server out of the box. Who else did that?
Well I came around cloudsigma.com, which had FreeBSD 10.2 as one of their images in a KVM
environment. They have a 7 day period of free testing of their server. They have
a datacenter i Europe (Switzerland) so that fit the bill and they have almost same pricepoint as
Digitalocean. I contacted their support due to some troubles with me making some
misunderstanding about how to get the 7 days free trial. Yes, you need to sign up with an email,
to get some a bit nagging sales emails during your testing period. But its not too bad.
So free testing period was the argument that had me signing up for an account, and test
this.

Getting Prestashop to run in FreeBSD with Nginx, PHP (PHP-FPM) and MySQL, was a bit of a
pain. I could have taken the easy way and just installed Apache, but I really like the Nginx
approach and wanted to have experience with that. Many hours of googling later, I now have
gotten it to work as it should. The lack of a fully fledged manual or how-to made me write
this up. Both as a note for myself in the future, but also to help others accomplish the
same or similar task.

So fire up your vanilla FreeBSD 10.2 either in a VPS or selfhosting which doesn’t matter
regarding to this writeup. Installing the FreeBSD is so easy that I will not be covering
that here. If you like it, leave a comment. If you don’t, leave a comment on what i did wrong.
I learn everyday, thats the beauty about life.

Please beaware that the image provided by cloudsigma is UFS based and not ZFS zroot.

Disclaimer: This could burn down your house, kill your children, blow up in your face, etc. etc. Its BSD do what you want, just don’t sue me.. 😉

Installation of prestashop

Login in with root

sudo su

Upgrade the packaging system

pkg upgrade
pkg update

Install the stuff you will need for Prestashop to run with all the PHP modules and Memcache etc.

pkg install nginx php56 mysql56-server memcached pecl-memcache pecl-memcached tmux

pkg install php56-pdo_mysql php56-imap php56-curl php56-extensions php56-mbstring php56-mcrypt php56-gd

Edit: Removed version numbers from pkg install string

This takes care of Nginx, PHP56, MySQL and memcached used for caching in prestashop.
Plus my favourite tool called tmux.
tmux is a alternative to screen which lets you run the terminal session on the host instead
of the client. So running the commands inside tmux allows you to disconnect the server SSH
and later reattach the session and you can go where left of.

tmux

Look up and remember your IP adress to verify everything is working in a webbrowser.

ifconfig

Enable all the services in rc.conf file

nano /etc/rc.conf

Insert the following in the file:

mysql_enable=”YES”
nginx_enable=”YES”
php_fpm_enable=”YES”

Close and save file.

Make changes to php-fpm.conf

nano /usr/local/etc/php-fpm.conf

 

Uncomment:

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

Close and save file.

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

Uncomment the cgi.fix_pathinfo and set to 0

cgi.fix_pathinfo=0

Close and save file.

service php-fpm start

MySQL setup

service mysql-server start

MySQL is not that save to use out of the box so we need to harden it for production use.
Fortunately they provide a script for this.

mysql_secure_installation

Go through all the steps. It will ask you for a root password, which we havent set. So
press enter, when asked for set root password answer yes and type in a secure password.
The rest should be as simple as press enter. When finished restart MySQL-server

service mysql-server restart

Now make a database and a user and grant all permissions to that user.

mysql -u root -p

mysql>create database prestashopdb;
mysql>show databases;
mysql>create user prestashopuser;
mysql>grant all on prestashopdb.* to ‘prestashopuser’@’localhost’ identified by ‘PASSWORD’;
mysql>quit

Nginx Setup.

Now to the fun part. Nginx is really cool compared to Apache. But also somehow more
confusing when you come from Apache. Nginx can have one looong config file or you can
split the config file up into multiple config files for each site you setup on your server.
Here I have chosen to split up the files as a best practices.

cd /usr/local/etc/nginx
mv nginx.conf nginx.conf.old
nano nginx.conf

Your config file should look like this:

###################################################################################

# user defines which user to use. Set this as the same as your php-fpm user.
user www;

# Worker processes are how many CPU’s you have available. 
# Use “sysctl hw.ncpu” to find out.
worker_processes 1;

# Setup log
 error_log /var/log/nginx/error.log info;

events {
 worker_connections 1024;
 }

http {
 include mime.types;
 default_type application/octet-stream;
 access_log /var/log/nginx/access.log;
 sendfile on;
 keepalive_timeout 65;

#Include is the link to the site config for your prestahop.
 include prestashop.conf;
 }

####################################################################################

Pretty easy, right?

Close and save file.

Now make your prestashop config file.

nano prestashop.conf
####################################################################################
### yoursite.com

server {
 access_log off;
 error_log /var/log/nginx/error.log warn;

listen 80;
 #Change server_name to you domainname
 server_name domainname localhost;
 root /usr/local/www/nginx;
 index index.php index.html;

# PrestaShop rewrite rules
 rewrite ^/([a-z0-9]+)-([a-z0-9]+)(-[_a-zA-Z0-9-]*)/([_a-zA-Z0-9-]*).jpg$ /img/p/$1-$2$3.jpg last;
 rewrite ^/([0-9]+)-([0-9]+)/([_a-zA-Z0-9-]*).jpg$ /img/p/$1-$2.jpg last;
 rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)/([_a-zA-Z0-9-]*).jpg$ /img/c/$1$2.jpg last;
 rewrite “^/lang-([a-z]{2})/([a-zA-Z0-9-]*)/([0-9]+)-([a-zA-Z0-9-]*).html(.*)$” /product.php?id_product=$3&isolang=$1$5 last;
 rewrite “^/lang-([a-z]{2})/([0-9]+)-([a-zA-Z0-9-]*).html(.*)$” /product.php?id_product=$2&isolang=$1$4 last;
 rewrite “^/lang-([a-z]{2})/([0-9]+)-([a-zA-Z0-9-]*)(.*)$” /category.php?id_category=$2&isolang=$1 last;
 rewrite ^/([a-zA-Z0-9-]*)/([0-9]+)-([a-zA-Z0-9-]*).html(.*)$ /product.php?id_product=$2$4 last;
 rewrite ^/([0-9]+)-([a-zA-Z0-9-]*).html(.*)$ /product.php?id_product=$1$3 last;
 rewrite ^/([0-9]+)-([a-zA-Z0-9-]*)(.*)$ /category.php?id_category=$1 last;
 rewrite ^/content/([0-9]+)-([a-zA-Z0-9-]*)(.*)$ /cms.php?id_cms=$1 last;
 rewrite ^/([0-9]+)__([a-zA-Z0-9-]*)(.*)$ /supplier.php?id_supplier=$1$3 last;
 rewrite ^/([0-9]+)_([a-zA-Z0-9-]*)(.*)$ /manufacturer.php?id_manufacturer=$1$3 last;
 rewrite “^/lang-([a-z]{2})/(.*)$” /$2?isolang=$1 last;

# static file cache configuration
 location ~* .(gif)$ {
 expires 2592000s;
 }
 location ~* .(jpeg|jpg)$ {
 expires 2592000s;
 }
 location ~* .(png)$ {
 expires 2592000s;
 }
 location ~* .(css)$ {
 expires 604800s;
 }
 location ~* .(js)$ {
 expires 604800s;
 }
 location ~* .(js)$ {
 expires 604800s;
 }
 location ~* .(ico)$ {
 expires 31536000s;
 }

# php-fpm configuration
 location ~ .php$ {
 root /usr/local/www/nginx;
 try_files $uri =404;
 fastcgi_pass unix:/var/run/php-fpm.sock;
 fastcgi_index index.php;

include fastcgi.conf;
 fastcgi_buffer_size 128k;
 fastcgi_buffers 256 4k;
 fastcgi_busy_buffers_size 256k;
 fastcgi_temp_file_write_size 256k;
 }
 }
 ####################################################################################

Close and save file.

Make the fastcgi.conf file needed.

nano fastcgi.conf
####################################################################################
 #fastcgi.conf
 fastcgi_param GATEWAY_INTERFACE CGI/1.1;
 fastcgi_param SERVER_SOFTWARE nginx;
 fastcgi_param QUERY_STRING $query_string;
 fastcgi_param REQUEST_METHOD $request_method;
 fastcgi_param CONTENT_TYPE $content_type;
 fastcgi_param CONTENT_LENGTH $content_length;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 fastcgi_param SCRIPT_NAME $fastcgi_script_name;
 fastcgi_param REQUEST_URI $request_uri;
 fastcgi_param DOCUMENT_URI $document_uri;
 fastcgi_param DOCUMENT_ROOT $document_root;
 fastcgi_param SERVER_PROTOCOL $server_protocol;
 fastcgi_param REMOTE_ADDR $remote_addr;
 fastcgi_param REMOTE_PORT $remote_port;
 fastcgi_param SERVER_ADDR $server_addr;
 fastcgi_param SERVER_PORT $server_port;
 fastcgi_param SERVER_NAME $server_name;
 ####################################################################################

The root folder /usr/local/www/nginx is actually a symlink to /usr/local/www/nginx-dist
remove that and make a new folder.

rm -R /usr/local/www/nginx
mkdir -p /usr/local/www/nginx

Make the log files while we are here.

mkdir -p /var/log/nginx
touch /var/log/nginx/access.log
touch /var/log/nginx/error.log

Use the html file from nginx-dist to verify that everything is working.

cp /usr/local/www/nginx-dist/index.html /usr/local/www/nginx

Make a phpinfo() file also to verify php installation.

nano /usr/local/www/nginx/info.php

Close and save file.

Test the Nginx config files by running:

nginx -t

If succesfull run

service nginx start

Now you should see a nice html page when logging in to your ip address with a webbrowser
and slashing a info.php after that you should also see support for PHP on your site.

Now we need to upload the prestashop to the server. I used SCP because i havent setup a
FTP server on the box.
Make permissions to upload to the nginx folder.

chmod 777 /usr/local/www/nginx

Go to prestashop.com and download the latest version. Mine was prestashop_1.6.1.1.zip
When finished open a new terminal window. And type:

cd “to the location of prestashop .zip file.”
scp prestashop_1.6.1.1.zip USER@IPNUMBER:/usr/local/www/nginx

Go to the server ssh window again.

cd /usr/local/www/nginx
unzip prestashop_1.6.1.1.zip

This will unpack the file so you now have your zip archive a .html file and a folder with
the prestashop files in it. Move those to the root folder and delete the other files.

rm Install_PrestaShop.html
rm prestashop_1.6.1.1.zip
rm index.html
rm info.php
cd prestashop/
mv * ..
cd ..
rm -R prestashop

Set the permissions

cd ..
chown -R www:www nginx
chmod -R 755 nginx

You should now be ready to rock.

Point your webbrowser at your server ip address, and voila you can proceed to setup
prestashop.

Be sure to pick hard and secure passwords all the way. Remember your MySQL database and
user name for the prestashop install.

In the next articles I will show you how to harden your server with appropriate firewall rules. Changing the SSH port. Plus show how to setup a mailserver so that you can have your shopdomain as maildomain on the same box.

Published inBSDServer Services

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.