Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Build LEMP (Linux+Nginx+MySQL+PHP) environment under CentOS 8.1 (detailed tutorial)

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

LEMP is a software stack that contains a set of free open source tools to power high-traffic and dynamic websites. LEMP is an acronym for Linux,Nginx (pronounced Engine X), MariaDB/MySQL and PHP.

Nginx is an open source, powerful and high-performance Web server, which can also be used as a reverse proxy. MariaDB is a database system for storing user data, while PHP is a server-side scripting language for developing and supporting dynamic web pages.

Related:

Building LAMP (Linux+Apache+MySQL+PHP) Environment https://www.linuxidc.com/Linux/2020-02/162446.htm under CentOS 8.1

In this article, you will learn how to install the LEMP server on the CentOS 8 Linux distribution.

Step 1: update the package on CentOS 8

First, update the repository and software packages on CentOS 8 Linux by running the following dnf command.

[linuxidc@localhost ~ / www.linuxidc.com] $sudo dnf update

Update the CentOS 8 software package

Step 2: install the Nginx Web server on CentOS 8

After the package update is complete, install Nginx using a simple command.

[linuxidc@localhost ~ / www.linuxidc.com] $sudo dnf install nginx

Install Nginx on CentOS 8

This code snippet shows that the Nginx installation is going well without any problems.

Install Nginx on CentOS 8

After the installation is complete, configure Nginx to start itself at system startup and verify that Nginx is running by executing the command.

[linuxidc@localhost ~ / www.linuxidc.com] $sudo systemctl enable nginx [linuxidc@localhost ~ / www.linuxidc.com] $sudo systemctl start nginx

Configure Nginx to start automatically when the system starts

[linuxidc@localhost ~ / www.linuxidc.com] $sudo systemctl status nginx

Verify Nginx service status

To check the installed version of Nginx, run the command.

[linuxidc@localhost ~ / www.linuxidc.com] $nginx-vnginx version: nginx/1.14.1

Check the Nginx version

If you are curious about Nginx and want to discover more information about Nginx, execute the following rpm command.

[linuxidc@localhost ~ / www.linuxidc.com] $rpm-qi nginx

View Nginx details

To verify that the Nginx server is running in a browser, simply type the IP address or URL of the system in the URL column (this article uses https://www.linuxidc.com as an example, as shown in the figure below), and then press Enter.

You should see "Welcome to nginx onRed Hat Enterprise Linux!" Web page, which indicates that your Nginx Web server is up and running.

Check the Nginx web page

Step 3: install MariaDB on CentOS 8

MariaDB is a free and open source offshoot of MySQL and provides the latest features that make it a better alternative to MySQL. To install MariaDB, run the command.

Install MariaDB in CentOS 8

"to have MariaDB start automatically when the system starts, run."

[linuxidc@localhost ~ / www.linuxidc.com] $systemctl start mariadb

[linuxidc@localhost ~ / www.linuxidc.com] $systemctl enable mariadb

Created symlink / etc/systemd/system/mysql.service → / usr/lib/systemd/system/mariadb.service.

Created symlink / etc/systemd/system/mysqld.service → / usr/lib/systemd/system/mariadb.service.

Created symlink / etc/systemd/system/multi-user.target.wants/mariadb.service → / usr/lib/systemd/system/mariadb.service.

MariaDB starts automatically when the system starts

After installation, use the following command to check its status.

Verify MariaDB service status

The MariaDB database engine is not secure and anyone can log in without credentials. To harden and protect MariaDB to minimize the chances of unauthorized access, run the command.

[linuxidc@localhost ~ / www.linuxidc.com] $mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current

Password for the root user. If you've just installed MariaDB, and

You haven't set the root password yet, the password will be blank

So you should just press enter here.

Enter current password for root (enter for none):

You will be prompted to enter your root password (if you already have a root password) or to set it. Answer Y in each prompt that follows.

After setting the password, answer the remaining questions to delete anonymous users, delete the test database, and disable remote root login.

After you have completed all the steps, you can log in to the MariaDB server and check the MariaDB server version information (provide the password you specified when protecting the server).

[linuxidc@localhost ~ / www.linuxidc.com] $mysql-u root-p

Enter password:

Welcome to the MariaDB monitor. Commands end with; or\ g.

Your MariaDB connection id is 8

Server version: 10.3.17-MariaDB MariaDB Server

Copyright (c) 2000, 2018 Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)] >

Check the MariaDB version

Step 4: install PHP 7 on CentOS 8

Finally, we will install the last LEMP component, PHP, a scripted Web programming language that is typically used to develop dynamic web pages.

At the time of writing this guide, the latest version is PHP 7.4. We will install it using the Remi repository. The Remi database is a free database that comes with the latest version of cutting-edge software and is not available on CentOS by default.

Run the following command to install the EPEL repository.

[linuxidc@localhost ~ / www.linuxidc.com] $sudo dnf install https://dl.Fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

Next, install yum utils and enable remi-repository using the following command.

[linuxidc@localhost ~ / www.linuxidc.com] $sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm

After successfully installing yum-utils and Remi-packages, search for downloadable PHP modules by running the command.

[linuxidc@localhost ~ / www.linuxidc.com] $sudo dnf module list php

