一、 LNMP环境介绍

LNMP(Linux + Nginx + Mysql/Mariadb + PHP)是目前互联网公司最常用的经典Web服务环境组合。

  • Nginx:是一高性能的HTTP和反向代理服务器。
  • Mysql/Mariadb:是 一个小型关系型数据库管理系统,用于存储数据库(如用户名、密码、文章等)
  • PHP:是一种在服务器端执行的嵌入HTML文档的脚本语言。

这3个软件均为免费开源软件,组合在一起跑在Linux系统之上,成为一个免费、高效、扩展性强的网站服务系统。

LNMP组合工作流程:

  • 用户通过浏览器输入域名请求Nginx web服务,如果请求的是静态资源,则由Nginx解析并返回给用户;
  • 如果是动态请求(.php结尾),那么Nginx就会将该请求通过FastCGI接口发送给PHP引擎服务(FastCGI进程php-fpm)进行解析;
  • 如果该动态请求需要读取数据库的数据,那么PHP就会继续向后端请求Mysql/Mariadb数据库,以读取需要的数据,而后通过Nginx服务将获取的数据返回给用户。

需要注意的是,这里所谓的静态和动态元素

  • 所谓静态,是指Nginx可以直接处理的图片、js、css、视频、音频、flash等等。
  • 所谓动态,是指这些请求需要和数据库进行链接。比如用户的登录,博客文章的查看和编写。

如下图:

