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

How to install and configure LEMP on a CentOS server

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the knowledge of "how to install and configure LEMP on the CentOS server". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The LEMP portfolio package is an increasingly popular web service portfolio package that plays a powerful role in the core web services in many production environments. As its name implies, the LEMP package consists of Linux, nginx, MariaDB/MySQL, and PHP. The Apache HTTP protocol server used in the traditional LAMP package has poor performance and is difficult to cluster on a large scale. Compared with the high performance and lightweight features of nginx, it is its alternative. MariaDB is an offshoot of a community-driven MySQL database with more features and better performance. PHP, a server-side programming language, is handled by the enhanced PHP-FPM component of PHP FastCGI to generate dynamic content of web pages.

(LCTT translation note: why use the abbreviation LEMP instead of LNMP? According to https://lemp.io/, the pronunciation of Nginx is Engine-X, the important pronunciation is not the initials, and LEMP is actually readable, while LNMP appears to be just the alphabet. )

In this article, we demonstrate how to install the LEMP package on the CentOS operating platform. The target of our installation is CentOS 6 and CentOS 7 operating platforms, and we will point out their differences if necessary.

Step 1: Nginx

Let's install nginx on CentOS as a first step, and then make some basic configurations for it, such as enabling it to start at boot time and personalizing the firewall.

Install Nginx

Let's install a prebuilt stable version of the nginx package from its official RPM source.

On CentOS 7 systems:

The code is as follows:

$sudo rpm-- import http://nginx.org/keys/nginx_signing.key

$sudo rpm-ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

$sudo yum install nginx

On CentOS 6 systems:

The code is as follows:

$sudo rpm-- import http://nginx.org/keys/nginx_signing.key

$sudo rpm-ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

$sudo yum install nginx

Note that before installing the nginx RPM package, if you do not import the official GPG key for nginx, you will issue a warning like the following:

The code is as follows:

Warning: / var/tmp/rpm-tmp.KttVHD: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY

Start Nginx

Nginx does not start automatically after the installation is complete. Now let's start it and configure it to boot as the operating system boots. We also need to open the TCP/80 port in the firewall so that nginx's web service can be accessed remotely. All these operations and settings can be realized by entering the following command.

On CentOS 7 systems:

The code is as follows:

$sudo systemctl start nginx

$sudo systemctl enable nginx

$sudo firewall-cmd-zone=public-add-port=80/tcp-permanent

$sudo firewall-cmd-reload

On CentOS 6 systems:

The code is as follows:

$sudo service nginx start

$sudo chkconfig nginx on

$sudo iptables-I INPUT-p tcp-m tcp-- dport 80-j ACCEPT

$sudo service iptables save

Test Nginx

The default document directory for nginx is / usr/share/nginx/html. The default index.html file must already be in this directory. Let's check to see if we can access the test web page and enter the ip address / access of http://nginx.

If you see the page shown above, nginx has started normally.

Step 2: MariaDB/MySQL

The next step is to install the database component of the LEMP package. The server / client installation package for MySQL is available in CentOS/RHEL 6 or earlier, but CentOS/RHEL 7 has used MariaDB instead of the default MySQL. As a simple alternative to MySQL, MariaDB ensures maximum compatibility with MySQL's API and command line usage. The following is an example of how to install and configure MaraDB/MySQL on CentOS.

On CentOS 7 systems:

Do the following to install the MariaDB service / client package and start the MariaDB service.

The code is as follows:

$sudo yum install mariadb-server

$sudo systemctl start mariadb

$sudo systemctl enable mariadb

On CentOS 6 systems:

Install the MySQL service / client package and start the MySQL service as shown below.

The code is as follows:

$sudo yum install mysql-server

$sudo service mysqld start

$sudo chkconfig mysqld on

After successfully starting the MariaDB/MySQL service, execute the script in the MariaDB/MySQL service package. This operation will make some security enhancements for the database server, such as setting (non-empty) root password, deleting anonymous users, and locking remote access.

The code is as follows:

$sudo mysql_secure_installation

This is the setting of the database. Now move on to the next step.

Step 3: PHP

PHP is an important component in the LEMP package, which is responsible for extracting the data stored in the MariaDB/MySQL server to generate dynamic content. In order to need LEMP, you need to install at least two modules, PHP-FPM and PHP-MySQL. PHP-FPM (FastCGI process Manager) implements the access interface between nginx servers and PHP applications that generate dynamic content. The PHP-MySQL module enables PHP programs to access MariaDB/MySQL databases.

Install the PHP module

On CentOS 7 systems:

The code is as follows:

$sudo yum php php-fpm php-mysql

On CentOS 6 systems:

First, you need to install the REMI library from the repository (see this guide) and install the software package.

The code is as follows:

$sudo yum-enablerepo=remi install php php-fpm php-mysql

There are two things to be aware of when installing PHP:

In the CentOS 6 system, when the latest php-mysql module in the REMI repository is installed, the MySQL server package and client package are automatically updated as part of the dependency package.

In CentOS 6 and CentOS 7, when you install the PHP package, you install the Apache web server (that is, httpd) as its dependency package. This will conflict with the nginx web server. This question will be discussed in the next section.

Depending on your usage, you can use the yum command to customize your PHP engine, and you may want to install any of the following extension PHP module packages.

Php-cli: the command line interface for PHP. It is very useful to test PHP from the command line.

Php-gd: image processing support for PHP.

Php-bcmath: mathematical support for PHP.

Php-mcrypt: encryption algorithm support for PHP (for example, DES, Blowfish, CBC, CFB, ECB ciphers, etc.).

Php-xml: XML parsing and processing support for PHP.

Php-dba: data abstraction layer support for PHP.

Php-pecl-apc: PHP Accelerator / cache support.

To see a complete list of available PHP modules at installation time, run:

The code is as follows:

$sudo yum search php- (CentOS 7)

$sudo yum-enablerepo=remi search php- (CentOS 6)

Start PHP-FPM

You need to start PHP-FPM and put it in the list of automatic startup services.

On CentOS 7 systems:

The code is as follows:

$sudo systemctl start php-fpm

$sudo systemctl enable php-fpm

On CentOS 6 systems:

The code is as follows:

$sudo chkconfig php-fpm on

$sudo service php-fpm start

Step 4: configure the LEMP package

The final step in this tutorial is to adjust the configuration of the LEMP package.

Make Httpd unavailable

First, let's disable the httpd service that was previously installed with the PHP package.

On CentOS 7 systems:

The code is as follows:

$sudo systemctl disable httpd

On CentOS 6 systems:

The code is as follows:

$sudo chkconfig httpd off

Configure Nginx

Next, let's configure the nginx virtual host so that nginx can handle PHP's tasks through PHP-FPM. Open / etc/nginx/conf.d/default.conf with a text editor and modify it as shown below.

The code is as follows:

$sudo vi / etc/nginx/conf.d/default.conf

Server {

Listen 80

Server_name www.server_domain.com

Root / usr/share/nginx/html

Index index.php index.html index.htm

Location / {

}

# redirect server error pages to the static page / 50x.html

Error_page 500 502 503 504 / 50x.html

Location = / 50x.html {

}

# nginx passes PHP scripts to FastCGI server via a TCP/9000 socket

# this setting much be consistent with / etc/php-fpm.d/www.conf

# try_files prevents nginx from passing bad scripts to FastCGI server

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

}

}

