LNMP Environment Construction

Source: Internet
Author: User
Tags configuration php gpg web hosting

Building LNMP environment is mainly for the purpose of practicing PHP, but also to build their own personal website to prepare.

First of all, my environment is CentOS 7.0.1406, kernel 3.10.0-123.el7.x86_64,nginx 1.10.1, Mysql 5.7.13, PHP 7.0.8.

Reference article:

(1) http://www.lnmp.cn/installing-php7-mysql57-nginx18-under-centos7.html

(2) http://www.osyunwei.com/archives/2353.html

(3) http://5323197.blog.51cto.com/5313197/1295151

(4) http://nginx.org/en/docs/configure.html

(5) http://dev.mysql.com/doc/refman/5.7/en/linux-installation-rpm.html

First, install Nginx

1. Update Nginx Source

(1) The Yum Library in CentOS does not provide nginx source by default, download and update the package of the Yum Library from Nginx official website, install.

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

rpm -IVH nginx-release-centos-7-0.el7.ngx.noarch.rpm

(2) then download GPG digital signature from official website (This step can be omitted, this record is only to learn how to use GPG signature)

wget Http://nginx.org/keys/nginx_signing.key

Import the GPG digital signature file:

Rpmkeys--import Nginx_signing.key

Then modify the following entry in the/etc/yum.repos.d/nginx.repo file:

Gpgcheck=1

(4) After this use Nginx.repo this Yum library to install the software will check the software GPG signature. The package you just downloaded to update the Yum Library, or use the following command to check its GPG signature:

rpmkeys-k nginx-release-centos-7-0.el7.ngx.noarch.rpm

2. Installation

(1) using Yum install Nginx to find the latest version is 1.6.3, so download the latest stable version of 1.10.1 from the official website.

Before installing Nginx, install some dependent libraries. Install these three base libraries directly with Yum: Pcre,openssl,zlib.

Get Source:

wget http://nginx.org/download/nginx-1.10.1.tar.gz

Unzip, then the source code is compiled and installed (Configure;make;make install) Nginx. Compile option Reference http://nginx.org/en/docs/configure.html, if there is an error, follow the compilation error prompt.

Detects if the installation was successful, finds execution files in the installation directory, runs. with PS aux | grep Nginx to see if the service is running.

Configure Nginx service Management.

Vim/usr/lib/systemd/system/nginx.service

Enter the following:

[Unit]

Description=nginx-high Performance Web Server

documentation=http://nginx.org/en/docs/

After=network.target Remote-fs.target Nss-lookup.target

[Service]

Type=forking

Pidfile=/var/run/nginx.pid

Execstartpre=/usr/sbin/nginx-t-c/etc/nginx/nginx.conf

Execstart=/usr/sbin/nginx-c/etc/nginx/nginx.conf

Execreload=/bin/kill-s HUP $MAINPID

Execstop=/bin/kill-s QUIT $MAINPID

Privatetmp=true

(Note: The Nginx.pid file in the above configuration file, the path of the Nginx executable and the nginx.conf configuration file to be modified to the path set by the installation itself)

(2) Turn on boot

Systemctl Enable Nginx.service

Then you can use SYSTEMCTL to manage the Nginx service (Start,stop,reload,status,restart and other commands)

After you start the Nginx service, enter the server address in the browser to verify success. See the following display, stating that the Nginx service is operating normally.

Second, install MySQL

1. Download the MySQL CentOS7 series Community rpm package to the website and install the required components with the RPM command (see the installation instructions for the official website)

2. configuration file in/etc/my.cnf, note that the Socket=/var/lib/mysql/mysql.sock in the file will be used in the subsequent configuration.

3. Because RPM is installed, the default is to use SYSTEMCTL to manage Services (service commands are not available), start the database service, check the status, and see if it starts properly.

4. MySQL 5.7 and the previous version of the big difference is that after the installation will automatically for the [email protected] User set a random initial password, the previous version password is empty.

Use the following command to get this random password from the log file.

grep ' temporary password '/var/log/mysqld.log

Log in to the server and modify the root password.

ALTER USER ' root ' @ ' localhost ' identified by ' newpasswd ' 

(Note that password requirements are strictly at least lowercase, uppercase, numeric, special characters, more than 8 digits).

Third, install PHP

PHP 7 was launched at the end of 2015, PHP officially said twice times faster than PHP 5, for this, this fresh must taste. However, one notable point is that although PHP 7 adds a lot of new features, there are many places that are backwards incompatible,

For example, the MySQL extension has been removed in PHP 7. These backwards incompatibilities cause many programs to run in PHP 7, such as Discuz. But there is no need to be particularly concerned, because we can install multiple versions of PHP on the same server.

The installation steps are as follows:

Download source code to/USR/LOCAL/SRC

Cd/usr/local/src

Wget-c http://cn2.php.net/distributions/php-7.0.8.tar.gz

Unzip and then enter the directory:

TAR-ZXVF php-7.0.5.tar.gz

CD php-7.0.5/

Install the software modules that will be used to prepare the extensions before installing

Yum-y Install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel cur L Curl-devel OpenSSL Openssl-devel

Configure

./configure--prefix=/usr/local/php7--enable-fpm--with-fpm-user=nginx--with-fpm-group=nginx--with-mysqli-- With-zlib--with-curl--WITH-GD--with-jpeg-dir

--with-png-dir--with-freetype-dir--with-openssl--enable-mbstring--enable-xml--enable-session--enable-ftp-- Enable-pdo-enable-tokenizer--enable-zip

As mentioned above, PHP 7 has removed the MySQL extension, so-with-mysql is no longer a valid option. This is replaced with mysqli or PDO.

Make;make Install;

Make (this will wait for a longer time)

Make install

Iv. Configuration

Before configuring, the user is described in the nginx running user, php-fpm running user, MySQL running the relationship. Refer to http://www.ilanni.com/?p=7438.

1. Description

Nginx itself cannot handle PHP, it is just a Web server. When a client request is received, if it is a PHP request, it is forwarded to the PHP interpreter for processing and returns the result to the client. If it is a static page, Nginx handles it itself and returns the result to the client.

The most used PHP interpreter under Nginx is fastcgi. In general, the PHP request forwarded to the FASTCGI management process processing, the FASTCGI management process to select the CGI sub-process processing, and then return the processing results to nginx.

In this process involves two users, one is Nginx running user, one is php-fpm running user. If you are accessing a static file, you only need the user who is running nginx to have read or write access to the file.

And if the access is a PHP file, you first need to run the user to the file has Read permissions, read to the file after the discovery is a PHP file, then forwarded to PHP-FPM, at this time you need to PHP-FPM users have read access to the file or read and write permissions.

2. Operation

with PS aux | grep nginx View Nginx running user, know, and configuration file nginx.conf configuration consistent (not configured by default is nobody this user)

Using PS aux |grep php-fpm to view PHP-FPM's running user, its corresponding profile is www.conf.

What we need to do now is to unify Nginx and php-fpm running user as nobody (through the above two configuration files, notice to restart the PHP-FPM service after the modification, reload nginx configuration file), Then the Nginx web hosting site root directory for nobody users and nobody user groups have all permissions.

3. Experience

In the actual production environment, our general configuration is Nginx and PHP-FPM are running under the nobody user, and the root directory of the site also belongs to nobody users, and the root directory for the nobody user has all permissions.

This configuration is most secure because the nobody user is the safest. Even if hackers break the site, they cannot log on to the system.

4.php Configuration

First, the PHP configuration document

[email protected] php-7.0.8]# CP Php.ini-development/usr/local/php7/lib/php.ini

The php.ini path should be placed in the Prefix/lib folder unless modified by this option at the time of installation

--with-config-file-path=path

If you do not specify--prefix when installing PHP, then the php.ini path is/usr/local/lib/php.ini. The installation has been specified--prefix, so it is/usr/local/php7/lib/php.ini.

Then modify the php.ini according to your actual needs.

[Email protected] ~]# Vim/usr/local/php7/lib/php.ini

Find Mysqli.default_socket, modified to:

Mysqli.default_socket =/var/lib/mysql/mysql.sock

One of the/var/lib/mysql/mysql.sock is mentioned above when installing MySQL. This value must be filled in, otherwise the following error will occur:
Warning:mysqli_connect (): (hy000/2002): No such file or directory

Modify the time zone, look for Date.timezone, change (mainly the front; To remove, this is used for annotation):

Date.timezone = PRC

Okay, PHP 7 is already installed, so let's verify

[Email protected] php-7.0.8]#/usr/local/php7/bin/php-v

PHP 7.0.8 (CLI) (Built:jul 06:59:19) (NTS)
Copyright (c) 1997-2016 the PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

Then look at the modules that have been installed

[Email protected] php-7.0.8]#/usr/local/php7/bin/php-m
[PHP Modules]
Core
CType
Curl
Date
Dom
FileInfo
Filter
Ftp
Gd
Hash
Iconv
Json
Libxml
Mbstring
Mysqli
Mysqlnd
Openssl
Pcre
Pdo
Pdo_sqlite
Phar
Posix
Reflection
Session
SimpleXML
Spl
Sqlite3
Standard
Tokenizer
Xml
XmlReader
XmlWriter
Zip
Zlib
[Zend Modules]

