Server-tutorials.net
  • Home
  • CentOS
    • All
    • Game Hosting
    • Hosting Control Panel
    How to Install Laravel on CentOS 8 Linux Server

    How to Install Laravel on CentOS 8 Linux Server

    How to Host a Minecraft Server on a CentOS 8 Server

    How to Host a Minecraft Server on a CentOS 8 Server

    How to Install and Configure an Email Server on CentOS 8 (dovecot,postfix,mariadb,roundcube)

    How to Install and Configure an Email Server on CentOS 8 (dovecot,postfix,mariadb,roundcube)

    How To Set Up a Firewall Using FirewallD on CentOS 7

    How To Set Up a Firewall Using FirewallD on CentOS 7

    How to Install VestaCP on CentOS 7: A Comprehensive Guide

    How to Install VestaCP on CentOS 7: A Comprehensive Guide

    engineer holding laptop

    How to Install cPanel on CentOS 8: A Comprehensive Guide

    close up photo of ethernet cables on network switch

    Setup a Firewall Using FirewallD on CentOS 7

    security logo

    Installing and Configuring a Firewall on CentOS 8 Server

    Step-by-Step Guide how to Install Elgg on Debian 12

    Setp-by-Setp Guide how to Install Elgg on CentOS 7

    Step-by-Step Guide: How to Install Elasticsearch 8 on Debian 11

    Installing Elasticsearch on CentOS 7

    Custom installation VestaCP on Debian/Ubuntu (Nginx,Apache2)

    How to install VestaCP on CentOS

    Trending Tags

    • centos
  • Debian
    • All
    • Game Hosting
    • Hosting Control Panel
    How to Install and Configure an Email Server on Debian 11 (Postfix, Dovecot, and Roundcube)

    How to Install and Configure an Email Server on Debian 11 (Postfix, Dovecot, and Roundcube)

    How to Install LEMP Stack on Debian 11 Server

    How to Install LEMP Stack on Debian 11 Server

    How to Identify and Remediate Security Breaches or Malware on Your Debian 12 Server

    How to Identify and Remediate Security Breaches or Malware on Your Debian 12 Server

    Upgrading ISPConfig from Debian 11 to Debian 12

    Upgrading ISPConfig from Debian 11 to Debian 12

    How to Downgrade PHP Version on Debian 11: A Step-by-Step Guide

    How to Downgrade PHP Version on Debian 11: A Step-by-Step Guide

    How to Host an OpenRA Game Server on Debian 12

    How to Host an OpenRA Game Server on Debian 12

    How to Install WireGuard VPN on Debian 12: A Step-by-Step Guide

    How to Install WireGuard VPN on Debian 12: A Step-by-Step Guide

    black headphones on black laptop computer

    How to Host a Counter-Strike: Global Offensive Game Server on Debian 12

    close up photo of ethernet cables on network switch

    How to Create a User for ProFTPD Server on Debian 12

    close up photo of mining rig

    Debian 12 (Bookworm) Setup with Apache, BIND, Dovecot, PureFTPD, and ISPConfig 3.2

    wooden robot

    How to Host a Minecraft Server on Debian 12: A Step-by-Step Guide

    modern computer placed near server racks

    Installing and Configuring a Firewall on Debian 12 Server

    black server racks

    A Comprehensive Guide to Backing Up a Debian 12 Server

    data codes through eyeglasses

    How to Install OpenVPN on Debian 12 Server: A Step-by-Step Guide

    close up photo of plugged cables

    Setting Up a Secure FTP Server on Debian 12

    Installing PostgreSQL 15 on Debian 11 Step-by-Step Tutorial

    Installing PostgreSQL 15 on Debian 11 Step-by-Step Tutorial

    Installing Tomcat 10 on Debian 11: Step-by-Step Guide

    Installing Tomcat 10 on Debian 11: Step-by-Step Guide

    Step-by-Step Guide how to Install Elgg on Debian 12

    Step-by-Step Guide how to Install Elgg on Debian 12

    Step-by-Step Guide: How to Install Elasticsearch 8 on Debian 11

    Step-by-Step Guide: How to Install Elasticsearch 8 on Debian 11

    Installing Laravel on Ubuntu 22.04 / Ubuntu 20.04: A Step-by-Step Guide

    Installing Laravel on Ubuntu 22.04 / Ubuntu 20.04: A Step-by-Step Guide

    Trending Tags

    • debian
    • debian 10
  • Ubuntu
    • All
    • Game Hosting
    • Hosting Control Panel
    • Network Monitoring
    How to Upgrade PHP from 7.2 to 7.4 and 8.0 on VestaCP Ubuntu 18.04

    How to Upgrade PHP from 7.2 to 7.4 and 8.0 on VestaCP Ubuntu 18.04

    Securing Nginx with Let’s Encrypt on Ubuntu 22.04: A Step-by-Step Guide

    Securing Nginx with Let’s Encrypt on Ubuntu 22.04: A Step-by-Step Guide

    How to install Counter-Strike: Global Offensive CSGO on Ubuntu 22.04 Server

    How to install Counter-Strike: Global Offensive CSGO on Ubuntu 22.04 Server

    Step-by-Step Guide: Installing Terraform on Ubuntu Server 22.04

    Step-by-Step Guide: Installing Terraform on Ubuntu Server 22.04

    How to Deploy a Secure CockroachDB Cluster on Ubuntu 22.04

    How to Deploy a Secure CockroachDB Cluster on Ubuntu 22.04

    How to Downgrade PHP Version on Ubuntu 22.04: A Step-by-Step Guide

    How to Downgrade PHP Version on Ubuntu 22.04: A Step-by-Step Guide

    How to Install and Configure an Email Server on Ubuntu 22.04 (Postfix, Dovecot, and Roundcube)

    How to Install and Configure an Email Server on Ubuntu 22.04 (Postfix, Dovecot, and Roundcube)

    How to Install Zabbix Monitoring Tool on Ubuntu 22.04

    How to Install Zabbix Monitoring Tool on Ubuntu 22.04

    How to Install Apache Guacamole via Docker on Ubuntu 22.04

    How to Install Apache Guacamole via Docker on Ubuntu 22.04

    How to Install and Configure a Proxy Server on Ubuntu 22.04 Server

    How to Install and Configure a Proxy Server on Ubuntu 22.04 Server

    close up photo of programming of codes

    How to Setup Zabbix Monitoring Tool on Ubuntu 22.04

    modern computer placed near server racks

    How to Install Plesk on Ubuntu 22.04 Server: A Comprehensive Guide

    Free server network image

    How to Install ClickHouse on Ubuntu 22.04: A Step-by-Step Guide

    man people night dark

    Installing and Configuring a Firewall on Ubuntu 22.04 Server

    ethernet cables plugged in network switch

    Securing Nginx on Ubuntu 22.04: Best Practices and Step-by-Step Guide

    close up photo of ethernet cables on network switch

    Installing FileRun on Ubuntu 22.04

    cables connected to ethernet ports

    How to Install ClickHouse on Ubuntu 22.04

    engineer holding laptop

    Install OpenNMS Network Monitoring System on Ubuntu 22.04

    Step by Step Guide how to install CakePHP on Ubuntu 22.04

    Step by Step Guide how to install CakePHP on Ubuntu 22.04

    Installing MySQL on Ubuntu 22.04: A Step-by-Step Guide

    Installing MySQL on Ubuntu 22.04: A Step-by-Step Guide

    Trending Tags

    • Ubuntu 20.04 LTS
    • Ubuntu 20.04
  • Others
    • All
    • Beginners
    industry internet connection technology

    Understanding the Differences Between Nginx and Apache

    Free server network image

    How to Install ClickHouse on Ubuntu 22.04: A Step-by-Step Guide

    crop hacker typing on laptop with data on screen

    Choosing Between VPS and Dedicated Server: A Comprehensive Comparison

    unrecognizable hacker with smartphone typing on laptop at desk

    Choosing the Best Control Panel for Server Management: A Comprehensive Guide

    close up photo of ethernet cables on network switch

    Installing FileRun on Ubuntu 22.04

    cables connected to ethernet ports

    How to Install ClickHouse on Ubuntu 22.04

    Installing Tomcat 10 on Debian 11: Step-by-Step Guide

    Installing Tomcat 10 on Debian 11: Step-by-Step Guide

    Step-by-Step Guide how to Install Elgg on Debian 12

    Step-by-Step Guide how to Install Elgg on Debian 12

    Step-by-Step Guide: How to Install Elasticsearch 8 on Debian 11

    Step-by-Step Guide: How to Install Elasticsearch 8 on Debian 11

    Trending Tags

    • php
