Wiki

Clone wiki

Linux / How_To_Install_And_Configure_Nginx_+php-fpm_+mysql5.7_Ubuntu16.04

Requirement

  • One Ubuntu16.04 Linux Box

  • Server Name = Web01

  • Server IP = 192.168.138.132

#!html

root@web01:~# uname -a
Linux web01 4.4.0-21-generic #37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
root@web01:~#

Installation Step One

  • First Install Nginx web server
#!html

root@web01:~# apt install nginx
  • Once installation complete start nginx server using below command
#!html

root@web01:~# systemctl start nginx
  • Enable Nginx server to start at boot using below command
#!html

root@web01:~# systemctl enable nginx
  • Test Nginx Web Server, Go to browser and hit Web server IP -

web01.PNG

Installation Step Two

  • Install MySql-Server run command apt install mysql-server
#!html

root@web01:~# apt install mysql-server


Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libfcgi-perl libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl
  libio-html-perl liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server-5.7 mysql-server-core-5.7
Suggested packages:
  libdata-dump-perl libipc-sharedcache-perl libwww-perl mailx tinyca
The following NEW packages will be installed:
  libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libfcgi-perl libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl
  libio-html-perl liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server mysql-server-5.7 mysql-server-core-5.7
0 upgraded, 20 newly installed, 0 to remove and 113 not upgraded.
Need to get 18.8 MB of archives.
After this operation, 162 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
  • Once Package downloading completed it will start installation automatically

  • during installation it will prompt you to set the MySql root password

mysqlpasswd.PNG

  • Retype Password once prompt

mysqlpasswdretype.PNG

*Now Installation get completed and it drop you to root shell

#!html

Setting up libaio1:amd64 (0.3.110-2) ...
Setting up mysql-client-core-5.7 (5.7.13-0ubuntu0.16.04.2) ...
Setting up mysql-client-5.7 (5.7.13-0ubuntu0.16.04.2) ...
Setting up mysql-server-core-5.7 (5.7.13-0ubuntu0.16.04.2) ...
Setting up mysql-server-5.7 (5.7.13-0ubuntu0.16.04.2) ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Renaming removed key_buffer and myisam-recover options (if present)
Setting up libhtml-tagset-perl (3.20-2) ...
Setting up liburi-perl (1.71-1) ...
Setting up libhtml-parser-perl (3.72-1) ...
Setting up libcgi-pm-perl (4.26-1) ...
Setting up libfcgi-perl (0.77-1build1) ...
Setting up libcgi-fast-perl (1:2.10-1) ...
Setting up libencode-locale-perl (1.05-1) ...
Setting up libhtml-template-perl (2.95-2) ...
Setting up libtimedate-perl (2.3000-2) ...
Setting up libhttp-date-perl (6.02-1) ...
Setting up libio-html-perl (1.001-1) ...
Setting up liblwp-mediatypes-perl (6.02-1) ...
Setting up libhttp-message-perl (6.11-1) ...
Setting up mysql-server (5.7.13-0ubuntu0.16.04.2) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu4) ...
root@web01:~#
  • Enable MySql server to start at boot time
#!html

root@web01:~# systemctl enable mysql
Synchronizing state of mysql.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable mysql
root@web01:~#

Installation Step Three

*Install PHP PHP-FPM

#!html

root@web01:~# apt install php-fpm

#!html

root@web01:~# apt install php-fpm



Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  php-common php7.0-cli php7.0-common php7.0-fpm php7.0-json php7.0-opcache php7.0-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  php-common php-fpm php7.0-cli php7.0-common php7.0-fpm php7.0-json php7.0-opcache php7.0-readline
0 upgraded, 8 newly installed, 0 to remove and 45 not upgraded.
Need to get 3503 kB of archives.
After this operation, 14.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Creating config file /etc/php/7.0/mods-available/tokenizer.ini with new version
Setting up php7.0-json (7.0.8-0ubuntu0.16.04.1) ...

Creating config file /etc/php/7.0/mods-available/json.ini with new version
Setting up php7.0-opcache (7.0.8-0ubuntu0.16.04.1) ...

Creating config file /etc/php/7.0/mods-available/opcache.ini with new version
Setting up php7.0-readline (7.0.8-0ubuntu0.16.04.1) ...

Creating config file /etc/php/7.0/mods-available/readline.ini with new version
Setting up php7.0-cli (7.0.8-0ubuntu0.16.04.1) ...
update-alternatives: using /usr/bin/php7.0 to provide /usr/bin/php (php) in auto mode
update-alternatives: using /usr/bin/phar7.0 to provide /usr/bin/phar (phar) in auto mode
update-alternatives: using /usr/bin/phar.phar7.0 to provide /usr/bin/phar.phar (phar.phar) in auto mode

Creating config file /etc/php/7.0/cli/php.ini with new version
Setting up php7.0-fpm (7.0.8-0ubuntu0.16.04.1) ...

Creating config file /etc/php/7.0/fpm/php.ini with new version
Setting up php-fpm (1:7.0+35ubuntu6) ...
Processing triggers for systemd (229-4ubuntu6) ...
Processing triggers for ureadahead (0.100.0-19) ...
root@web01:~#
  • Now open php.ini file and find line "cgi.fix_pathinfo=1" Change it as below
#!html

root@web01:~# vi /etc/php/7.0/fpm/php.ini
#!html

cgi.fix_pathinfo=0
  • Now restart php7.0-fpm service
#!html

root@web01:~# systemctl restart php7.0-fpm

Installation Step Four

  • Configure php7.0-fpm in Nginx default Server Message Block

#!html

root@web01:~# vi /etc/nginx/sites-available/default
* Uncomment below mentioned line

#!html

location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
* Save and Exit from File

*Now Create phpinfo file to test PHP script

#!html

root@web01:~# vi /var/www/html/phpinfo.php
  • Copy and Paste below code

#!html

<?php
 phpinfo();
* Save and Exit

  • Now open Browser and open URL - http://192.168.138.132/phpinfo.php

  • You will get below output if everything goes well

php.PNG

Everything went well congratulation

  • To check Service Status Use below Command
#!html

root@web01:~# systemctl status nginx


 nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2016-07-23 11:55:17 IST; 13min ago
  Process: 9632 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 9965 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 9964 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 9966 (nginx)
    Tasks: 2
   Memory: 3.4M
      CPU: 133ms
   CGroup: /system.slice/nginx.service
           ├─9966 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           └─9967 nginx: worker process

Jul 23 11:55:16 web01 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jul 23 11:55:17 web01 systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
Jul 23 11:55:17 web01 systemd[1]: Started A high performance web server and a reverse proxy server.
root@web01:~#
#!html

root@web01:~# systemctl status mysql


 mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2016-07-23 08:18:13 IST; 3h 50min ago
 Main PID: 2656 (mysqld)
    Tasks: 28
   Memory: 100.4M
      CPU: 7.695s
   CGroup: /system.slice/mysql.service
           └─2656 /usr/sbin/mysqld

Jul 23 08:18:12 web01 systemd[1]: Starting MySQL Community Server...
Jul 23 08:18:13 web01 systemd[1]: Started MySQL Community Server.
root@web01:~#
#!html

root@web01:~# systemctl status php7.0-fpm

 php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2016-07-23 11:50:05 IST; 19min ago
  Process: 9605 ExecStartPre=/usr/lib/php/php7.0-fpm-checkconf (code=exited, status=0/SUCCESS)
 Main PID: 9612 (php-fpm7.0)
   Status: "Processes active: 0, idle: 2, Requests: 1, slow: 0, Traffic: 0req/sec"
    Tasks: 3
   Memory: 13.0M
      CPU: 471ms
   CGroup: /system.slice/php7.0-fpm.service
           ├─9612 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
           ├─9613 php-fpm: pool www
           └─9614 php-fpm: pool www

Jul 23 11:50:04 web01 systemd[1]: Starting The PHP 7.0 FastCGI Process Manager...
Jul 23 11:50:05 web01 systemd[1]: Started The PHP 7.0 FastCGI Process Manager.
root@web01:~#

That’s it. Your LEMP server is ready to use.

Cheers!

Updated