LEMP portfolio is an increasingly popular web service portfolio software package, which plays a powerful role in the core web services in many production environments. As the name implies, the LEMP package consists of Linux, nginx, MariaDB / MySQL, and PHP. Apache HTTP protocol server used in traditional lamp package has low performance and is difficult to cluster on a large scale. Compared with nginx, its high performance and lightweight features are its alternative. MariaDB is a branch of MySQL database driven by community support, which has more functions and better performance. PHP, the server-side programming language, is processed by the php-fpm component of PHP fastcgi to generate the dynamic content of web pages.
Lctt: why use LEMP instead of LNMP? According to https://lemp.io/ The pronunciation of nginx is engine-x, the important pronunciation is not the first letter, and LEMP is actually readable, while LNMP looks like an alphabet.)
In this article, we demonstrate how to install LEMP package on CentOS operating platform. We are installing CentOS 6 and CentOS 7. If necessary, we will point out their differences.
Step 1: nginx
Let’s install nginx on CentOS as the first step, and then make some basic configuration for it, such as booting and personalizing the firewall.
Install nginx
Let’s install a pre built stable version of nginx package from its official RPM source.
On CentOS 7 system:
The code is as follows:
$ 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 system:
The code is as follows:
$ 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: before installing nginx RPM package, if you do not import the official GPG key of nginx, you will give a warning as follows:
The code is as follows:
Start nginx
After installation, nginx will not start automatically. Now let’s start it, and make some configuration so that it can start as the operating system starts. We also need to open the TCP / 80 port in the firewall so that we can access the web services of nginx remotely. All these operations and settings can be realized by inputting the following commands.
On CentOS 7 system:
The code is as follows:
$ sudo systemctl enable nginx
$ sudo firewall-cmd –zone=public –add-port=80/tcp –permanent
$ sudo firewall-cmd –reload
On CentOS 6 system:
The code is as follows:
$ 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 of nginx is / usr / share / nginx / HTML. default index.html The file must be in this directory. Let’s check if we can access the test web page and enter http://nginx IP address / access of.
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 LEMP package. The server / client installation package of MySQL is provided in CentOS / RHEL 6 or earlier versions, but the default MySQL has been replaced by MariaDB in CentOS / RHEL 7. 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 system:
Install the MariaDB service / client package and start the MariaDB service as follows.
The code is as follows:
$ sudo systemctl start mariadb
$ sudo systemctl enable mariadb
On CentOS 6 system:
As shown below, install the MySQL service / client package and start the MySQL service.
The code is as follows:
$ 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 time, some security enhancement measures will be taken for the database server, such as setting (non empty) root password, deleting anonymous users and locking remote access.
The code is as follows:
This is the setting of the database. Now take the next step.
Step 3: PHP
PHP is an important component in LEMP package, which is responsible for fetching the data stored in MariaDB / MySQL server to generate dynamic content. For 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 server and PHP application that generates dynamic content. The php-mysql module enables PHP programs to access the MariaDB / MySQL database.
Install PHP module
On CentOS 7 system:
The code is as follows:
On CentOS 6 system:
First, you need to install the Remi library from the repository (see this guide) and install the package.
The code is as follows:
When installing PHP, you should pay attention to two aspects:
In CentOS 6 system, when installing the latest PHP MySQL module in Remi warehouse, the server package and client package of MySQL will be automatically updated as part of the dependency package.
In CentOS 6 and CentOS 7, when installing PHP package, Apache Web server (httpd) will be installed as its dependent package. This conflicts with the nginx web server. This problem will be discussed in the next section.
Depending on your usage, you can customize your PHP engine by using the yum command. You may want to install any of the following extended PHP module packages.
- PHP cli: the command line interface of PHP. It’s very useful when testing PHP from the command line.
PHP Gd: PHP image processing support.
PHP bcmath: mathematical support for PHP.
PHP mcrypt: PHP encryption algorithm support (such as DES, blowfish, CBC, CFB, ECB ciphers, etc.).
PHP XML: PHP’s XML parsing and processing support.
PHP DBA: PHP’s data abstraction layer support.
PHP PECL APC: PHP accelerator / cache support.
To view the complete list of available PHP modules during installation, you can run:
The code is as follows:
$ sudo yum –enablerepo=remi search php- (CentOS 6)
Start php-fpm
You need to start php-fpm and put it on the list of auto start services.
On CentOS 7 system:
The code is as follows:
$ sudo systemctl enable php-fpm
On CentOS 6 system:
The code is as follows:
$ sudo service php-fpm start
Step 4: configure LEMP package
The final step in this tutorial is to adjust the configuration of the LEMP package.
Make httpd unavailable
First of all, let’s disable the httpd service installed with the PHP package earlier.
On CentOS 7 system:
The code is as follows:
On CentOS 6 system:
The code is as follows:
Configure nginx
Next, let’s configure the nginx virtual host so that nginx can handle PHP tasks through php-fpm. Open / etc / nginx / conf.d with a text editor/ default.conf , and then modify as follows.
The code is as follows:
<p> 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 worker threads of nginx (in / etc / nginx)/ nginx.conf (specified in the file) is 1. Let’s adjust this number. Generally speaking, the number of worker threads we create should be the same as the number of CPU cores. To be sure of the number of cores in your CPU, run the following command:
The code is as follows:
If your CPU is 4-core, modify / etc / nginx as follows/ nginx.conf Documents.
The code is as follows:
<p> worker_processes 4;
Configure PHP
Next, let’s look at the PHP configuration file / etc/ php.ini Make custom settings. More specifically, in / etc/ php.ini Add the following two lines to the file.
The code is as follows:
date.timezone = “PRC”
For the sake of security, we hope that the PHP interpreter only deals with the file task of specifying the file path, rather than predicting the search for some nonexistent files. That’s what the first line above does. (lctt: the original time zone is “America / New York”. According to the domestic situation, Chinese cities under PRC or Asia should be used.)
The second line defines that the date / time correlation function in PHP uses the relevant default time zone. Use this guide to find your time zone and set it accordingly date.timezone The value of.
Testing PHP
Finally, let’s test whether nginx can handle PHP pages. Before testing, make sure to restart nginx and php-fpm.
On CentOS 7 system:
The code is as follows:
$ sudo systemctl restart php-fpm
On CentOS 6 system:
The code is as follows:
$ sudo service php-fpm restart
Create one called test.php And then write the following contents into the / usr / share / nginx / HTML directory.
The code is as follows:
Open the browser and enter http://nginx IP address of/ test.php .
If you can see the page shown above, the LEMP setting is completely successful!