No Result
View All Result
Server-tutorials.net
Home

How To Install Nginx, MySQL, PHP (LEMP) stack On CentOS 7

Chris Thompson by Chris Thompson
September 12, 2023
in Webhosting
2

Installing the Linux, Nginx, MySQL, and PHP (LEMP) stack on CentOS 7 is described in detail here.

Introduction

In order to host dynamic websites and online applications, a LEMP software stack is a collection of open source software that is often deployed together. As a matter of fact, this phrase is an abbreviation for Linux and the ENginx web server (which replaces the Apache component of a LAMP stack). Using a MySQL database and PHP, the site’s dynamic content is created.

In this tutorial, we’ll learn how to setup an LEMP stack on a CentOS 7 server using PHP 7.4 and MariaDB as the database server. Using MariaDB as a replacement for the original MySQL server does not need any configuration or code modifications in your application, making it easy to transition to MariaDB.

Prerequisites

You should have a non-root user account set up on your server before beginning this course. When you’ve completed steps 1-4 in CentOS 7’s basic server setup, you will learn how to accomplish this

Step 1 – Getting Nginx up and running it

The high-performance web server Nginx will be used to show our website’s content. Installing the EPEL repository, which offers extra software for the CentOS 7 operating system, is the first step in getting the newest version of Nginx.