二、Mysql的二进制免编译安装

  1. 1)下载二进制免编译版本mysql 5.6.35
  2. [root@localhost tools]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
  3. [root@localhost tools]# wget http://cn.php.net/distributions/php-7.2.5.tar.gz
  4. 2)增加mysql运行用户
  5. [root@localhost tools]# useradd -s /sbin/nologin -M mysql
  6. 3)解压并移动Mysql到指定的安装路径
  7. [root@localhost tools]# tar -zxf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
  8. [root@localhost tools]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql-5.6.35
  9. 4)创建软连接并更改目录所属
  10. [root@localhost tools]# ln -sv /usr/local/mysql-5.6.35 /usr/local/mysql
  11. ‘/usr/local/mysql -> ‘/usr/local/mysql-5.6.35
  12. [root@localhost mysql]# chown -R mysql.mysql /usr/local/mysql
  13. 5)初始化Mysql
  14. [root@localhost mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
  15. 6)拷贝Mysql启动脚本,并修改脚本权限启动
  16. [root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
  17. [root@localhost mysql]# chmod 755 /etc/init.d/mysqld
  18. [root@localhost mysql]# vim /etc/init.d/mysqld
  19. basedir=/usr/local/mysql
  20. datadir=/usr/local/mysql/data
  21. [root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf
  22. [root@localhost mysql]# /etc/init.d/mysqld start
  23. Starting MySQL.Logging to '/usr/local/mysql/data/localhost.err'.
  24. ... SUCCESS!
  25. [root@localhost mysql]# netstat -tulnp |grep 3306
  26. tcp6 0 0 :::3306 :::* LISTEN 62679/mysqld
  27. 7)加入开机启动,测试登录
  28. [root@localhost mysql]# chkconfig --add mysqld
  29. [root@localhost mysql]# chkconfig mysqld on
  30. [root@localhost mysql]# export PATH=/usr/local/mysql/bin/:$PATH
  31. [root@localhost mysql]# mysql
  32. Welcome to the MySQL monitor. Commands end with ; or \g.
  33. Your MySQL connection id is 1
  34. Server version: 5.6.35 MySQL Community Server (GPL)
  35. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  36. Oracle is a registered trademark of Oracle Corporation and/or its
  37. affiliates. Other names may be trademarks of their respective
  38. owners.
  39. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  40. mysql> quit;
  41. 8mysql安全设置
  42. [root@localhost mysql]# mysqladmin -uroot password '123456' //配置mysql的root用户密码
  43. Warning: Using a password on the command line interface can be insecure.
  44. [root@localhost mysql]# mysql
  45. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  46. [root@localhost mysql]# mysql -uroot -p123456 -e "show databases;"
  47. Warning: Using a password on the command line interface can be insecure.
  48. +--------------------+
  49. | Database |
  50. +--------------------+
  51. | information_schema |
  52. | mysql |
  53. | performance_schema |
  54. | test |
  55. +--------------------+
  56. [root@localhost mysql]# mysql -uroot -p //清理无用的Mysql用户和库
  57. Enter password:
  58. mysql> select user,host from mysql.user;
  59. +------+-----------+
  60. | user | host |
  61. +------+-----------+
  62. | root | 127.0.0.1 |
  63. | root | ::1 |
  64. | | localhost |
  65. | root | localhost |
  66. +------+-----------+
  67. rows in set (0.01 sec)
  68. mysql> drop user "root"@"::1"
  69. -> ;
  70. Query OK, 0 rows affected (0.00 sec)
  71. mysql> drop user ""@"localhost";
  72. Query OK, 0 rows affected (0.00 sec)
  73. mysql> select user,host from mysql.user;
  74. +------+-----------+
  75. | user | host |
  76. +------+-----------+
  77. | root | 127.0.0.1 |
  78. | root | localhost |
  79. +------+-----------+
  80. rows in set (0.00 sec)
  81. 有时使用drop命令删除不了用户,可能是大写或者是特殊的Linux主机名导致的,如下:
  82. mysql> drop user ''@'MySQL';
  83. ERROR 1396 (HY000): Operation DROP USER failed for ''@'mysql'
  84. 解决办法如下:
  85. mysql> delete from mysql.user where user='' and host='MySQL';
  86. mysql> flush privileges;

三、PHP 7.2.5编译部署

1、CGI和FastCGI

  CGI(Common Gateway Interface)通用网关接口,为HTTP服务器与其他机器上的程序服务通信交流的一种工具,CGI程序必须要在网络服务器上运行。传统的CGI接口方式存在很大的缺点就是性能较差。每次HTTP服务器遇到动态程序时,都需要重新启动解析器来执行解析,之后结果才会被返回给HTTP服务器,而这种方式在高并发的场景下是无法使用的,为了解决这一问题,随之产生的就是FastCGI。

  FastCGI是一个可伸缩、高速和HTTP服务器和动态脚本语言之间通信的接口(在Linux环境下,FastCGI接口即为socket,这个socket可以是文件socket,也可以是ip socket),主要优点是把动态语言和HTTP服务器分离开来。FastCGI采用的是C/S架构,可以将HTTP服务器和脚本解析服务器分开,同时还能在脚本解析服务器上启动一个或多个脚本解析守护进程,而后将得到的结果返回给浏览器。这种方式可以使HTTP服务器专一处理静态请求,或者是将动态请求的结果返回客户端,在这一点上大大提升了应用系统的性能。

2、Nginx FastCGI的运行原理

  Nginx不支持对外部动态程序的直接调用或者解析,所有的外部程序(如PHP)必须通过FastCGI接口来调用。FastCGI在Linux下是socket,为了调用CGI程序,还需要一个FastCGI的wrapper(可以理解为用于启动线程的工具),这个wrapper绑定在某个固定的socket上,如端口或文件socket,比如127.0.0.1:9000。当Nginx将CGI请求发送到这个socket的时候,通过FastCGI接口,wrapper接口到请求,然后派生一个新的线程,这个线程再去调用解释器或者外部程序处理脚本来读取返回的数据;接着wrapper再将返回的数据通过FastCGI接口,沿着原来的socket传递个Nginx;最后Nginx将返回的数据发送给客户端。

3、PHP的编译安装步骤

  1. (1)准备PHP依赖的库环境
  2. [root@localhost ~]# yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses curl openssl-devel gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel
  3. [root@localhost ~]# cd /tools
  4. (2)下载php 7.2.5并解压
  5. [root@localhost ~]# wget http://cn.php.net/distributions/php-7.2.5.tar.gz
  6. [root@localhost tools]# tar -zxf php-7.2.5.tar.gz
  7. [root@localhost tools]# cd php-7.2.5
  8. (3)编译安装
  9. [root@localhost php-7.2.5]# ./configure \
  10. --prefix=/usr/local/php7.2.5 \ //指定 php 安装目录
  11. --with-config-file-path=/usr/local/php7.2.5/etc \ //指定php.ini位置
  12. --with-config-file-scan-dir=/usr/local/php7.2.5/etc/conf.d \ //指定扩展php.ini位置
  13. --enable-inline-optimization \ //优化线程
  14. --disable-debug \ //关闭调试模式
  15. --disable-rpath \ //关闭额外的运行库文件
  16. --enable-shared \
  17. --enable-opcache \ //启用操作码缓存
  18. --enable-fpm \ //表示激活PHP-FPM方式服务,即FactCGI方式运行PHP服务。
  19. --with-mysql=mysqlnd \ //增加mysql支持
  20. --with-mysqli=mysqlnd \
  21. --with-pdo-mysql=mysqlnd \
  22. --with-gettext \ //打开gnu 的gettext 支持,编码库用到
  23. --enable-mbstring \ //多字节,字符串的支持
  24. --with-iconv \ //打开iconv函数,多种字符集间的转换
  25. --with-mcrypt \ //启用mcrypt加密算法
  26. --with-mhash \ //启用mhash加密算法
  27. --with-openssl \ //openssl的支持,加密传输时用到的
  28. --enable-bcmath \ //打开图片大小调整,用到zabbix监控的时候用到了这个模块
  29. --enable-soap \
  30. --with-libxml-dir \ //打开libxml2库的支持
  31. --enable-pcntl \ //freeTDS需要用到,可能是链接mssql
  32. --enable-shmop \
  33. --enable-sysvmsg \
  34. --enable-sysvsem \ //使用sysv信号机制
  35. --enable-sysvshm \
  36. --enable-sockets \ //打开sockets支持
  37. --enable-exif \
  38. --enable-zend-signals \
  39. --enable-gd-native-ttf \ //支持TrueType字符串函数库
  40. --enable-ftp \ //打开ftp的支持
  41. --with-curl \ //打开curl浏览工具的支持
  42. --with-zlib \ //打开zlib库的支持
  43. --enable-zip \ //打开对zip的支持
  44. --with-bz2 \ //打开对bz2文件的支持
  45. --with-readline \
  46. --with-jpeg-dir \ //打开对jpeg图片的支持
  47. --with-png-dir \ //打开对png图片的支持
  48. --with-gd \ //打开gd库的支持
  49. --with-freetype-dir \ //打开对freetype字体库的支持
  50. --with-pear \ //打开pear命令的支持,PHP扩展用的
  51. [root@localhost php-7.2.5]# make && make install
  52. (4)修改php服务的相关配置文件
  53. [root@localhost php-7.2.5]# cp php.ini-production /usr/local/php7.2.5/etc/php.ini
  54. [root@localhost php-7.2.5]# cd /usr/local/php7.2.5/etc/
  55. [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
  56. [root@localhost etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
  57. (5)拷贝启动脚本,并添加到开机启动
  58. [root@localhost etc]# cp /tools/php-7.2.5/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
  59. [root@localhost etc]# /usr/local/php7.2.5/sbin/php-fpm -t
  60. [17-Jul-2018 16:56:41] NOTICE: configuration file /usr/local/php7.2.5/etc/php-fpm.conf test is successful
  61. [root@localhost etc]# export PATH=/usr/local/php7.2.5/bin/:/usr/local/php7.2.5/sbin/:$PATH
  62. [root@localhost etc]# systemctl enable php-fpm
  63. Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
  64. (6)启动php-fpm,并检查端口
  65. [root@localhost etc]# systemctl start php-fpm
  66. [root@localhost etc]# netstat -tulnp |grep 9000
  67. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 14853/php-fpm: mast
  68. [root@localhost php7.2.5]# ln -sv /usr/local/php7.2.5 /usr/local/php
  69. ‘/usr/local/php -> ‘/usr/local/php7.2.5

四、Nginx的编译安装

  1. 1)安装pcreopenssl依赖
  2. pcre为正则依赖的库包文件,openssl在使用https时会用到
  3. [root@localhost tools]# yum install -y pcre pcre-devel openssl openssl-devel
  4. [root@localhost tools]# rpm -qa pcre pcre-devel
  5. pcre-devel-8.32-17.el7.x86_64
  6. pcre-8.32-17.el7.x86_64
  7. [root@localhost tools]# rpm -qa openssl openssl-devel
  8. openssl-devel-1.0.2k-12.el7.x86_64
  9. openssl-1.0.2k-12.el7.x86_64
  10. 2)下载nginx,并解压
  11. [root@localhost tools]# wget http://nginx.org/download/nginx-1.15.1.tar.gz
  12. [root@localhost tools]# tar -zxf nginx-1.15.1.tar.gz
  13. 3)编译安装
  14. [root@localhost tools]# useradd -s /sbin/nologin -M nginx
  15. [root@localhost tools]# cd nginx-1.15.1
  16. [root@localhost nginx-1.15.1]# ./configure --help
  17. [root@localhost nginx-1.15.1]# ./configure \
  18. --user=nginx \ #配置进程用户权限
  19. --gourp=nginx \ #配置进程用户组权限
  20. --prefix=/usr/local/nginx1.15.1 \ #指定安装路径
  21. --with-http_stub_status_module \ #使用激活状态信息模块
  22. --with-http_ssl_module #使用ssl功能模块
  23. [root@localhost nginx-1.15.1]# make && make install
  24. [root@localhost nginx-1.15.1]# ln -sv /usr/local/nginx1.15.1 /usr/local/nginx
  25. ‘/usr/local/nginx -> ‘/usr/local/nginx1.15.1
  26. 5)启动
  27. [root@localhost ~]# /usr/local/nginx/sbin/nginx -t #检查配置文件语法
  28. nginx: the configuration file /usr/local/nginx1.15.1/conf/nginx.conf syntax is ok
  29. nginx: configuration file /usr/local/nginx1.15.1/conf/nginx.conf test is successful
  30. [root@localhost ~]# /usr/local/nginx/sbin/nginx #启动nginx
  31. [root@localhost ~]# netstat -tunlp |grep 80  #检查监听端口
  32. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 58713/nginx: master

