Setup Your Laravel & Nginx Server with PHP 5.5

Setup Your Laravel & Nginx Server with PHP 5.5

January 28, 2014

In this article Zaher explains how to install Laravel 4.1 with PHP 5.5 & the latest Nginx on Ubuntu 12.04.3 x64. This article assumes that the latest copies of the software required are the ones listed.

1 - Updating your system

apt-get update && apt-get upgrade
adduser [username]
usermod -aG sudo [username]
apt-get -y install git

Now you will have to logout then you need to login using the user you create:

git config --global user.name "your name"
git config --global user.email youremail@serviceprovider.com

 

2 - Installing latest nginx version

sudo -s
nginx=stable
apt-get -y install python-software-properties
add-apt-repository ppa:nginx/$nginx
apt-get update && apt-get upgrade
apt-get -y install nginx
service nginx start
exit

 

3- Installing PHP 5.5

sudo -s
add-apt-repository ppa:ondrej/php5
apt-get update && apt-get upgrade
apt-get -y install php5-fpm php5-mcrypt php5-sqlite sqlite php5-cli php5-xcache php5-curl

Just remember that if you want to install mysql you should also install php5-mysql and any other required module.

4 - Installing Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

 

5 - Installing Laravel

sudo -s
cd /var
mkdir www
chown -R [username]:[username] www
exit
cd www && composer create-project laravel/laravel

We will just change the owner of the storage directory to be the web server, or you can just make it writable for all users. For security reasons, we prefer changing the ownership ..

chown -R www-data:www-data laravel/app/storage

 

6 - Configure Nginx for Laravel

sudo -s
cd /etc/nginx/sites-avaliable
rm default
nano default

Once complete, then you have to paste this and edit it as you need:

# Port that the web server will listen on.
listen          80;

# Host that will serve this project.
server_name     .tipsyandtumbler.dev;

# Useful logs for debug.
access_log      /var/www/laravel/access.log;
error_log       /var/www/laravel/error.log;
rewrite_log     on;

# The location of our projects public directory.
root            /var/www/laravel/public;

# Point index to the Laravel front controller.
index           index.php;

location / {

    # URLs to attempt, including pretty ones.
    try_files   $uri $uri/ /index.php?$query_string;

}

# Remove trailing slash to please routing system.
if (!-d $request_filename) {
    rewrite     ^/(.+)/$ /$1 permanent;
}

# PHP FPM configuration.
location ~* \.php$ {
        fastcgi_pass                    unix:/var/run/php5-fpm.sock;
        fastcgi_index                   index.php;
        fastcgi_split_path_info         ^(.+\.php)(.*)$;
        include                         /etc/nginx/fastcgi_params;
        fastcgi_param                   SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

# We don't need .ht files with nginx.
location ~ /\.ht {
        deny all;
}

# Set header expirations on per-project basis
location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
        expires 365d;

}

 

7 - Final Small Steps & Cleanup:

We like to have a link to my Laravel installation under my home directory so:

ln -s /var/www/laravel www

Finally restart Nginx or you can just reboot your VPS / Server instance:

sudo service nginx restart

 

Troubleshooting

You may get some problem with memory and composer functionality if your using a small VPS with 512 M RAM, so you can easily add a swap partition to your system as the following:

sudo -s
dd if=/dev/zero of=/swapfile bs=1024 count=512k
mkswap /swapfile
swapon /swapfile
exit

This will not retain your swap partition if you reboot your VPS, so we need to edit the fstab file as the following

sudo nano /etc/fstab

and add the following line:

/swapfile       none    swap    sw      0       0  

Swapping in the file should be set to 0. Skipping this step may cause both poor performance, whereas setting it to 0 will cause swap to act as an emergency buffer, preventing out-of-memory crashes.

You can do this with the following commands:

echo 0 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 0 | sudo tee -a /etc/sysctl.conf

To prevent the file from being world-readable, you should set up the correct permissions on the swap file:

sudo chown root:root /swapfile  
sudo chmod 0600 /swapfile