LEMP Stack – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Tue, 11 Oct 2022 17:29:33 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04 https://tecadmin.net/how-to-install-lemp-stack-on-ubuntu-22-04/ https://tecadmin.net/how-to-install-lemp-stack-on-ubuntu-22-04/#comments Thu, 07 Apr 2022 01:31:43 +0000 https://tecadmin.net/?p=9770 The Linux operating system is a very popular and widely used OS for the server. It powers the majority of the world’s websites, including some of the most well-known ones such as Yahoo, Google, and Facebook. The logical acronym LAMP is commonly used to refer to the mixture of free and open-source software that is [...]

The post How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04 appeared first on TecAdmin.

]]>
The Linux operating system is a very popular and widely used OS for the server. It powers the majority of the world’s websites, including some of the most well-known ones such as Yahoo, Google, and Facebook. The logical acronym LAMP is commonly used to refer to the mixture of free and open-source software that is frequently used together to create a server architecture that can handle dynamic websites, such as those built on PHP, MySQL, and Apache.

Each letter in the acronym refers to a separate software package: That being said, let’s see how we can install and setup LEMP Stack on Ubuntu.

Pre-Requisities

Assuming that you have a running Ubuntu 22.04 Linux system with sudo (or root) privileged access.

Access your system and open a terminal. It will be good to update the package manager cache and upgrade currently installed packages. To do this execute:

sudo apt update && sudo apt upgrade 

Let’s begin the LEMP (Linux, Nginx, MySQL, and PHP) stack installation on Ubuntu 22.04 Jammy Jellyfish Linux system.

Step 1 – Installing NGINX

First, we will install the Latest Nginx web server on our system. Use the following commands to add PPA for installing the latest Nginx version on your Ubuntu 22.04 Linux.

Use the following commands to install Nginx web server.

sudo apt install nginx 

This will install the Nginx web server and start the service.

Now, you need to allow webserver ports in the firewall. To allow ports 80 and 443 in the UFW firewall, execute the following commands.

sudo ufw allow 80/tcp 
sudo ufw allow 43/tcp 

Open a web browser on your system and type the server’s IP in the address bar. You will get the default Nginx server page

How to Install LEMP Stack on Ubuntu 22.04
Nginx Default Page

Step 2 – Installing PHP

First, you need to decide on the PHP version to install on your system. You can also install multiple PHP versions on a single system. Currently the repository contains PHP 5.6, PHP 7.1, 7.2, 7.3, 7.4 and PHP 8.0, 8.1. The below instruction will install PHP 8.1. Please change the version as per your requirements.

The ondrej/php ppa contains all PHP version’s for Ubuntu systems. So add this repository in your system with command below:

sudo add-apt-repository ppa:ondrej/php 

Now update the apt cache and install PHP 8.1.

sudo apt update 
sudo apt install php8.1 

This will install PHP on your Ubuntu system along with some useful PHP extensions.

Step 3 — Install and Configure PHP-FPM

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features. Nginx web server required php-fpm for processing the PHP scripts.

To install PHP-FPM, run the following command based on the installed PHP version:

sudo apt install php8.1-fpm 

Once the installation finished, check the service status:

sudo systemctl status php8.1-fpm 
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-04-07 06:26:55 UTC; 11min ago
       Docs: man:php-fpm8.1(8)
    Process: 108650 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 81 (code=exited, status=0/SUCCESS)
   Main PID: 108647 (php-fpm8.1)
     Status: "Processes active: 0, idle: 2, Requests: 2, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 1034)
     Memory: 10.7M
        CPU: 88ms
     CGroup: /system.slice/php8.1-fpm.service
             ├─108647 "php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)
             ├─108648 "php-fpm: pool www
             └─108649 "php-fpm: pool www

Apr 07 06:26:55 ubuntu2204 systemd[1]: Starting The PHP 8.1 FastCGI Process Manager...
Apr 07 06:26:55 ubuntu2204 systemd[1]: Started The PHP 8.1 FastCGI Process Manager.

In Step 6, we will configure the Nginx virtual host with PHP-FPM to serve PHP applications.