CentOS 7 EPEL repositories may be added by using the command:

sudo yum install epel-release

These tasks are performed as root since we used the sudo command. To make sure you have root rights, it will ask you for your usual user password. You’ll also be asked to accept the terms of the agreement, so hit Y.

Nginx can now be installed on your server thanks to the EPEL repository installed on your machine.

sudo yum install nginx

Once the installation is complete, start the Nginx service by entering the following command:

sudo systemctl start nginx

By accessing your server’s public IP address in your web browser immediately away, you can verify that everything happened as intended (if you do not already know your server’s public IP address, see the note under the following section for further information on how to find out what it is):

Open in a web browser:

http://server_domain_name_or_IP/

Because it’s there for testing and informative reasons, the CentOS 7 Nginx home page will appear. Something like this should be what you get:


hA successful web server installation is confirmed by the fact that you can see this page now.

When booting, use the following command to allow Nginx to start automatically.

sudo systemctl enable nginx

What Is The Public IP Address Of Your Server?

The public IP address of your server can be found in many different places. As a general rule, this is the SSH hostname you’ll use when connecting to your server.

There are many methods to do this from the command line. Your IP address can be retrieved using the iproute2 tools if you enter this command:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

A few lines of text will be retrieved using this method. Neither of these addresses is incorrect, however your machine may only be able to utilize one of them.

As an alternative, you may hire a third-party to tell you what it thinks about your server. If you want to know what your IP address is, you can simply query a certain server:

curl http://icanhazip.com

To access your server, no matter how you get your IP address, you can put it into your web browser’s address bar and go to your server.

Step 2 – Getting MariaDB up and running

It’s time to install MariaDB, a MySQL drop-in alternative, now that our web server is up and running. Branched from MySQL, MariaDB is a free and open-source alternative.

