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:

    [bash]cgi.fix_pathinfo=0[/bash]

  • Install opcache:

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

    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.):

    [bash]zend_extension=/usr/lib/php5/20100525/opcache.so
    opcache.memory_consumption=128
    opcache.interned_strings_buffer=8
    opcache.max_accelerated_files=4000
    opcache.revalidate_freq=60
    opcache.fast_shutdown=1
    opcache.enable_cli=1[/bash]

  • 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;
    }[/bash]

  • 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;

    server_name domainname.com www.domainname.com;

    #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;
    }
    }[/bash]

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://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
      sudo add-apt-repository ‘deb http://ftp.yz.yamagata-u.ac.jp/pub/dbms/mariadb/repo/5.5/ubuntu trusty main'[/bash]
      Note: If you only input “keyserver.ubuntu.com”, 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.

Leave a comment

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