五、YUM安装Nginx

官网文档链接:http://nginx.org/en/linux_packages.html

  1. 1)增加Nginx仓库源
  2. [root@localhost ~]# vim /etc/yum.repos.d/nginx.repo
  3. [nginx]
  4. name=nginx repo
  5. baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
  6. gpgcheck=0
  7. enabled=1
  8. 2)安装Nginx
  9. [root@localhost ~]# yum install -y nginx
  10. 3)启动nginx
  11. [root@localhost ~]# nginx -t
  12. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  13. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  14. [root@localhost ~]# systemctl start nginx

浏览器访问:http://192.168.56.11,会出现nginx的欢迎页

【第五课】LNMP环境的入门的更多相关文章

  1. 用源码搭建LNMP环境+部署WordPress

    首先要做的是就是关闭Centos7.4的防火墙及selinux #systemctl stop firewalld #systemctl disable firewalld #sed -ri 's/^ ...

  2. Elasticsearch7.X 入门学习第五课笔记---- - Mapping设定介绍

    原文:Elasticsearch7.X 入门学习第五课笔记---- - Mapping设定介绍 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本 ...

  3. IC入门课第五课作业:完善 Microblog 前端(1、显示发布者的名字;2、增加新UI、3、关注其他学员的 canister)

    上周完成了 IC 入门课程第五课的作业 现将答案贴出,欢迎同学们参考,禁止抄袭作业哦 课程作业 (完善 microblog 前端) 1. 显示消息的发布者名字 a. 给 Message 增加 auth ...

  4. 新手配置LNMP环境教程

    回顾一下这几天自己配置LNMP环境踩得坑,希望帮助更多人 前期准备:VMtool.Linux.Nginx.Mysql.PHP.cmake 版本如下:Centos6.nginx1.6.0.mysql5. ...

  5. LAMP坏境和LNMP环境安装Nagios4.1.1和基本配置

    ----------------------------------------以下内容为笔者生产环境的监控,安装都是经过一步步测试的-------------------------------- ...

  6. Ubuntu系统下lnmp环境搭建和Nginx多站点配置

    最近需要使用Ubuntu作为服务器搭建Lnmp环境,顺便将操作过程写下来,与大家分享.如有不足之处,欢迎大家提出不同意见.(本文默认读者已经熟悉相关linux命令的使用,比如创建文件和文件夹,编辑文件 ...

  7. 阿里云(ECS)Centos服务器LNMP环境搭建

    阿里云( ECS ) Centos7 服务器 LNMP 环境搭建 前言 第一次接触阿里云是大四的时候,当时在校外公司做兼职,关于智能家居项目的,话说当时俺就只有一个月左右的 php 后台开发经验(还是 ...

  8. NeHe OpenGL教程 第四十五课:顶点缓存

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  9. NeHe OpenGL教程 第三十五课:播放AVI

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

随机推荐

  1. 使用Webpack对Css文件压缩处理的思考

    问题的起因: 使用 bulma.css ,通过webpack打包后样式出错,查看压缩代码,发现代码从css的 long hand 属性被压缩为 short hand(PS: 什么是long hand ...

  2. JavaScript语法详解:if语句&for循环&函数

    本文首发于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. if语句 最基本的if语句 if语句的结构体:(格式) if (条件表达式) ...

  3. LeetCode题解之Binary Tree Tilt

    1.题目描述 2.分析 利用递归实现. 3.代码 int findTilt(TreeNode* root) { if (root == NULL) ; ; nodesTilt(root,ans); r ...

  4. c#创建文件( File.Create() )后对文件写操作出错的分析

    在C#中,使用system.IO.File.Create()创建完一个文件之后,如果需要对这个文件进行写操作,会出现错误,提示你“这个文件正在被使用”. 原因是System.IO.File.Creat ...

  5. jQuery为元素设置css的问题

    例子: 有如下的html代码 对文本框设置字体大小为20px ,即font-size:20px 首先会想到如下: $('input').css({font-size:'20px'}); 由于属性不能使 ...

  6. Visual Studio 2012自动添加注释(如版权信息等)

    转自:http://blog.163.com/guohuan88328@126/blog/static/69430778201381553150156/ 如何使用Visual Studio 2012给 ...

  7. 使用yum下载rpm包

    查看系统有哪些可用的yum源yum repolist all yum指定本地源安装rpm包yum install <package-name> --enablerepo=<repos ...

  8. 深度访谈Amazon员工与HR:华裔因pip跳楼背后(图)

    http://www.wenxuecity.com/news/2016/12/01/5813342.html 首先,让我们来回顾一下这起事件.两天前在某论坛中,有同学发了这么一个帖子,大致意思是说有一 ...

  9. 卸载CocoaPods

    1. 移除pod组件 这条指令会告诉你Cocoapods组件装在哪里 : $ which pod 你可以手动移除这个组件 : $ sudo rm -rf <path> 2.移除 RubyG ...

  10. 四、 git关联远程仓库及推送

    接之前笔记,在 github上建立与本地同名的仓库 demo 关联远程仓库 1. https 模式     远程库的名字就是origin,这是Git默认的叫法 git remote add origi ...