Our program may be acquired and installed using yum again. To help us get our components to interact with one other, we’ll also install some more auxiliary packages:

sudo yum install mariadb-server mariadb

MariaDB has to be started when the installation is complete using the following command:

sudo systemctl start mariadb

It is time to execute a security script that removes certain risky defaults and restricts access to our MariaDB database now that it is up and running. Run the interactive script to get it started:

sudo mysql_secure_installation

To proceed, you’ll be prompted to provide your MariaDB root password. If you’ve just installed MariaDB, you’re probably going to be without one, so simply hit enter to clear it. After that, you’ll be prompted to choose a root password. As long as you comply with the directions, you’re good to go!

mysql_secure_installation prompts:
Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

To accept the default settings for the remaining questions, just press “ENTER” at each prompt. These new rules will be loaded so that MySQL quickly recognizes the modifications we’ve made, and this will eliminate some example users and databases.

Make sure MariaDB is not started when the system boots up. You can accomplish this by using the following command:

sudo systemctl enable mariadb

We can now proceed with setting up your database system.

Step 3 – Creating a PHP server

In order to show dynamic material, we’ll be using PHP, a component of our setup. With its scripting capabilities and the ability to access our MySQL databases, it can process data and pass it on to our web server for display.

For this reason, we’ll need to install a third-party package repository on your CentOS 7 server in order to download PHP 7+ and have it installed on your CentOS 7 server. Remi is a well-known CentOS package repository that provides the most recent PHP versions.

To set up the Remi repository on CentOS 7, follow these steps:

sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

The repository holding your desired version of PHP will need to be enabled when the installation is complete. Run the following command to see which Remi releases of PHP 7+ are now available:

yum --disablerepo="*" --enablerepo="remi-safe" list php[7-9][0-9].x86_64

You should expect to see something like this:

Output
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* remi-safe: mirrors.ukfast.co.uk
Available Packages
php70.x86_64 2.0-1.el7.remi remi-safe
php71.x86_64 2.0-1.el7.remi remi-safe
php72.x86_64 2.0-1.el7.remi remi-safe
php73.x86_64 2.0-1.el7.remi remi-safe
php74.x86_64 1.0-3.el7.remi remi-safe
php80.x86_64 1.0-3.el7.remi

In this tutorial, we’ll learn how to set up PHP 7.4, the most recent stable version. Run the following command to install the right Remi package for PHP 7.4:

sudo yum-config-manager --enable remi-php74

We can now continue with the installation of PHP using yum as normal. PHP 7.4 can now connect to MySQL databases with the help of the following Nginx command line configuration.

sudo yum install php php-mysqlnd php-fpm

Run the following command to verify that the version of PHP you selected is indeed available:

php –version

You should expect to see something like this:

OutputPHP 7.4.5 (cli) (built: Apr 14 2020 12:54:33) ( NTS )Copyright (c) The PHP GroupZend Engine v3.4.0, Copyright (c) Zend Technologies

Your system now has PHP installed and ready to use. Make a few changes to the default setup next. As an alternative to CentOS’s default text editor, vi, we’ll first install the more user-friendly nano:

sudo yum install nano

Make use of nano or another text editor to access the configuration file at /etc/php-fpm.d/www.conf:

sudo nano /etc/php-fpm.d/www.conf

Look for the directions for users and groups now. In nano, you can use CTRL+W to look for these words in the currently open file by searching with those phrases.

/etc/php-fpm.d/www.conf
…
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache
…

There is a noticeable difference between the apache user and group variables. These need be changed to nginx:

/etc/php-fpm.d/www.conf
…
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
…

The listen directive can then be found by searching for it. PHP-fpm is set up to listen on a specified port on a certain host by default. To increase server speed, we want to alter this configuration such that it listens on a local socket file. A new directive for listening should be added:

/etc/php-fpm.d/www.conf
listen = /var/run/php-fpm/php-fpm.sock;

