LNMP Architecture (1) Architecture Introduction, MySQL Installation, PHP Installation, Nginx Introduction

1. Introduction to LNMP Architecture

LNMP stands for: Nginx+MySQL+PHP web server architecture under Linux system.

Enter image description

  • Unlike LAMP, Nginx provides web services
  • And php exists as an independent service called php-fpm
  • Nginx processes static requests directly, and dynamic requests are forwarded to php-fpm.

2. MySQL installation

2.1 Uninstall MySQL installed by binary package

Confirm the running status of the MySQL service and stop it

[root@host ~]# ps -ef | grep mysql
[root@host ~]# /etc/init.d/mysql.server status
[root@host ~]# /etc/init.d/mysql.server stop

Delete files related to MySQL installation

[root@host ~]# rm -rf /usr/local/mysql 
[root@host ~]# rm -rf /etc/init.d/mysqld 
[root@host ~]# rm -rf /data/mysql

2.2 Install MySQL

As with the previous method of installing MySQL in the LAMP environment, you can refer to the Mysql installation in the previous article LAMP Architecture and Installation Configuration .

3. PHP installation

It is different from installing PHP with LAMP, you need to open the php-fpm service.

3.1 Prepare PHP packages and users

[root@host ~]# cd /usr/local/src/
[root@host src]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz   //下载php二进制包
[root@host src]# tar zxvf php-5.6.30.tar.gz   //解压缩
[root@host  src]# useradd -s /sbin/nologin php-fpm   //创建专门账号用来运行php-fpm服务,因为在LNMP环境中,PHP是以一种服务的形式独立存在的

3.2 Uninstall the PHP compiled and installed before (if it was installed before)

[root@host ~]# cd /usr/local/src
[root@host src]# ls
[root@host src]# cd php-5.6.30
[root@host php-5.6.30]# ls
[root@host php-5.6.30]# make clean

3.3 Install PHP

[root@host php-5.6.30]# cd php-5.6.30
[root@host php-5.6.30]# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl  --with-openssl   //初始化

3.4 Troubleshooting errors during compilation

Error 1:

onfigure: error: xml2-config not found. Please check your libxml2 installation.

Solution 1:

[root@host php-5.6.30]# yum list |grep libxml2
libxml2.x86_64                          2.9.1-6.el7_2.3                @anaconda
libxml2.i686                            2.9.1-6.el7_2.3                base    
libxml2-devel.i686                      2.9.1-6.el7_2.3                base    
libxml2-devel.x86_64                    2.9.1-6.el7_2.3                base    
libxml2-python.x86_64                  2.9.1-6.el7_2.3                base    
libxml2-static.i686                    2.9.1-6.el7_2.3                base    
libxml2-static.x86_64                  2.9.1-6.el7_2.3                base    

[root@host php-5.6.30]# yum install -y libxml2 libxml2-devel     //一般只需要安裝devel的庫文件就好了

Error 2:

configure: error: Cannot find OpenSSL's <evp.h>

Solution 2:

[root@host php-5.6.30]# yum install -y openssl openssl-devel

Error 3:

configure: error: Please reinstall the libcurl distribution -
    easy.h should be in <curl-dir>/include/curl/

Solution 3:

[root@host php-5.6.30]# yum install -y libcurl libcurl-devel

Error 4:

configure: error: jpeglib.h not found.

Solution 4:

[root@host php-5.6.30]# yum install -y libjpeg libjpeg-turbo-devel 

Mistake 5:

configure: error: png.h not found.

Solution 5:

[root@host php-5.6.30]# yum install -y libpng libpng-devel

Mistake 6:

configure: error: freetype-config not found.

Solution 6:

[root@host php-5.6.30]# yum install -y freetype freetype-devel

Error 7:

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

Solution 7:

[root@host php-5.6.30]# yum install -y libmcrypt libmcrypt-devel

Error 8:

configure: error: mcrypt.h not found. Please reinstall libmcrypt.
(centos源不能安装libmcrypt-devel,由于版权的原因没有自带mcrypt的包rpm -qa|grep limcrypt limcrypt-devel,此源为rethot社区版的源)

Solution 8: Install a third-party yum source

wget http://www.atomicorp.com/installers/atomic
sh ./atomic

yum  install  php-mcrypt  libmcrypt  libmcrypt-devel

Detect, compile and install

[root@host php-5.6.30]# echo $?
0

[root@host php-5.6.30]# make 

[root@host php-5.6.30]# make install