Step 4 – Installing MySQL

The default Ubuntu repositories contain MySQL 8.0. Which can be directly installed using the package manager. To install the available MySQL server version, execute the following command.

sudo apt-get install mysql-server 

Once the installation is finished, you can secure the MySQL server by executing the following command.

sudo mysql_secure_installation 

This will ask for a few questions to secure the MySQL server.

  1. Press ‘y’ to enable validate password plugin. This will allow you to set a strict password policy for user accounts.
    VALIDATE PASSWORD COMPONENT can be used to test passwords
    and improve security. It checks the strength of password
    and allows the users to set only those passwords which are
    secure enough. Would you like to setup VALIDATE PASSWORD component?
    
    Press y|Y for Yes, any other key for No: y
    
  2. Chose the password complexity level. Read all 3 options and choose one. For production servers we recommend to choose STRONG policy.
    LOW    Length >= 8
    MEDIUM Length >= 8, numeric, mixed case, and special characters
    STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
    
    Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
    
  3. Enter a new password and re-enter it. Make sure it matches the complexity level as described above.
    New password: *************
    Re-enter new password: *************
    
  4. Press ‘y’ to continue with provided password.
    Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
    
  5. Remove default anonymous users from MySQL server:
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
    
  6. Disable root login from remote systems
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
    
  7. Remove test database form MySQL created by default during installation.
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
    
  8. Reload all privileges to apply above changes immediately.
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
    

You have secured the MySQL server in the LAMP stack on Ubuntu 22.04 Linux system.

Remember that the above password set for the root accounts is used for remote users only. To log in from the same system, just type mysql on terminal.

sudo mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.28-0ubuntu4 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Type ‘quit’ to exit from the MySQL shell and return to the system terminal.

Step 5 – Installing Additional Packages

You may also need to install modules like MySQL and other extensions for PHP based on the application requirements. Use the following command to find our available PHP extensions.

sudo apt search php8.1-* 

The above command will list all available PHP7 modules for installation, Let’s begin the installation of modules.

sudo apt install php8.1-mysql php8.1-curl php8.1-xml 

You may also need to install other required PHP extensions on your system.

Step 6 — Configure Nginx VirtualHost

Finally, do the configuration of the Nginx virtual host. For this example, we are editing the default configuration file.

sudo nano /etc/nginx/sites-enabled/default 

and make changes as below.

server {
        listen   80;

        root /var/www/example.com;
		
        index index.php;
		
        server_name  example.com www.example.com;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        }

}

In the above configuration file, look for location ~ \.php$ section. Which is required to service PHP scripts via Nginx server.

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

Step 7 – Verify Setup

You have successfully completed the installation of Nginx, MySQL, and PHP on the Ubuntu 22.04 Linux system. To verify the PHP integration with Nginx, create a PHP script (example: info.php) on the website document root and write the below content.

<?php
   phpinfo();
?>

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

http://server-ip-or-domain-name/info.php 
Running PHP Script with Nginx and PHP-FPM

Conclusion

This tutorial helped you to set up the LEMP (Linux, Nginx, MySQL, and PHP) stack on Ubuntu 22.04 LTS system. Now, you can host PHP-based web applications on your server.

The post How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-install-lemp-stack-on-ubuntu-22-04/feed/ 4
How to Install PHP 7, NGINX, MySQL on CentOS/RHEL 7.6 & 6.10 https://tecadmin.net/install-php-7-nginx-mysql-5-on-centos/ https://tecadmin.net/install-php-7-nginx-mysql-5-on-centos/#comments Thu, 11 Feb 2016 09:49:01 +0000 https://tecadmin.net/?p=9812 A few days back PHP version 7.2 has been released. Which has a number of changes and improvements over PHP version 7.X. This article will help you to install PHP 7, NGINX and MySQL 5.6 on CentOS / RHEL 7.4 & 6.9 operating systems. This tutorial has been tested with CentOS 7.4, so all the [...]

