Detailed configuration of LEMP environment construction in CentOS 7

Source: Internet
Author: User
Tags anonymous fpm install php php and web services centos mysql database

Recently, due to project requirements, the server has been upgraded from CentOS6 to CentOS7, and the corresponding PHP version has also been upgraded to PHP5.6. We are familiar with the one-click installation package of the LEMP environment. However, in this article, we will install each component module separately and build a complete PHP running platform.
We often say that the LNMP environment refers to the Linux/nginx/MySQL/PHP combination. What is LEMP? In fact, Nginx is pronounced Engine-X = E. The LEMP package is composed of Linux, nginx, MariaDB/MySQL, and PHP. It seems that LEMP and LNMP are the same, the industry is often referred to as LEMP. MariaDB is a branch of the community-driven MySQL database. Its features provide better performance. Therefore, we have installed MariaDB under CentOS7. I have installed CentOS7. Now I only need to install Nginx, MariaDB, and PHP.
1. Install Nginx
We will install a pre-built stable version of nginx package from the official nginx RPM source.
$ 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
In this way, nginx is installed, and nginx will not be started automatically after installation. Now we need to make nginx start automatically, and also make some configuration so that it can start with the operating system. We also need to open TCP/80 ports in the firewall to allow remote access to nginx web services. For all these operations and settings, you only need to enter the following command.
$ Sudo systemctl start nginx
$ Sudo systemctl enable nginx
$ Sudo firewall-cmd -- zone = public -- add-port = 80/tcp -- permanent
$ Sudo firewall-cmd -- reload
Nginx has been started. Now we will test nginx. The default document directory for nginx under CentOS7 is/usr/share/nginx/html. The default index.html file must already be in this directory. Let's check whether we can access this test web page and enter the http: // nginx IP address/access.
Nginx
2. Install MariaDB/MySQL
CentOS/RHEL 7 uses MariaDB instead of the default MySQL. As a simple alternative to MySQL, MariaDB ensures maximum compatibility with MySQL APIs and command line usage. The following is an example of how to install and configure MaraDB/MySQL on CentOS7.
$ Sudo yum install mariadb-server
$ Sudo systemctl start mariadb
$ Sudo systemctl enable mariadb
After the MariaDB/MySQL service is successfully started, you must configure database security, such as setting (not empty) the root password, deleting anonymous users, and locking remote access. Run the following code:
$ Sudo mysql_secure_installation
Set the root password and delete anonymous users as prompted.
3. Install PHP
PHP is an important component in the LEMP package. It is responsible for extracting data stored in the MariaDB/MySQL server to generate dynamic content. For LEMP, you need to install at least two modules, PHP-FPM and PHP-MySQL. The PHP-FPM (FastCGI Process Manager) implements access interfaces for nginx servers and PHP applications that generate dynamic content. The PHP-MySQL module enables PHP programs to access the MariaDB/MySQL database.
First, check the currently installed PHP package.
Yum list installed | grep php
If you have installed PHP packages, delete them first.
Add source package for yum installation.
Rpm-Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
Rpm-Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Run yum install.
Use the yum command to customize the PHP engine and install some PHP extension module packages.
Yum install php56w. x86_64 php56w-cli.x86_64 php56w-common.x86_64 php56w-gd.x86_64 php56w-ldap.x86_64 php56w-mbstring.x86_64 php56w-mcrypt.x86_64 php56w-mysql.x86_64
Then install php fpm.
Yum install php56w-fpm
Finally, start PHP-FPM
$ Sudo systemctl start php-fpm
$ Sudo systemctl enable php-fpm
4. Configure LEMP to enable Nginx to support PHP
First, disable the httpd service installed with the PHP package.
$ Sudo systemctl disable httpd
Next, configure the nginx virtual host so that nginx can handle PHP tasks through the PHP-FPM. Open/etc/nginx/conf. d/default. conf in the text editor and modify it as follows.
Server {
Listen 80;
Server_name localhost;
Root/usr/share/nginx/html;
Index. php index.html index.htm;
 
# Charset KOI8-R;
# Access_log/var/log/nginx/log/host. access. log main;
 
Location /{
    }
 
# Error_page 404/404 .html;
 
# Redirect server error pages to the static page/50x.html
    #
Error_page 500 502 503 x.html;
Location =/50x.html {
    }
 
# Proxy the PHP scripts to Apache listening on 127.0.0.1: 80
    #
# Location ~ \. Php $ {
# Proxy_pass http: // 127.0.0.1;
#}
 
# Pass the PHP scripts to FastCGI server listening on Fig: 9000
    #
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;
    }
}

Then, configure PHP and modify/etc/php. ini.

Cgi. fix_pathinfo = 1
Date. timezone = PRC
Finally, test whether nginx can process PHP pages. Before testing, make sure to restart nginx and PHP-FPM.
$ Sudo systemctl restart nginx
$ Sudo systemctl restart php-fpm
Create a file named test. php and write the following content into the/usr/share/nginx/html/directory.
<? Php
Phpinfo ();
?>
Open your browser and enter the http: // nginx IP address/test. php. LEMP installation is complete.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.