CentOS7.2 on Nginx PHP Mariadb Environment Construction

Source: Internet
Author: User
Tags fpm mysql version openssl library php error php script php source code phpinfo zend

Recently there is a Erp+php-web test page environment deployment needs, need to deploy nginx+php cgi+socket mode, MySQL environment. To see if the service port is up, we are accustomed to using the netstat command to view it, so if you do not have Netstat installed in your system, you can install the Yum install-y net-tools directly


Lab Environment:

Linux version: CentOS 7.2

Nginx Version: nginx-1.10.1

PHP Version: php-5.6.5

MySQL version: MariaDB-5.5.52


First, the deployment of Nginx

Put the dependency on the bag and finish it.

#yum install-y openssl-devel, Zlib-devel ' Pcre-devel


The program by default is run with nobody identity, we use Nginx user to run, first add Nginx group and user, do not create home directory, not allow login system

#groupadd Nginx

#useradd-M-s/sbin/nologin-g nginx


The installation Nginx takes nginx-1.10.1.tar.gz

# Tar XF nginx-1.10.1.tar.gz

# CD nginx-1.10.1


Specify the owner and the group to be used for the installation of the recording and running, and activate the state control module, etc.

#./configure \

--prefix=/usr/local/nginx \

--pid-path=/usr/local/nginx/logs/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--user=nginx \

--group=nginx \

--with-http_ssl_module \

--with-http_flv_module \

--with-http_stub_status_module \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/tmp/nginx/client/\

--http-proxy-temp-path=/var/tmp/nginx/proxy/\

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/\

--HTTP-UWSGI-TEMP-PATH=/VAR/TMP/NGINX/UWSGI \

--HTTP-SCGI-TEMP-PATH=/VAR/TMP/NGINX/SCGI \

--with-pcre


Approximate configuration results

Configuration Summary

+ Using System PCRE Library

+ Using System OpenSSL Library

+ md5:using OpenSSL Library

+ sha1:using OpenSSL Library

+ Using System zlib Library


Nginx path prefix: "/usr/local/nginx"

Nginx binary file: "/usr/local/nginx/sbin/nginx"

Nginx modules path: "/usr/local/nginx/modules"

Nginx configuration prefix: "/usr/local/nginx/conf"

Nginx configuration file: "/usr/local/nginx/conf/nginx.conf"

Nginx pid file: "/var/run/nginx/nginx.pid"

Nginx error log file: "/usr/local/nginx/logs/error.log"

Nginx HTTP access log file: "/usr/local/nginx/logs/access.log"

Nginx HTTP client request body Temporary files: "/var/tmp/nginx/client/"

Nginx HTTP proxy temporary files: "/var/tmp/nginx/proxy/"

Nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/"

Nginx http Uwsgi temporary files: "/var/tmp/nginx/uwsgi"

Nginx http scgi temporary files: "/var/tmp/nginx/scgi"


When the above configuration is complete, you can edit the installation

#make && make Install

#mkdir/var/tmp/nginx/client/-PV


After the compilation and installation is completed in the/usr/local will appear in the Nginx directory, after entering this directory is very simple to find the directory.

Its configuration file is stored in the Conf directory, the Web page file is stored in HTML, the log file is stored in the logs,

There is only one executable program "Nginx" under the Sbin directory


Second, the deployment of PHP environment

A. fastcgi is a scalable, high-speed interface for communicating between HTTP server and dynamic scripting languages

B. Nginx is a lightweight HTTP server, you must use a third-party fastcgi processor to be able to parse PHP

C. PHP-FPM is a third-party fastcgi process Manager, which is developed as a patch for PHP, and it needs to be compiled along with the PHP source code at the time of installation.

This means that PHP-FPM is compiled into the PHP kernel, so it is better at handling performance, and it is much better at handling high concurrency than the spawn-fcgi engine.

Therefore, it is recommended that nginx+php/php-fpm this combination to parse PHP.


Start by installing some of the PHP dependencies

GCC gcc-c++ libxml2 libxml2-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib Zlib-dev El glibc glibc-devel glib2 glib2-devel


And then decompress the loaded tar packets.

# TAR-XVZF Php-5.6.5.tar.gz


Into the php-5.6.5 of the decompression.

# CD php-5.6.5

#./configure--prefix=/usr/local/php–enable-fpm–enable-mbstring–with-mysql=mysqlnd–with-mysqli=mysqlnd– With-pdo-mysql=mysqlnd

# Make && make install


View PHP Version

# php-v

PHP 5.6.5 (CLI) (Built:jan 10 2017 03:53:13)

Copyright (c) 1997-2014 the PHP Group

Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies


If not, then directly in the PHP executable file to make a link to/usr/bin/php or copy one to/usr/bin/below to view the PHP version


#ln-S/usr/local/php/bin/php/usr/bin/php

Or

#cp-raf/usr/local/php/bin/php/usr/bin/php


PHP configuration file php.ini, copy an installation directory php.ini-development this profile template to php directory

# Cp-raf Php-5.6.5/php.ini-development/usr/local/php/lib/php.ini


