Linux, Nginx, MySQL, PHP – LEMP Stack for Laravel on Ubuntu

laravel-logoThis quick tutorial will guide to install LEMP stack for Laravel on Ubuntu OS.

Install PHP

  • Install necessary php mods:

    [bash]apt-get install nginx php5-fpm php5-cli php5-mcrypt php5-json php5-dev php-pear git[/bash]

  • Enable mcrypt:

    [bash]ln -s /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available
    php5enmod mcrypt[/bash]

  • Modify /etc/php5/fpm/php.ini and change cgi.fix_pathinfo to 0:


  • Install opcache:

    [bash]pecl install zendopcache-7.0.3
    echo "zend_extension=/usr/lib/php5/20100525/" > /etc/php5/mods-available/opcache.ini
    ln -s /etc/php5/mods-available/opcache.ini /etc/php5/conf.d/20-opcache.ini

    We can also use the following configuration for opcache.ini file (In case your application only uses annotations at development time, you can achieve even better performance by adding opcache.save_comments=0 in your PHP configuration file as well.):


  • Restart php5-fpm:

    [bash]service php5-fpm restart[/bash]

  • Be sure php5-fpm is running at startup:

    [bash]initctl list[/bash]

Intall NGINX

  • Install nginx:

    [bash]apt-get install nginx[/bash]

  • Create /etc/nginx/php5-fpm.conf as follow:

    [bash]location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

  • Remember to include the php5-fpm.conf file in nginx configuration for laravel project. Sample file is as follows:

    [bash]server {
    listen 80 default_server;

    root /home/domainname/public;
    index index.html index.htm index.php;


    #access_log /var/log/nginx/local.laravel-access.log;
    access_log off;
    error_log /var/log/nginx/local.laravel-error.log error;

    charset utf-8;

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|svg|css|zip|tgz|gz|rar|bz2|exe|pdf|doc|xls|ppt|txt|odt|ods|odp|odf|tar|bmp|rtf|js|mp3|avi|mpeg|flv|html|htm|mp4|woff|ttf|eot)$ {
    root /home/domainname/public;
    expires max;

    location / {
    try_files \$uri \$uri/ /index.php?$query_string;

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; }

    error_page 404 /index.php;

    include php5-fpm.conf; # The PHP5-FPM Magic Here

    # Deny .htaccess file access
    location ~ /\.ht {
    deny all;

Install MySQL/MariaDB

  • For Percona server, check Setting up Nginx, HHVM, and Percona for Laravel
  • For MariaDB:
    • First, go to Repository configuration tool to add necessary source list to ubuntu. E.g. for Ubuntu 14.04:

      [bash]sudo apt-get install software-properties-common
      sudo apt-key adv –recv-keys –keyserver hkp:// 0xcbcb082a1bb943db
      sudo add-apt-repository ‘deb trusty main'[/bash]
      Note: If you only input “”, remember to open the TCP_OUT port 11371 or it will not be able to import necessary keys.

    • Install mariadb server, client tools and driver for PHP:

      [bash]sudo apt-get update
      sudo apt-get install mariadb-server mariadb-client php5-mysql[/bash]

    • Done

That’s it. We can work on Laravel right now.

About NhocConan

A super lazy guy who tries to write tech blog entries in English. He is lazy, so he can only write when he is in a good mood or when he is tired of coding.

Leave a comment

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