php7 hosting on Ubuntu Server with Nginx(LEMP stack)

LEMP STACK:

PHP7 is the latest version with a lot of performance improvements and everyone wants to update their servers and code to it. Let's see how to configure one and host your code.

Here we see the combination of ubuntu, Nginx, PHP-fpm, php7, mysql, vsftpd

Configuring Ubuntu server:

The first step is to disable password authentication and enable ssh key based authentication as it will give some protection towards server hacks.

Install Requirements:

sudo apt-get install nginx

sudo apt-get install mysql

sudo apt-get install php7.0-cli php7.0-fpm php7.0-mysql

Copy your project to /usr/share/nginx/www/ directory, of course you can have your project location based on your requirement but remember to specify its location in nginx configuration.

Now that we have all setup lets create nginx configuration.

cd /etc/nginx/site-available/

vi my_project.conf

server {
    listen 80;
    root /usr/share/nginx/www;
    index index.php index.html;
    server_name your_ip_or_domain_name;
    location / {
        try_files $uri $uri/ /index.php;
    }
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php7-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
    }
}

The above configuration specifies that the project is served by port 80, the location / represents the root directory of your project, so any file that you request by specifying after your domain name will be search in / directory, here its your project root directory. The php7-fpm.sock file is genereted by php7-fpm which is used by nginx to communicate with your php project.

Now lets enable this project by linking it with sites-enable direcotry, run:

ln -s /etc/nginx/sites-available/my_project.conf /etc/nginx/sites-enabled/

Test your configuration, run:

nginx -t

if no errors, restart nginx

service nginx restart

By now your server server is deployed with LEMP stack.

Posted On 28 May 2016 By MicroPyramid


Need any Help in your Project?Let's Talk

Latest Comments
Understand Deep Learning and how it works with a simple example

Deep Learning is a part of Machine Learning, it mimics the way our brains fundamentally work. We all know that our brain consists of billions …

Continue Reading...
Letsencrypt wildcard - Setup wildcard subdomain using letsencrypt and certbot

To get wildcard supported certificates, we need to pass the challenge which requires adding TXT records in your dns records.
To get certificates for single domains, …

Continue Reading...
Deploying Wordpress blog with Django using Docker Containers.

Containers run services in an isolated environment, if we deploy our applications using containers then the host system on which these containers are running will …

Continue Reading...
How to setup http password authentication with nginx

HTTP Authentication is used to allow access limit to a site or particular directories by validating the username and password. HTTP basic authentication can also …

Continue Reading...
Gitlab and Docker - Continuous Integration, Deployment and Continuous Delivery

In this blog post, we will learn how to setup and use gitlab container registry to implement, test build and deploy your code automatically with …

Continue Reading...

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties