Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Databases»MySQL»How to Install Nginx, PHP 7 & MySQL on Ubuntu 16.04, 14.04

    How to Install Nginx, PHP 7 & MySQL on Ubuntu 16.04, 14.04

    RahulBy RahulFebruary 5, 20163 Mins ReadUpdated:February 22, 2018

    The latest major release of PHP version 7.2 has been released with the number of speed optimizations and security. So you should try this version for your development. This tutorial will help you to install Nginx, PHP 7.2 & MySQL 5.7 on Ubuntu 17.10, 16.04, and 14.04 LTS release.

    php7-nginx-mysql

    Step 1 – Nginx Installation

    First of all, we will install Latest Nginx web server on our system. Use the following commands to add PPA for installing latest Nginx version on your Ubuntu 14.04 (Trusty).

    echo "deb http://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx" >> /etc/apt/sources.list
    echo "deb-src http://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx"  >> /etc/apt/sources.list
    

    and use the following commands to install Nginx web server.

    curl http://nginx.org/keys/nginx_signing.key | sudo apt-key add -
    sudo apt-get update
    sudo apt-get install nginx
    

    Step 2 – PHP & PHP-FPM Installation

    Install python-software-properties package on your system which provides add-apt-repository command then use the following set of commands to add PPA for PHP with PHP-FPM packages on your Ubuntu system and install it.

    sudo apt-get install python-software-properties
    sudo add-apt-repository ppa:ondrej/php
    sudo apt-get update
    sudo apt-get install -y php7.2 php7.2-fpm
    

    Also install the required php modules for you application.

    sudo apt-get install php7.2-mysql php7.2-curl php7.2-json
    

    Step 3 – MySQL Installation

    Download the MySQL apt configuration Debian package officially provided by MySQL team and install it on your system. For Ubuntu 16.04 and later version’s MySQL 5.7 is available under default apt repositories, so you don’t need to enable additional repository that.

    wget http://repo.mysql.com/mysql-apt-config_0.8.9-1_all.deb
    sudo dpkg -i mysql-apt-config_0.8.9-1_all.deb
    

    Use the following commands to install MySQL server on your Ubuntu 16.04 and 14.04 systems. Currently, this is the most popular version used by the among users.

    sudo apt-get update
    sudo apt-get install mysql-server-5.7
    

    You can find more MySQL installation instructions here.

    Step 4 – PHP-FPM Configuration

    PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features.

    sudo nano /etc/php/7.2/fpm/php.ini
    

    un-comment cgi.fix_pathinfo=1 line and set value to 0.

    cgi.fix_pathinfo=0
    

    Now set the listen parameter in /etc/php/7.2/fpm/pool.d/www.conf configuration file. Here you can use php7.2-fpm socket to work or start php7.2-fpm service on specific port. We are going to use it as service .

    sudo nano /etc/php/7.2/fpm/pool.d/www.conf
    

    Now make changes in the configuration file as below. Commend listen to socket file and enable it as service

    #listen = /run/php/php7.2-fpm.sock
    listen = 127.0.0.1:9000
    

    Step 5 – Nginx VirtualHost Configuration

    Finally do the configuration of Nginx virtualhost. For this example we are editing default configuration file.

    sudo nano /etc/nginx/conf.d/default.conf
    

    and make changes as below.

    server {
            listen   80;
    
            root /var/www;
            index index.php index.html index.htm;
            server_name  example.com www.example.com;
    
            location / {
                    try_files $uri $uri/ /index.html;
            }
    
            error_page 404 /404.html;
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                  root /usr/share/nginx/www;
            }
    
            location ~ .php$ {
                    try_files $uri =404;
                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include fastcgi_params;
            }
    }
    

    You have to do the same changes in all VirtualHosts configured.

    Step 6 – Restart All Services

    After installing and configuring all the services on your system, restart all required services to reload any changes made.

    sudo service nginx restart
    sudo service php7.2-fpm restart
    

    Step 7 – Allow Firewall Access

    If you are using iptables, Use following commands to open port 80 for public access of web server.

    Iptables Users:

    sudo iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
    

    UFW Users:

    $ sudo ufw allow 80/tcp
    

    Step 8 – Test Setup

    Finally, test the installation of PHP 7 with NGINX. Let’s create a file info.php on website document root using following content.

    1
    2
    3
    <?php
    phpinfo();
    ?>

    Now browse this file in web browser. It will so all the details about versions and installation.

    Install PHP 7, NGINX & MySQL

    lemp LEMP Stack MySQL nginx PHP7
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install Java 9 on Debian 9/8 via PPA
    Next Article How to Install PHP 7, NGINX, MySQL on CentOS/RHEL 7.6 & 6.10

    Related Posts

    Backup MySQL Databases to Amazon S3 (Shell Script)

    Updated:May 28, 20222 Mins Read

    How to Install MariaDB on Ubuntu 22.04

    Updated:May 28, 20222 Mins Read

    How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04

    Updated:June 19, 20223 Mins Read

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How To Install MySQL Server on Ubuntu 22.04

    Updated:April 6, 20224 Mins Read

    How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 22.04

    Updated:June 28, 20225 Mins Read

    8 Comments

    1. Andrew on January 13, 2018 1:35 am

      Hi!

      I installed PHP 7.2 +Nginx on Centos 7.x, everything seems to be fine:

      http://47.89.247.154/tst.php

      but, when I try to stop or start or restart php-fpm (root# service php-fpm restart), I always get the following error message:

      “Failed to restart php-fpm.service: Unit not found.”

      Can you please tell me how I can fix this?

      Thanks a lot!

      Andrew

      Reply
      • Rahul K. on January 13, 2018 5:40 am

        Try below command

        systemctl restart php7.2-fpm.service

        Where 7.2 is the active php version…

        Reply
    2. Suzanne on April 22, 2016 10:26 pm

      502 error
      bad gateway

      I did exactly
      and my public folder is /usr/share/nginx/html

      Reply
    3. andy on March 20, 2016 3:11 pm

      install mysql error

      (Reading database … 52863 files and directories currently installed.)
      Removing mysql-server (5.6.27-2+deb.sury.org~trusty+1) …
      Setting up mysql-server-5.6 (5.6.28-0ubuntu0.14.04.1) …
      start: Job failed to start
      invoke-rc.d: initscript mysql, action “start” failed.
      dpkg: error processing package mysql-server-5.6 (–configure):
      subprocess installed post-installation script returned error exit status 1
      Errors were encountered while processing:
      mysql-server-5.6
      E: Sub-process /usr/bin/dpkg returned an error code (1)

      Reply
      • Michael on April 10, 2016 2:08 am

        Ditto. Had to use mysql-server 5.5

        Reply
      • Maneesh on May 6, 2020 8:23 am

        Thanks

        Reply
    4. Gil on February 12, 2016 10:25 am

      Hi,

      Thanks for the tuto.
      @Step 7 : I cannot restart nginx
      ‘Failed to start nginx.service: Unit nginx.service is masked.’

      To fix it I had to
      ‘cd /etc/systemd/system’
      and
      ‘rm nginx.service’
      and reboot the system.

      Reply
      • Paolo on March 29, 2016 2:53 pm

        Gil,
        your comment saved me after hours and hours of searching with no luck. Many thanks for that!

        Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.