Our socket file is now owned by us, therefore we’ll need to alter the group and owner settings. Determine where to find the commands for listening on behalf of an individual, group, and mode. By default, these lines are omitted from the source code. Remove the ; symbol at the beginning of the line to uncomment them. After that, make nginx the new owner and group:

/etc/php-fpm.d/www.conf
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

Once you’ve finished editing, save and shut the file. In nano, hit CTRL + X, Y, and ENTER to make your selection.

To activate and start the php-fpm service, type:

sudo systemctl start php-fpm

Finally, your PHP environment is ready to go. The next step is to set up Nginx so that all requests for PHP scripts are sent to php-fpm for processing.

Step 4 – Setting up Nginx for PHP Page Processing

Now that we’ve installed all of the necessary components, we’re good to go. Nginx is already configured to utilize our PHP processor for dynamic content, so we simply need to tell it to.

Using a server block, we can construct a different configuration file for each hosted website in a dedicated directory in Nginx. Virtual hosts in Apache are a good analogy for this.

The default installation, on the other hand, leaves this directory completely untouched. You can override the nginx.conf file’s default PHP website block by creating a new file in the /etc directory, which will act as the default for this server’s PHP website.

Open the /etc/nginx/conf.d directory and create a new file:

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

Make sure to change the server name directive with your server’s domain name or IP address before copying the following PHP server definition block into your configuration file:

/etc/nginx/conf.d/default.conf