3.5 Configuring PHP

Add configuration file

[root@host php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini

Configuration file editing

[root@host php-5.6.30]# vi /usr/local/php/etc/php-fpm.conf   //写入如下内容

[global]
#定义全局参数
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
#监听地址,也可以写:listen = 127.0.0.1::9000,本地监听,也可以监听其他IP:port
#此处格式会影响配置Nginx和PHP结合时Nginx寻址PHP的路径
listen.mode = 666
#当监听的为socket文件时该部分才生效,用于指定.sock文件的权限
user = php-fpm
group = php-fpm
#定义php-fpm服务的用户
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
#以上部分为进程相关信息

Configure startup scripts

[root@host etc]# cd /usr/local/src/php-5.6.30  //进入源码目录下来

[root@host php-5.6.30]#  cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm  //把启动脚本放到到系统配置

[root@host php-5.6.30]# chmod 755 /etc/init.d/php-fpm  //修改权限

[root@host php-5.6.30]# chkconfig --add php-fpm  //添加到开机启动项

[root@host php-5.6.30]# chkconfig php-fpm on  //设置开机启动

[root@host php-5.6.30]# service php-fpm start  //启动php-fpm服务
Starting php-fpm  done

[root@host php-5.6.30]# ps aux |grep php-fpm  //查看后台www的pool是否启动

4. Introduction to Nginx

Nginx is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server distributed under a BSD-like protocol. Its characteristics are that it occupies less memory and has strong concurrency capabilities. In fact, the concurrency capabilities of nginx are indeed better among web servers of the same type. Users of nginx websites in mainland China include: Baidu, JD.com, Sina, NetEase, Tencent, Taobao, etc.

  • Nginx official website nginx.org, the latest version 1.13, the latest stable version 1.12
  • Nginx application scenarios: web services, reverse proxy, load balancing
  • The famous branch of Nginx, Taobao's Tengine developed based on Nginx, is consistent with Nginx in use, the service name and configuration file name are the same, the biggest difference from Nginx is that Tenging adds some customized modules, which are outstanding in terms of safe speed limit. Support for js, css merging
  • Nginx core + lua-related components and modules form a high-performance web container openresty that supports lua.

1. Advantages of Nginx Nginx is designed as a working mode of one main process and multiple worker processes, each process is a single thread to handle multiple connections, and each worker process uses non-blocking I/O to handle multiple connections, thereby reducing Therefore, in the production environment, the CPU will be bound to the Nginx worker process to improve its performance; in addition, due to the characteristics of the single-threaded working mode, the memory usage is very small. . Nginx changes the configuration and restarts very quickly, in milliseconds, and supports upgrading the Nginx version and dynamically reloading the Nginx configuration without stopping Nginx. There are also a lot of Nginx modules, and their functions are also very powerful. Not only can they be used as HTTP load balancing, but Nginx version 1.9.0 also supports TCP load balancing, and can also easily implement functions such as content caching, web server, reverse proxy, and access control. .

2. Advantages of Lua Lua is a lightweight, embeddable scripting language, which can be easily embedded in other languages. In addition, Lua provides coroutine concurrency, that is, asynchronous execution in the form of synchronous calls to achieve concurrency. Compared with the concurrency of the callback mechanism, the code is easier to write and understand, and it is easier to troubleshoot problems. Lua also provides a closure mechanism, functions can be passed as First Class Value parameters, and it implements mark-to-clear garbage collection. Because Lua is small and lightweight, you can embed a Lua VM in Nginx, create a VM when the request is made, and recycle the VM when the request ends.

3. What is ngx_lua ngx_lua is a module of Nginx, which embeds Lua into Nginx, so that Lua can be used to write scripts, so that application scripts can be written in Lua and deployed to Nginx to run, that is, Nginx becomes a web container ; so that developers can use the Lua language to develop high-performance Web applications. ngx_lua provides many APIs for interacting with Nginx. For developers, they only need to learn these APIs to develop functions. For developing web applications, if they have contacted Servlet, their development is similar to Servlet, nothing more It is to know what the API looks like in the steps of receiving requests, parsing parameters, processing functions, and returning responses.

4. Development environment We can use OpenResty to build a development environment. OpenResty packages Nginx core, LuaJIT, many useful Lua libraries and Nginx third-party modules together; in this way, developers only need to install OpenResty, and do not need to understand Nginx core and write complex The C/C++ module can be used, and you only need to use the Lua language for Web application development.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325364980&siteId=291194637