The output will include available PHP modules, streams, and installation configuration files, as shown below.

Extra Packages for Enterprise Linux 8-x86 MB 64 3.3 MB/s | 5.9 MB 00:01

Remi's Modular repository for Enterprise Linux 7.1kB/s | 535kB 01:15

Safe Remi's RPM repository for Enterprise Linux 3.7 kB/s | 1.4 MB 06:27

Last metadata expiration check: performed before 0:00:03 on Wednesday, February 26, 2020, 07:39:24.

CentOS-8-AppStream

Name Stream Profiles Summary

Php 7.2 [d] [e] common [d], devel, minimal PHP scripting language

Php 7.3 common, devel, minimal PHP scripting language

Remi's Modular repository for Enterprise Linux 8-x86o64

Name Stream Profiles Summary

Php remi-7.2 common [d], devel, minimal PHP scripting language

Php remi-7.3 common [d], devel, minimal PHP scripting language

Php remi-7.4 common [d], devel, minimal PHP scripting language

Tip: [d] default, [e] enabled, [x] disabled, [I] installed

The output indicates that the currently installed version of PHP is PHP 7.2. To install the newer version of PHP 7.4, reset the PHP module.

[linuxidc@localhost ~ / www.linuxidc.com] $sudo dnf module reset php

After resetting the PHP module, enable the PHP 7.4 module by running.

[linuxidc@localhost ~ / www.linuxidc.com] $sudo dnf module enable php:remi-7.4

Finally, use the command to install PHP,PHP-FPM (FastCGI process Manager) and the associated PHP module.

[linuxidc@localhost ~ / www.linuxidc.com] $sudo dnf install php php-opcache php-gd php-curl php-mysqlnd

Installed:

Php-7.4.3-1.el8.remi.x86_64

Php-gd-7.4.3-1.el8.remi.x86_64

Php-mysqlnd-7.4.3-1.el8.remi.x86_64

Php-opcache-7.4.3-1.el8.remi.x86_64

Nginx-filesystem-1:1.14.1-9.module_el8.0.0+184+e34fea82.noarch

Php-fpm-7.4.3-1.el8.remi.x86_64

Php-mbstring-7.4.3-1.el8.remi.x86_64

Php-sodium-7.4.3-1.el8.remi.x86_64

Oniguruma-6.8.2-1.el8.x86_64

Libsodium-1.0.18-2.el8.x86_64

Php-pdo-7.4.3-1.el8.remi.x86_64

Over!

Verify that the installed version can be run.

[linuxidc@localhost ~ / www.linuxidc.com] $php-v

PHP 7.4.3 (cli) (built: Feb 18 2020 11:53:05) (NTS)

Copyright (c) The PHP Group

Zend Engine v3.4.0, Copyright (c) Zend Technologies

With Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Perfect! Now, we have installed PHP 7.4. Equally important, we need to start and enable PHP-FPM at startup.

[linuxidc@localhost ~ / www.linuxidc.com] $sudo systemctl start php-fpm

[sudo] password of linuxidc:

[linuxidc@localhost ~ / www.linuxidc.com] $sudo systemctl enable php-fpm

Created symlink / etc/systemd/system/multi-user.target.wants/php-fpm.service → / usr/lib/systemd/system/php-fpm.service.

To check its status, execute the command.

[linuxidc@localhost ~ / www.linuxidc.com] $sudo systemctl status php-fpm

Check PHP-FPM status

The other thing is that by default, PHP-FPM is configured to run as the Apache user. But since we are running the Nginx Web server, we need to change it to the Nginx user.

Therefore, open the file / etc/php-fpm.d/www.conf.

[linuxidc@localhost ~ / www.linuxidc.com] $sudo nano / etc/php-fpm.d/www.conf

Find these two lines.

User = apache

Group = apache

Now change both values to Nginx.

User = nginx

Group = nginx

Configure PHP-FPM

Save and exit the configuration file.

Then restart Nginx and PHP-FPM for the changes to take effect.

[linuxidc@localhost ~ / www.linuxidc.com] $sudo systemctl restart nginx

[sudo] password of linuxidc:

[linuxidc@localhost ~ / www.linuxidc.com] $sudo systemctl restart php-fpm

Step 5: test PHP information

By default, the Web directory folder for Nginx is located in the / usr/share/nginx/html/ path. To test PHP-FPM, we will create a phpinfo file.

[linuxidc@localhost ~ / www.linuxidc.com] $cd / usr/share/nginx/html/ [linuxidc@localhost / usr/share/nginx/html] $su

Password:

[root@localhost / usr/share/nginx/html] $echo "" > linuxidc.com.php

Save and exit the file.

Start the browser and type the IP address or URL of your Web server in the URL bar (this article uses https://www.linuxidc.com as an example), as shown in the figure.

If all goes well, you will see information about the running version of PHP and display other metrics.

OK, in this way, you have now successfully installed the LEMP server stack on CentOS 8. For security reasons, you may want to delete the info.php file to prevent people from getting information from your Nginx server.

Summary

So far, this is the end of this article on building LEMP (Linux+Nginx+MySQL+PHP) environment under CentOS 8.1. for more information about the construction of centos8 linux installation lemp environment, please search for previous articles or continue to browse the relevant articles below. I hope you will support it in the future!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report