Brief guide to install Nginx, PHP, Mysql on Ubuntu 17

Brief guide to install Nginx, PHP, Mysql on Ubuntu 17

Introductions

Nginx is a light Web / Reverse Proxy / Mail Proxy server .
PHP is a popular server-side scripting language and quite a few applications such as blog , forum applications are written in php .
MySQL software delivers a very fast, multi-threaded, multi-user, and robust SQL (Structured Query Language) database server.

Preparations

Ubuntu 17.10 server and a user with root privileges ( You can either 'sudo' or 'su -' )
You may need the upgration of the installed packages .
Run:

 apt-get update
 apt-get upgrade

Start installing

Let's start with installing the Nginx server !

Install Nginx

By default Nginx is in the package list .
If not , run :

  wget http://nginx.org/keys/nginx_signing.key
  apt-key add nginx_signing.key
  echo "deb http://nginx.org/packages/ubuntu/ trusty nginx" >> /etc/apt/sources.list
  echo "deb-src http://nginx.org/packages/ubuntu/ trusty nginx" >> /etc/apt/sources.list
  apt-get update

Then , run:

  apt-get install nginx -y

A few minutes later , simply open your browser and type in your server-address , you will see the Nginx-default page .

P.S You can run this command to see the version of Nginx :

  nginx -v

It will get you the output like this :

  nginx version: nginx/1.12.1 (Ubuntu)

Install MySQL

By default Mysql is in the package list .
If not , run :

  add-apt-repository -y ppa:ondrej/mysql-5.6

Then , run :

  apt-get install mysql-server -y

Start MySQL

   systemctl start mysql

Run this command to see the version

   mysql -v

Install php7.0

Add ppa , run :

  apt-get install python-software-properties software-properties-common
  add-apt-repository ppa:ondrej/php
  apt-get update

Install php7.0 and some needed extensions , run :

  apt-get install php7.0-fpm php7.0-mysql php7.0-common php7.0-curl php7.0-cli php7.0-mcrypt php7.0-mbstring php7.0-dom

Making configurations

Do a little configuration to php7.0 to avoid safety problems

   vi /etc/php/7.0/fpm/php.ini

Find the line as below :

   ;cgi.fix_pathinfo = 1;

Replace with this :

   cgi.fix_pathinfo = 0;

Save the changes and quit .
Start php-fpm

   systemctl start php7.0-fpm

Make php7.0 environment works in Nginx

Change to the site configuration folder :

   cd /etc/nginx/sites-available
   ls

And you will see all the sites that are available . ( By default , there is only the site 'default' )
Let's create a site called tblog . :)

   cp default tblog

Now make changes to the config file .

   vi tblog

Fild the php config block as below :

   # pass PHP scripts to FastCGI server
   #
   #location ~ \.php$ {
   #    include snippets/fastcgi-php.conf;
   #
   #    # With php-fpm (or other unix sockets):
   #    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
   #    # With php-cgi (or other tcp sockets):
   #    fastcgi_pass 127.0.0.1:9000;
   #}

Replace with this :

   # pass PHP scripts to FastCGI server
   #
   location ~ \.php$ {
   include snippets/fastcgi-php.conf;

   # With php-fpm (or other unix sockets):
   fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
   # With php-cgi (or other tcp sockets):
   #fastcgi_pass 127.0.0.1:9000;
   }

Then , change the document root ( where you store the site files ) and make 'index.php' another index file , find this part :

   root /var/www/html;
   # Add index.php to the list if you are using PHP
   index index.html index.htm index.nginx-debian.html;
   server_name _;

Make it like this :

    root /var/www/tblog;# Where to places the contents ?
    index index.html index.htm index.nginx-debian.html;# Add index.php here .
    server_name twic.me;# Here you should place the domain name .

Then , disable the 'default' site and enable the 'tblog' site :

   rm /etc/nginx/sites-enabled/default
   ln-s /etc/nginx/sites-available/tblog /etc/nginx/sites-enabled

Create a simple php test file in the document root :

   touch /var/www/tblog/index.php
   vi /var/www/tblog/index.php

Add some php code , for example :

   <?php
   echo "Hello world!";

Restart Nginx

   systemctl restart nginx.service

At last

Enable the services to boot with the system

systemctl enable nginx
systemctl enable mysql
systemctl enable php7.0-fpm

That's all done .