5. Configure PHP-FPM

Configuration document for copy PHP-FPM

[Email protected] php-7.0.8]# cp/usr/local/php7/etc/php-fpm.conf.default/usr/local/php7/etc/php-fpm.conf
[Email protected] php-7.0.8]# cp/usr/local/php7/etc/php-fpm.d/www.conf.default/usr/local/php7/etc/php-fpm.d/ Www.conf

One of the www.conf should pay attention to the following value

Listen = 127.0.0.1:9000

9000 ports are used here, this option is used when configuring Nginx Web site.

Configuring the PHP-FPM Startup service script

[email protected] php-7.0.8]# CP sapi/fpm/php-fpm.service/usr/lib/systemd/system/

Modify the startup script to replace the contents of the inside prefix with the actual path

[Email protected] php-7.0.8]# Vim/usr/lib/systemd/system/php-fpm.service

Will

Pidfile=${prefix}/var/run/php-fpm.pid
EXECSTART=${EXEC_PREFIX}/SBIN/PHP-FPM--nodaemonize--fpm-config ${prefix}/etc/php-fpm.conf

Modified into

Pidfile=/usr/local/php7/var/run/php-fpm.pid
EXECSTART=/USR/LOCAL/PHP7/SBIN/PHP-FPM--nodaemonize--fpm-config/usr/local/php7/etc/php-fpm.conf

Re-loading SYSTEMD

[Email protected] php-7.0.8]# Systemctl daemon-reload

Let php-fpm start randomly.

[Email protected] php-7.0.8]# Systemctl enable PHP-FPM
Ln-s '/usr/lib/systemd/system/php-fpm.service '/etc/systemd/system/multi-user.target.wants/php-fpm.service '

Start PHP-FPM Now

[Email protected] php-7.0.8]# systemctl start php-fpm

View status

[Email protected] php-7.0.5]# systemctl status php-fpm
Php-fpm.service-the PHP FastCGI Process Manager
Loaded:loaded (/usr/lib/systemd/system/php-fpm.service; enabled)
Active:active (running) since Fri 2016-04-08 00:50:54 CST; 3s ago
Main pid:13609 (PHP-FPM)
CGroup:/system.slice/php-fpm.service
├─13609 Php-fpm:master Process (/usr/local/php7/etc/php-fpm.conf)
├─13610 Php-fpm:pool www
└─13611 Php-fpm:pool www
APR 00:50:54 lnmp.cn systemd[1]: Started the PHP FastCGI Process Manager.

PHP-FPM has been successfully started.

6. Configure nginx.conf

(1) Index index.php index.html index.htm; #增加index. php

(2) Uncomment the FASTCGI Server section location, and note the parameters of the Fastcgi_param line, change to $document_root$fastcgi_script_name, or use the absolute path

7. Testing

Enter Nginx Default Web site root directory (ref.:/usr/local/nginx/html)

VI index.php #新建index. php file

Input

<?php
Phpinfo ();

: wq! #保存

Chown nobody:nobody/usr/share/nginx/html/-R #设置目录所有者

In the client browser enter the server IP address, you can see the relevant configuration information!

  

The configuration was successful.

8. Configure the Web site (there are also questions ...). )

First set up a LNMP site, the path is/www/lnmp/web

[Email protected] php-7.0.8]# mkdir-p/www/lnmp/web

and prepare the Phpinfo test file.

[Email protected] php-7.0.8]# vim/www/lnmp/web/phpinfo.php

Enter the following to save:

<?php
Phpinfo ();

One Nginx configuration file for each site to be placed in/usr/local/nginx/conf/

[Email protected] php-7.0.8]# cd/usr/local/nginx/conf/
[Email protected] conf]# vim myphp.com.conf

Add the following to the myphp.com.conf and save

Server {
    listen      ;
    server_name  www.myphp.com;
    root        /www/lnmp/web;
    location/{
        index  index.php index.html index.htm;
   }
    Location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  script_filename  $document _root$fastcgi_ Script_name;
        include        Fastcgi_params;
   }
}

Among them server_name www.myphp.com; Change the www.myphp.com into your own domain name.
Among them root/www/lnmp/web; is the site directory you just created
Among them Fastcgi_pass 127.0.0.1:9000; Is the above configuration php-fpm mentioned to pay attention to the value

After modifying the configuration, be sure to remember reload nginx to take effect

[Email protected] conf]# Systemctl Reload Nginx

OK, you're done, just open the domain name verification (if the domain name is already pointing to the configured server IP: The Hosts file on Windows sets the domain name map to the server IP)

Browser input: http://www.myphp.com/phpinfo.php See if you can get the configuration information page for the 7 test.

LNMP 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.