A detailed tutorial on Lemp environment under CentOS 7.x _linux

Source: Internet
Author: User
Tags anonymous fpm install php

Recently, due to project requirements, the server upgraded from CentOS6 to CentOS7, and the corresponding PHP version was upgraded to PHP5.6. We are familiar with the LEMP environment one-click installation package, but this article we will install each module separately, and build a complete PHP operating platform.

We often say that the LNMP environment refers to the linux/nginx/mysql/php combination, and lemp is what? In fact, Nginx's pronunciation is Engine-x = E,lemp package is composed of Linux, Nginx, Mariadb/mysql and PHP, then it seems that lemp and Lnmp are the same, and now the industry habitually called Lemp. MARIADB is a branch of the community-driven MySQL database that features more performance, so we install mariadb under CentOS7. CentOS7 I've installed it now, just install NGINX,MARIADB and PHP.

1. Installation Nginx

We install a pre-built, stable version of the Nginx package from Nginx's official 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, the Nginx is installed and the Nginx does not start automatically when the installation is complete. What needs to be done now is to get Nginx to start automatically, and to configure it to start as the operating system starts. We also need to open the TCP/80 port in the firewall so that the Nginx Web service can be accessed remotely. All of these actions and settings are implemented only if you want to enter the following command.

$ sudo systemctl start nginx 
$ sudo systemctl enable Nginx 
$ sudo firewall-cmd--zone=public--add-port=80/tcp Ermanent 
$ sudo firewall-cmd--reload

Nginx has started, now to test the nginx. Nginx CentOS7 The default document to directory is/usr/share/nginx/html. The default index.html file must already be in this directory. Let's check if we can access this test Web page and enter Http://nginx IP address/access.

2. Installation Mariadb/mysql

Centos/rhel 7 uses the MARIADB instead of the default MySQL. As a simple replacement for MySQL, MARIADB guarantees the most compatibility with MySQL's API and command-line usage. Here is an example of how to install and configure Maradb/mysql on a CentOS7.

$ sudo yum install mariadb-server 
$ sudo systemctl start mariadb 
$ sudo systemctl enable mariadb

After the Mariadb/mysql service is successfully started, the security configuration of the database is also performed, such as setting (Non-empty) root password, deleting anonymous users, and locking remote access. Execute the following code:

$ sudo mysql_secure_installation

Follow the prompts to set the root password and remove anonymous users.

3. Install PHP

PHP is an important component in the Lemp package, which is responsible for taking out the data stored in the Mariadb/mysql server to generate dynamic content. For Lemp needs, you need to install at least PHP-FPM and php-mysql two modules. PHP-FPM (FastCGI Process Manager) implements the access interface 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 there are installed PHP packages, delete them first.

Add source packs to the 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 expansion module packs.

Yum install php56w.x86_64 php56w-cli.x86_64 php56w-common.x86_64 php56w-gd.x86_64 php56w-mbstring.x86_64 php56w-mcrypt.x86_64 php56w-mysql.x86_64 php56w-pdo.x86_64

Then install the PHP FPM.

Yum Install PHP56W-FPM

Finally, start PHP-FPM

$ sudo systemctl start php-fpm 
$ sudo systemctl enable PHP-FPM

4, configure Lemp, let Nginx support PHP

First, disable the HTTPD service that is installed with the PHP package.

$ sudo systemctl disable httpd

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

server { 
listen; 
server_name localhost; 
root/usr/share/nginx/html; 
Index 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 502 503 504/50x.html; 
Location =/50x.html { 
} 
# Proxy The PHP scripts to Apache listening on 127.0.0.1:80 
# 
#location ~ \.P hp$ { 
# proxy_pass http://127.0.0.1; 
# Pass the 
PHP scripts to FastCGI server listening on 127.0.0.1:9000 
# 
Location ~ \.php$ { 
Try_file s $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 to modify the/etc/php.ini.

Cgi.fix_pathinfo=1 
Date.timezone = PRC

Finally, test whether the Nginx can process PHP pages. Be sure to restart Nginx and PHP-FPM before testing.

$ sudo systemctl restart Nginx 
$ sudo systemctl restart PHP-FPM

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

<?php 
phpinfo (); 
? >

Open the browser and enter the Http://nginx IP address/test.php. See the following interface Lemp installation completed.

The above is a small set to introduce the CentOS 7.x under the lemp environment to build a detailed tutorial, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.