server {
listen 80;
server_name server_domain_or_IP;

root /usr/share/nginx/html;
index index.php index.html index.htm;

location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

location = /50x.html {
root /usr/share/nginx/html;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

The file should be saved and closed after you’re done with it.

Nginx must be restarted to take effect.

sudo systemctl restart nginx

Everything on your web server has been set up now. We’ll test the PHP interaction with Nginx in the following stage.

Step 5 – Checking your web server’s PHP processing capabilities

Using php-fpm, we can now write a test PHP script to verify that Nginx is properly handling.php files.

Nginx’s default document root ownership will be changed so that our ordinary sudo user can create files there before we begin writing our script.

In order to make a modification to the default Nginx document root’s ownership, run the following command as the tommy user and group, replacing the highlighted username and group.

sudo chown -R tommy.tommy /usr/share/nginx/html/

A test PHP page will now be created to ensure that the web server functions as planned.

In the /usr/share/nginx/html directory, create a new PHP file named info.php.

nano /usr/share/nginx/html/info.php

The following PHP code will provide details about the server’s current PHP environment:

/usr/share/nginx/html/info.php

<? phpinfo(); ?>

Save and close the file when you’re done.

Now we can check whether our web server can show PHP script-generated content appropriately. Access the /info.php page on your server by typing its hostname or IP address into your browser:

http://server host or IP/info.php

A page like this will appear:

The file you produced, which includes critical information about your PHP environment and your CentOS server, should be deleted once you have checked the required information about your PHP server on that page. Remove that file using rm:

rm /usr/share/nginx/html/info.php

If you ever need this file again, you can easily regenerate it.

Conclusion

In this article, you learned how to create a flexible basis for providing PHP websites and apps to your users by using Nginx as a web server and the most recent PHP release version available. You’ve configured Nginx to handle PHP requests using the php-fpm module, and you’ve also configured a MariaDB database to hold the data for your website.

Tags: CentOS 7lempMySQLnginxphpstack
Previous Post

How To Install Linux, Apache, MariaDB, PHP (LAMP) stack on Debian 10

Next Post

How to Install and Configure VNC on Debian 10

Chris Thompson

Chris Thompson

Next Post
How to Install LEMP Stack on Debian 10 Server

How to Install and Configure VNC on Debian 10

Ubuntu 20.04 LTS Nginx Performance Optimization

Ubuntu 20.04 LTS Nginx Performance Optimization

How to install WHM & CPanel on Ubuntu 20.04 LTS Linux

How to install WHM & CPanel on Ubuntu 20.04 LTS Linux

Comments 2

  1. trillium says:
    2 years ago

    Mу partner and I stumbled over here coming from
    a different page and thought I should chеck
    things out. I like whаt I sеe so now i am following you.
    Look forward to finding out about your web page for a second
    time.

    Reply
  2. Tyson says:
    1 year ago

    Exactly what i was looking for. Thank You

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categorys

  • AI Server Technology
  • Apache
  • Backup
  • CentOS
    • Game Hosting
    • Hosting Control Panel
  • Crypto
  • Debian
    • Game Hosting
    • Hosting Control Panel
  • Email
  • FTP
  • Laravel
  • MySQL
  • Nginx
  • Others
    • Beginners
  • PHP
  • Security
    • Firewall
  • Tor Hosting
  • Ubuntu
    • Game Hosting
    • Hosting Control Panel
      • Plesk
      • VestaCP
    • Network Monitoring
  • VNC
  • VPN
  • Webhosting

Popular Posts

  • How to install WHM & CPanel on Ubuntu 20.04 LTS Linux

    How to install WHM & CPanel on Ubuntu 20.04 LTS Linux

    664 shares
    Share 266 Tweet 166
  • Ubuntu 20.04 LTS Nginx Performance Optimization

    606 shares
    Share 242 Tweet 152
  • How to host a Tor .onion site on a Debian Server (nginx + tor)

    602 shares
    Share 241 Tweet 151
  • How to upgrade php7.2 to php7.4 in VestaCP on Ubuntu ?

    601 shares
    Share 240 Tweet 150
  • How to use nginx as reverse proxy on ubuntu 20.04

    600 shares
    Share 240 Tweet 150

Recent Posts

How to Upgrade PHP from 7.2 to 7.4 and 8.0 on VestaCP Ubuntu 18.04

How to Upgrade PHP from 7.2 to 7.4 and 8.0 on VestaCP Ubuntu 18.04

September 26, 2023
How to Install Laravel on CentOS 8 Linux Server

How to Install Laravel on CentOS 8 Linux Server

September 26, 2023
Securing Nginx with Let’s Encrypt on Ubuntu 22.04: A Step-by-Step Guide

Securing Nginx with Let’s Encrypt on Ubuntu 22.04: A Step-by-Step Guide

September 14, 2023

About Us

Server-tutorials.net - Linux Debian, Ubuntu, CentOS Server Tutorials.

Popular Tag

7.2 7.4 18.04 22.04 Apache apache2 backup server centos CentOS 7 centos 8 cpanel debian debian 11 Dovecot Elasticsearch firewall centos 8 FTP Server how to howto How to Install ClickHouse on Ubuntu 22.04 install installation kvm lavarel lemp linux mariadb MySQL nginx node openvz php Postfix server setup tutorial ubuntu Ubuntu 20.04 Ubuntu 22.04 update vesta vestacp vnc vserver Wireguard

Recent News

How to Upgrade PHP from 7.2 to 7.4 and 8.0 on VestaCP Ubuntu 18.04

How to Upgrade PHP from 7.2 to 7.4 and 8.0 on VestaCP Ubuntu 18.04

September 26, 2023
How to Install Laravel on CentOS 8 Linux Server

How to Install Laravel on CentOS 8 Linux Server

September 26, 2023
  • Cookie Policy
  • Privacy & Policy
  • Change privacy settings
  • Privacy settings history
  • Revoke consents
  • Contact
  • Imprint

© 2022 Server-tutorials.net - Linux Server Tutorials

No Result
View All Result
  • Home
  • CentOS
    • Hosting Control Panel
  • Debian
    • Backup
    • FTP
    • Hosting Control Panel
    • Laravel
    • Laravel
    • Network Monitoring
    • Security
      • Firewall
    • Tor Hosting
    • VPN
    • Webhosting
  • Ubuntu
    • VestaCP
    • Nginx
  • Others
    • Beginners
  • Contact
  • Privacy Policy
  • Cookie Policy
  • Imprint

© 2022 Server-tutorials.net - Linux Server Tutorials