Of course, we also need to configure the PHP-FPM, in the installation of PHP, we have generated a configuration template for us, in/usr/local/php/etc/ Php-fpm.conf.default, so just copy this template and renamed to php-fpm.conf on it, go in and change the daemonize to Yes, back to the running mode of service start. Then pinch, Whereis php-fpm,

This is an executable file that executes the PHP-FPM directly and then netstat-an| grep php-fpm, see, sure enough, port number No. 9000 is listening.

#cp-raf/usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf

# Whereis PHP-FPM


Configuration and Optimization php-fpm

The global configuration file for PHP is php.ini, and in the above steps, the file has been copied to/usr/local/php/lib/php.ini. Depending on the needs of each application, the php.ini can be configured accordingly.

The following highlights the configuration files for the PHP-FPM engine.

The default configuration file for PHP-FPM is/usr/local/php/etc/php-fpm.conf, based on the installation path specified above.

PHP-FPM.CONF is an XML-formatted plain text file whose contents are easily understood. Here are a few key configuration tags:

The label listen_address is the IP address and port that the fastcgi process listens on, the default is 127.0.0.1:9000, the port can be changed

<value name= "Listen_address" >127.0.0.1:9000</value>

The label display_errors is used to set whether to display PHP error message, default is 0, do not display error message, set to 1 can display PHP error message.

<value name= "Display_errors" >0</value>

The tags user and group are used to set up the users and user groups that run the fastcgi process. Note that the users and user groups specified here are consistent with the users and user groups specified in the Nginx configuration file.

<value name= "User" >nobody</value>

<value name= "group" >nobody</value>

Label Max_children The number of processes used to set fastcgi. According to the official recommendation, less than 2GB of memory server, can only open 64 processes, more than 4GB of memory server can open 200 processes.

<value name= "Max_children" >5</value>

The label request_terminate_timeout is used to set the time fastcgi executes the script. The default is 0s, which is infinite execution, and can be modified according to the situation.

<value name= "Request_terminate_timeout" >0s</value>

The label rlimit_files is used to set the PHP-FPM limit on open file descriptors, which is 1024 by default. The value of this tag must be associated with the number of open files in the Linux kernel, for example to set this value to 65535.

You must execute ' ULIMIT-HSN 65536 ' on the Linux command line.

<value name= "Rlimit_files" >1024</value>

The label max_requests indicates how many requests per children are processed and is turned off, with the default setting of 500.

<value name= "Max_requests" >500</value>

The label allowed_clients is used to set the IP address that allows access to the FASTCGI process resolver. If you do not specify an IP address here, the PHP parsing request sent by Nginx will not be accepted.

<value name= "Allowed_clients" >127.0.0.1</value>


Manage the fastcgi process, you can start the fastcgi process after you have configured PHP-FPM

/usr/local/php/sbin/php-fpm


After the fastcgi process starts, its listening IP address and port are also started and can be viewed via PS and netstat


Because Nginx itself does not parse PHP, so to implement Nginx support for PHP, in fact, the PHP page request to the FASTCGI process to listen to the IP address and port.

If the PHP-FPM as a dynamic application server, then Nginx is actually a reverse proxy server. Nginx through the reverse proxy function to achieve the parsing of PHP, which is the nginx implementation of PHP dynamic parsing principle


The path to the Nginx configuration file is/usr/local/nginx/conf/nginx.conf. Below is a virtual host configuration instance that supports PHP parsing under Nginx

# vi/usr/local/nginx/conf/nginx.conf


The fastcgi_param directive specifies the home directory where the PHP dynamic program is placed, that is, the path specified in the $fastcgi_script_name, which is the/usr/local/nginx/html directory

The Fastcgi_params file is a parameter configuration file for the fastcgi process, and after installing Nginx, a file such as this is generated by default, which is included in the fastcgi parameter configuration file via the include directive.


After the configuration is completed nginx+fastcgi, in order to ensure the high-speed and stable operation of the PHP environment, we need to add some fastcgi optimization instructions. An example of optimization is given below,

Add the following code to the HTTP level in the Nginx master configuration file

Location ~ \.php$ {

root HTML;

Fastcgi_pass 127.0.0.1:9000;

Fastcgi_index index.php;

Fastcgi_param script_filename $document _root$fastcgi_script_name;

Include Fastcgi_params;

# Fastcgi_cache_path/usr/local/nginx/fastcgi_cache Levels=1:2 keys_zone=test:10m inactive=5m;

Fastcgi_connect_timeout 300;

Fastcgi_send_timeout 300;

Fastcgi_read_timeout 300;

Fastcgi_buffer_size 64k;

Fastcgi_buffers 4 64k;

Fastcgi_busy_buffers_size 128k;

Fastcgi_temp_file_write_size 128k;

# Fastcgi_cache TEST;

Fastcgi_cache_valid 302 1h;

Fastcgi_cache_valid 301 1d;

Fastcgi_cache_valid any 1m;

}


The following is an introduction to the meaning of the above code.