The default number of working threads for nginx (specified in the / etc/nginx/nginx.conf file) is 1, so let's adjust this number as well. Generally speaking, we should create the same number of worker threads as the number of CPU cores. To confirm the core count of your CPU, run the following command:

The code is as follows:

$grep processor / proc/cpuinfo | wc-l

If your CPU is 4-core, modify the / etc/nginx/nginx.conf file as shown below.

The code is as follows:

$sudo vi / etc/nginx/nginx.conf

Worker_processes 4

Configure PHP

Next, let's customize the configuration file / etc/php.ini for PHP. More specifically, add the following two lines to the / etc/php.ini file.

The code is as follows:

Cgi.fix_pathinfo=0

Date.timezone = "PRC"

For security reasons, what we want is that the PHP interpreter only handles file tasks that specify file paths, rather than predicting and searching for files that do not exist. That's what the first line above does. LCTT translation note: the time zone used in the original text is "America/New York". According to the domestic situation, Chinese cities under PRC or Asia should be used. )

The second line defines the use of the relevant default time zone for date / time dependent functions in PHP. Use this guide to find out your time zone and set the value of the appropriate date.timezone.

Test PHP

Finally, let's test whether nginx can handle PHP pages. Be sure to restart nginx and PHP-FPM before testing.

On CentOS 7 systems:

The code is as follows:

$sudo systemctl restart nginx

$sudo systemctl restart php-fpm

On CentOS 6 systems:

The code is as follows:

$sudo service nginx restart

$sudo service php-fpm restart

Create a file called test.php, then write the following and put it in the / usr/share/nginx/html directory.

The code is as follows:

Open a browser and enter the IP address / test.php of http://nginx.

If you can see the page shown above, the LEMP set up is completely successful!

That's all for "how to install and configure LEMP on a CentOS server". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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