The post How to Install PHP 7, NGINX, MySQL on CentOS/RHEL 7.6 & 6.10 appeared first on TecAdmin.

]]>
A few days back PHP version 7.2 has been released. Which has a number of changes and improvements over PHP version 7.X. This article will help you to install PHP 7, NGINX and MySQL 5.6 on CentOS / RHEL 7.4 & 6.9 operating systems. This tutorial has been tested with CentOS 7.4, so all the services command are used with systemctl, For CentOS 6 users change all systemctl command correspondence service command.

php7-nginx-mysql

Step 1 – Setup Yum Repository

In the first step install all the required yum repositories in your system used in the remaining tutorial for various installations. You are adding REMI, EPEL, Webtatic & MySQL community server repositories in your system.

CentOS / RHEL 7

yum install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
rpm -Uvh http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

CentOS / RHEL 6

yum install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -Uvh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm

Step 2 – Install PHP 7

Now install PHP 7 packages from remi-php72 rpm repository using the following command.

yum --enablerepo=remi-php72 install php

Now install required php modules. Use following command to list available modules in yum repositories.

yum --enablerepo=remi-php72 search php

Now check all listed modules in above command and install required modules like below.

yum --enablerepo=remi-php72 install php-mysql php-xml \
php-soap php-xmlrpc php-mbstring php-json php-gd php-mcrypt

Step 3 – Install NGINX

NGINX is the popular web server used on Linux systems. Let’s install the Nginx web server using the following command on your system.

yum install nginx

Now start nginx service and enable to start on boot using below commands.

systemctl enable nginx.service
systemctl start nginx.service

Step 4 – Install MySQL 5.6

In step 1 we already have installed required yum repository in your system. Lets use following command to install MySQL server on your system.

yum install mysql-server

You need to execute mysql_secure_installation once after installation of MySQL server using following command. First it will prompt to set a password for root account, after that ask few questions, I suggest to say yes ( y ) for all.

systemctl start mysqld.service
mysql_secure_installation

Now restart MySQL service and enable to start on system boot.

systemctl restart mysqld.service
systemctl enable mysqld.service

Step 5 – Setup PHP-FPM

Now use following command to install php7 fpm package using following command.

yum --enablerepo=remi-php72 install php-fpm

Step 6 – Create Nginx VirtualHost

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

sudo vi /etc/nginx/conf.d/example.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 Virtual Hosts configured.

Step 7 – Restart Services

After installing all services on your system, start all required services.

systemctl restart nginx.service
systemctl restart php-fpm.service

Step 8. Open Port in Firewall

Finally open firewall ports for HTTP (80) and https (443) services using the following command.

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

Step 9. Verify Setup

Let’s check the installed versions of packages on system using following commands one by one.

# php -v

PHP 7.2.0 (cli) (built: Nov 28 2017 20:22:21) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
# nginx -v

nginx version: nginx/1.12.2

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

<?php
  phpinfo();
?>

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

Install PHP 7, NGINX & MySQL 5.6

You have successfully configured LEMP Stack setup on your CentOS / RHEL 7.4 & 6.9 system.

The post How to Install PHP 7, NGINX, MySQL on CentOS/RHEL 7.6 & 6.10 appeared first on TecAdmin.

]]>
https://tecadmin.net/install-php-7-nginx-mysql-5-on-centos/feed/ 6
How to Install Nginx, PHP 7 & MySQL on Ubuntu 16.04, 14.04 https://tecadmin.net/install-php-7-nginx-mysql-on-ubuntu/ https://tecadmin.net/install-php-7-nginx-mysql-on-ubuntu/#comments Fri, 05 Feb 2016 10:24:23 +0000 https://tecadmin.net/?p=9800 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. Step 1 – Nginx Installation First [...]

The post How to Install Nginx, PHP 7 & MySQL on Ubuntu 16.04, 14.04 appeared first on TecAdmin.

]]>
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.

<?php
 phpinfo();
?>

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

Install PHP 7, NGINX & MySQL

The post How to Install Nginx, PHP 7 & MySQL on Ubuntu 16.04, 14.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/install-php-7-nginx-mysql-on-ubuntu/feed/ 8