The first line of code is to specify a file path for the fastcgi cache, a directory structure level, a keyword area storage time, and an inactive delete time.

FASTCGI_CONNECT_TIMEOUT Specifies the time-out to connect to the backend fastcgi.

FASTCGI_SEND_TIMEOUT Specifies the time-out for sending requests to fastcgi, which is the time-out for sending requests to fastcgi after two handshake has been completed.

The fastcgi_read_timeout specifies the time-out for receiving a fastcgi answer, which is the time-out for receiving fastcgi answers after two handshakes have been completed.

The fastcgi_buffer_size is used to specify how much buffer is required for the first part of the read fastcgi answer, which indicates that the first part (the answer header) that will use 1 64KB of buffer read answer, can be set to Fastcgi_ The buffers option specifies the buffer size.

Fastcgi_buffers specifies how many and how large buffers are needed locally to buffer the fastcgi response request. If a PHP script produces a page size of 256KB, it is allocated a buffer of 4 64KB to cache, if the page size is greater than 256KB, then the portion greater than 256KB will be cached in the path specified by fastcgi_temp, but this is not a good method, Because the data in memory is processed faster than the hard disk. Generally this value should be the site PHP script generated by the middle of the page size, if most of the site script generated by the page size of 256KB, then you can set this value to "16k", "4 64k" and so on.

The default value for Fastcgi_busy_buffers_size is twice times that of Fastcgi_buffers.

Fastcgi_temp_file_write_size indicates how much data block is used when writing to the cache file, and the default value is twice times that of Fastcgi_buffers.

Fastcgi_cache indicates that the FASTCGI cache is turned on and assigned a name. Enabling caching is useful to reduce the load on the CPU and prevent 502 errors, but opening the cache can also cause many problems, depending on the situation.

Fastcgi_cache_valid, fastcgi is used to specify the cache time for the answer code, the values in the instance indicate that the 200 and 302 responses are cached for one hours, the 301 response cache is 1 days, and the other responses are cached for 1 minutes.


Test Nginx's parsing function on PHP


Here, create a phpinfo.php file under the/usr/local/nginx/html directory, with the following content:

<?php phpinfo ();?>

Then access http://ip/index.html through the browser, by default in the browser display "Welcome to nginx!" Indicates that Nginx is operating normally.

Then access the http://ip/phpinfo.php in the browser, if PHP can parse normally, will appear PHP installation configuration and feature List statistics.


Three. Yum Install MARIADB Database

Delete all mysql/mariadb-related RPM packages

MySQL is no longer included in the CentOS 7 source, but instead MariaDB;

1. Using Rpm-qa | grep MariaDB searches for MariaDB existing packages:

If present, use RPM-E--nodeps mariadb-* to remove all


2. Using Rpm-qa | grep MySQL search mysql existing package:

If present, use yum remove MySQL mysql-server mysql-libs compat-mysql all deleted;


I recommend using the system comes with the mariadb, so the direct Yum installation is more convenient

3. Yum Installation mariadb

# yum Install-y mariadb Mariadb-server

Note: MARIADB is a database client and Mariadb-server is the database server side


4. command to start MARIADB service

# Systemctl Start mariadb


5. Check if the MARIADB process service is running

# Ps-ef | grep mariadb

Root 10131 10095 0 19:24 pts/2 00:00:00 grep--color=auto mariadb

MySQL 10197 10009 0 Jan19? 15:56:01/usr/sbin/mysqld--basedir=/usr--datadir=/var/lib/mysql--plugin-dir=/usr/lib64/mysql/plugin--user=mysql --log-error=/var/log/mariadb/mariadb.log--pid-file=/var/lib/mysql/zgz.pid--socket=/var/lib/mysql/mysql.sock


6. View the status of the repository

#systemctl Status mariadb


7. Then run mysql_secure_installation initialization configuration mariadb:

#mysql_secure_installation

At this stage the root initial password is empty, so enter it. I personally feel that you can choose to modify the settings database root password at this time, and then the next setting, except disallow root login remotely, Remove test database and accesss to it can be N, the others are Y


8, login MariaDB and create the corresponding database user and database

(1) Login with mysql-uroot-p, enter the password after entering.

(2) Create user ' git ' @ ' localhost ' identified by ' $password ', where $password fill in the password you set. Of course, can also be modified later;

(3) Setting up the storage engine

Mariadb[none]> set Storage_engine=innodb;

(4) Create a database

Mariadb[none]>create database database_name character Set UTF8;

(5) Set user permissions

Grant all privileges on * * to ' root ' @ '% ' identified by ' $password ' with option;

#上述可以定义为: The root user can remotely connect to all content in the database from any machine and has the highest privileges, and has the right to grant remote connections to others.


After the above steps, the LNMP environment even the basic construction is complete. You only need to put the application code in the Nginx HTML, and the application and database connection configuration file is set up.

This article is from the "10793382" blog, please be sure to keep this source http://10803382.blog.51cto.com/10793382/1925278

CentOS7.2 on Nginx PHP Mariadb Environment Construction

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.