Linux云计算架构-部署LNMP架构

Love The Way You Lie 2023-02-20 01:55 137阅读 0赞

文章目录

  • Linux云计算架构-部署LNMP架构
      1. 什么是LNMP架构
      1. LNMP架构中nginx服务解析
      1. 部署LNMP架构
      • 3.1 部署前准备
      • 3.2 部署nginx服务
      • 3.3 配置nginx支持php
      • 3.4 nginx服务收尾工作
      • 3.5 nginx服务检验效果
      • 3.6 安装并配置mysql
      • 3.7 mysql收尾工作
      • 3.8 安装并配置php

Linux云计算架构-部署LNMP架构

1. 什么是LNMP架构

LNMP=Linux + Nginx + MySQL + PHP

Nginx 是一款轻量级的 Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个 BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上 Nginx 的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用 Nginx 网站用户有:百度 、新浪、网易、腾讯等。

官网http://nginx.org/

  1. # 查看下百度和新浪网页的服务器类型
  2. [root@master ~]# curl -I www.bai.com
  3. HTTP/1.1 200 OK
  4. Server: nginx/1.14.1
  5. [root@master ~]# curl -I www.sina.com
  6. HTTP/1.1 301 Moved Permanently
  7. Server: nginx

2. LNMP架构中nginx服务解析

nginx服务和apache服务对比(两种web服务):

nginx通过php-fpm服务处理php文件

nginx和php-fpm各自独立运行,需要分别启动

优点:占用更少的内存和资源、高并发、以异步非塞的方式处理请求【apache处理请求是阻塞型的】、在高并发下能保持低资源低消耗高性能、高度模块化设计、编写模块相对简单、社区活跃、高性能模块产出较快。(追求性能)

nginx有很多优点,但有一个弱点,即是处理动态请求较慢,故一般把动态请求交由apache去做,nginx只适合静态和反向。
在这里插入图片描述

apache通过libphp5.so模块处理php文件

apache的libphp5.so模块随着apache服务一起启动

优点:rewrite强大、模块多、少bug、稳定。
在这里插入图片描述

3. 部署LNMP架构

基础环境:

①系统环境:

[root@master ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

②IP地址:192.168.8.116 可连接外网

③配置了自带的yum。

3.1 部署前准备

  1. # 安装部署LNMP所需依赖包
  2. # 这个不用专门记下来有什么,上网搜就可以了
  3. [root@master ~]# yum -y install make gcc gcc-c++ flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel gd freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel ncurses-devel gmp-devel unzip libcap lsof
  4. # 官网下载nginx源码包
  5. http://nginx.org/download/nginx-1.14.1.tar.gz
  6. # 以下源码包可到各自官网下载
  7. # 我这里直接下载到本地,然后上传到目录/usr/local/src/目录下
  8. [root@master src]# ls
  9. boost_1_59_0.tar.gz mcrypt-2.6.8.tar.gz nginx-1.14.1.tar.gz
  10. libiconv-1.14.tar.gz mhash-0.9.9.9.tar.gz pcre-8.41.tar.gz
  11. libmcrypt-2.5.8.tar.gz mysql-5.7.19.tar.gz php-7.1.24.tar.gz
  12. [root@master src]# pwd
  13. /usr/local/src
  14. # 安装epel扩展yum源
  15. [root@master ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  16. # 安装所需依赖包
  17. # nginx提供gzip模块,需要zlib支持
  18. # openssl为nginx提供SSL功能
  19. [root@master ~]# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre*
  20. # 创建nginx用户
  21. # -M 参数是指创建家目录
  22. -M, --no-create-home
  23. Do not create the user's home directory, even if the system
  24. wide setting from /etc/login.defs (CREATE_HOME) is set to
  25. yes.
  26. [root@master ~]# useradd -M -s /sbin/nologin nginx

3.2 部署nginx服务

  1. # 解压并进入nginx目录下
  2. [root@master ~]# cd /usr/local/src/
  3. [root@master src]# tar xzvf nginx-1.14.1.tar.gz
  4. [root@master src]# tar xzvf pcre-8.41.tar.gz
  5. [root@master src]# cd nginx-1.14.1/
  6. # 编译前准备
  7. [root@master nginx-1.14.1]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.41 --user=nginx --group=nginx
  8. # --prefix 指定安装目录
  9. # --with-http_dav_module 启用支持,增加PUT\DELETE\MKCOL,编译开启
  10. # --with-http_stub_status_module 获取nginx上次启动以来的工作状态
  11. # --with-http_addition_module 作用一个输出过滤器,支持不完全缓冲,分部分相应请求
  12. # --with-http_sub_module 允许一些其他文本替换nginx中的一些文本
  13. # --with-http_flv_module 支持flv视频格式
  14. # --with-http_mp4_module 支持mp4视频格式
  15. # --with-pcre pcre源码所在目录
  16. # --user nginx用户
  17. # --group nginx用户组
  18. # 编译 make
  19. # -j 用来指定CPU核心数,可加快编译速度。 加 -j 8 用8核心来编译的,可以提高编译速度;要确定CPU有多少核先。
  20. # 查看服务器 CPU 核心数
  21. [root@master nginx-1.14.1]# grep process /proc/cpuinfo | wc -l
  22. 8
  23. [root@master nginx-1.14.1]# make -j 8
  24. # 编译安装 make install
  25. [root@master nginx-1.14.1]# make install
  26. # make[1]: 离开目录“/usr/local/src/nginx-1.14.1”
  27. # 若有以上报错,可以看看目录/usr/local/nginx/是否存在,存在则不用理会该报错。
  28. # 查看nginx目录下的文件
  29. [root@master local]# cd nginx/
  30. [root@master nginx]# ls
  31. conf html logs sbin
  32. [root@master nginx]# ll
  33. 总用量 4
  34. drwxr-xr-x. 2 root root 4096 6 20 14:43 conf
  35. drwxr-xr-x. 2 root root 40 6 20 14:43 html
  36. drwxr-xr-x. 2 root root 6 6 20 14:43 logs
  37. drwxr-xr-x. 2 root root 36 6 20 14:46 sbin

3.3 配置nginx支持php

  1. # 备份nginx主配置文件
  2. [root@master ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
  3. [root@master ~]# vim /usr/local/nginx/conf/nginx.conf
  4. # 修改nginx主配置文件
  5. # 修改用户
  6. 2 user nginx;
  7. # 去掉65行开始的注释
  8. 65 location ~ \.php$ {
  9. 66 root html;
  10. 67 fastcgi_pass 127.0.0.1:9000;
  11. 68 fastcgi_index index.php;
  12. 69 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
  13. 70 include fastcgi_params;
  14. 71 }

3.4 nginx服务收尾工作

  1. # 启动服务(启动脚本启动)
  2. # sbin目录下存放着启动脚本
  3. [root@master ~]# /usr/local/nginx/sbin/nginx
  4. # 配置环境变量文件
  5. [root@master ~]# vim /etc/profile.d/nginx.sh
  6. export PATH=/usr/local/nginx/sbin:$PATH
  7. # 读取变量或者添加软连接
  8. # 若没有执行权限,可以给+x权限
  9. [root@master ~]# chmod +x /etc/profile.d/nginx.sh
  10. root@master ~]# /etc/profile.d/nginx.sh
  11. # 生成服务启动脚本
  12. [root@master ~]# vim /etc/init.d/nginx
  13. #!/bin/bash
  14. # chkconfig: - 99 2
  15. # description: Nginx Service Control Script
  16. PROG="/usr/local/nginx/sbin/nginx"
  17. PIDF="/usr/local/nginx/logs/nginx.pid"
  18. case "$1" in
  19. start)
  20. $PROG
  21. ;;
  22. stop)
  23. kill -3 $(cat $PIDF)
  24. ;;
  25. restart)
  26. $0 stop &> /dev/null
  27. if [ $? -ne 0 ] ; then continue ; fi
  28. $0 start
  29. ;;
  30. reload)
  31. kill -1 $(cat $PIDF)
  32. ;;
  33. *)
  34. echo "Userage: $0 { start | stop | restart | reload }"
  35. exit 1
  36. esac
  37. exit 0
  38. [root@master ~]# chmod +x /etc/init.d/nginx
  39. # 配置服务启动和自启动
  40. [root@master ~]# chkconfig --add nginx
  41. [root@master ~]# chkconfig nginx on

3.5 nginx服务检验效果

  1. # 清空iptables,再浏览器上验证,当看到下图,则表示nginx已经装好了。
  2. [root@master ~]# iptable -F
  3. # 若有报错,可以用以下命令维护
  4. # 检查配置文件是否有语法错误
  5. [root@master ~]# nginx -t
  6. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  7. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  8. # 检查nginx版本和配置参数
  9. [root@master ~]# nginx -V
  10. nginx version: nginx/1.14.1
  11. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
  12. configure arguments: --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.41 --user=nginx --group=nginx

在这里插入图片描述

3.6 安装并配置mysql

  1. # 删除系统自带的boost-*
  2. [root@master ~]# yum -y remove boost-*
  3. # 删除系统自带的mysql和mariadb-*
  4. [root@master ~]# yum -y remove mysql mariadb-*
  5. # 安装mysql依赖包
  6. [root@master ~]# yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel
  7. # 创建mysql组和mysql用户
  8. [root@master ~]# groupadd mysql
  9. [root@master ~]# useradd -M -s /sbin/nologin -g mysql mysql
  10. # 下载mysql源码包
  11. http://www.mysql.com/Downloads/MySQL-5.7/mysql-5.7.19.tar.gz
  12. # 下载boost源码包
  13. http://liquidtelecom.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
  14. # 我这里已经下载好了,直接解压
  15. [root@master ~]# cd /usr/local/src
  16. root@master src]# tar xzvf mysql-5.7.19.tar.gz
  17. root@master src]# tar xzvf boost_1_59_0.tar.gz
  18. [root@master src]# ls
  19. boost_1_59_0 mhash-0.9.9.9.tar.gz pcre-8.41
  20. boost_1_59_0.tar.gz mysql-5.7.19 pcre-8.41.tar.gz
  21. libiconv-1.14.tar.gz mysql-5.7.19.tar.gz php-7.1.24.tar.gz
  22. libmcrypt-2.5.8.tar.gz nginx-1.14.1
  23. mcrypt-2.6.8.tar.gz nginx-1.14.1.tar.gz
  24. # 规划安装目录
  25. # 数据目录最好是一个单独的分区或者RAID或者LVM,便于日后的磁盘维护和扩容
  26. # 对于读写频繁的业务,可采用SSD固态硬盘存储数据
  27. 安装目录: /var/lib/mysql
  28. 数据目录: /var/lib/mysql/data
  29. [root@master ~]# mkdir -p /var/lib/mysql/data
  30. [root@master ~]# chown -R mysql:mysql /var/lib/mysql/
  31. [root@master ~]# ll -d /var/lib/mysql/
  32. drwxr-xr-x. 3 mysql mysql 18 6 20 15:31 /var/lib/mysql/
  33. # 编译前准备
  34. [root@master mysql-5.7.19]# cmake -DCMAKE_INSTALL_PREFIX=/var/lib/mysql -DMYSQL_DATADIR=/var/lib/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/src/boost_1_59_0
  35. # -DCMAKE_INSTALL_PREFIX=/var/lib/mysql 指定MySQL程序的安装目录,默认/usr/local/mysql
  36. # -DMYSQL_DATADIR=/var/lib/mysql/data 数据文件目录
  37. # -DSYSCONFDIR=/etc 初始化参数文件目录
  38. # -DWITH_MYISAM_STORAGE_ENGINE=1 指定静态编译到 mysql 的存储引擎,MyISAM,MERGE
  39. # -DWITH_INNOBASE_STORAGE_ENGINE=1
  40. # -DWITH_MEMORY_STORAGE_ENGINE=1
  41. # -DWITH_READLINE=1
  42. # -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock socket文件路径,默认/tmp/mysql.sock
  43. # -DMYSQL_TCP_PORT=3306 服务端口号,默认 3306
  44. # -DENABLED_LOCAL_INFILE=1 指定是否允许本地执行
  45. # -DWITH_PARTITION_STORAGE_ENGINE=1
  46. # -DEXTRA_CHARSETS=all
  47. # -DDEFAULT_CHARSET=utf8 指定服务器默认字符集,默认 latin1
  48. # -DDEFAULT_COLLATION=utf8_general_ci 指定服务器默认的校对规则,默认 latin1_general_ci
  49. # -DDOWNLOAD_BOOST=1
  50. # -DWITH_BOOST=/usr/local/src/boost_1_59_0
  51. # 编译
  52. # -j 用来指定CPU核心数,可加快编译速度。 加 -j 4 用4核心来编译的,可以提高编译速度;要确定CPU有多少核先。
  53. # 查看服务器 CPU 核心数
  54. [root@master mysql-5.7.19]# grep processor /proc/cpuinfo | wc -l
  55. 8
  56. # 编译时估计要等几分钟
  57. # 若是遇到报错,可以删除mysql解压目录下的文件CMakeCache.txt文件,重新进行一次编译前准备(cmake)和编译(make),第二次应该是不会报错的。
  58. # 报错如下:
  59. #make[2]: *** [libmysqld/CMakeFiles/sql_embedded.dir/__/sql/item_geofunc.cc.o] 错误 4
  60. [root@master mysql-5.7.19]# make -j 4
  61. # 编译安装
  62. [root@master mysql-5.7.19]# make install
  63. [root@master ~]# vim /etc/my.cnf
  64. [mysqld]
  65. basedir=/var/lib/mysql
  66. datadir=/var/lib/mysql/data
  67. port=3306
  68. socket=/var/lib/mysql/mysql.sock
  69. character-set-server=utf8
  70. log-error=/var/log/mysqld.log
  71. pid-file=/tmp/mysqld.pid
  72. [mysql]
  73. socket=/var/lib/mysql/mysql.sock
  74. [client]
  75. socket=/var/lib/mysql/mysql.sock

3.7 mysql收尾工作

  1. # 添加环境变量
  2. [root@master ~]# vim /etc/profile.d/mysql.sh
  3. export PATH=/var/lib/mysql/bin:$PATH
  4. # 使环境变量生效
  5. root@master ~]# chmod +x /etc/profile.d/mysql.sh
  6. [root@master ~]# /etc/profile.d/mysql.sh
  7. # 生成服务启动脚本
  8. [root@master ~]# cp /var/lib/mysql/support-files/mysql.server /etc/init.d/mysqld
  9. # 初始化数据库
  10. [root@master ~]# /var/lib/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/var/lib/mysql --datadir=/var/lib/mysql/data
  11. # 启动mysqld服务
  12. [root@master ~]# /etc/init.d/mysqld start
  13. Starting MySQL.. ERROR! The server quit without updating PID file (/tmp/mysqld.pid).
  14. # 若报错,删除数据目录下文件,重新初始化再启动服务
  15. [root@master ~]# cd /var/lib/mysql/data/
  16. [root@master data]# ls
  17. auto.cnf ib_buffer_pool ibdata1 ib_logfile0 ib_logfile1
  18. [root@master data]# rm -f *
  19. [root@master data]# /var/lib/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/var/lib/mysql --datadir=/var/lib/mysql/data
  20. [root@master data]# /etc/init.d/mysqld start
  21. Starting MySQL. SUCCESS!
  22. # 登录mysql并设置root用户密码
  23. [root@master ~]# mysql
  24. Welcome to the MySQL monitor. Commands end with ; or \g.
  25. Your MySQL connection id is 3
  26. Server version: 5.7.19 Source distribution
  27. Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  28. Oracle is a registered trademark of Oracle Corporation and/or its
  29. affiliates. Other names may be trademarks of their respective
  30. owners.
  31. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  32. mysql> set password for root@localhost = password('123456');
  33. Query OK, 0 rows affected, 1 warning (0.00 sec)
  34. mysql> flush privileges;
  35. Query OK, 0 rows affected (0.00 sec)
  36. mysql> exit
  37. Bye
  38. [root@master ~]#

经历了漫长岁月,mysql也装好了。

3.8 安装并配置php

官网地址:http://php.net/

  1. # 下载php依赖包
  2. http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
  3. http://iweb.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
  4. http://iweb.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
  5. # 解决包依赖
  6. [root@master ~]# yum -y install php-mcrypt libmcrypt libmcrypt-devel php-pear libxml2 libxml2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel
  7. # 解压php源码包
  8. [root@master ~]# cd /usr/local/src/
  9. [root@master src]# tar xzvf php-7.1.24.tar.gz
  10. # 编译前准备
  11. [root@master src]# cd php-7.1.24/
  12. [root@master php-7.1.24]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/ --enable-fpm --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
  13. # 编译并安装
  14. [root@master php-7.1.24]# make -j 4 && make install
  15. # 配置php和php-fpm
  16. #生成php自身配置文件
  17. [root@master ~]# cp /usr/local/src/php-7.1.24/php.ini-production /usr/local/php/php.ini
  18. #生成 php-fpm 配置文件
  19. [root@master ~]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf
  20. # 修改PHP-FPM 启动脚本
  21. [root@master ~]# vim /usr/local/php/etc/php-fpm.conf
  22. 23 user = nginx
  23. 24 group = nginx
  24. # 生成 php-fpm 启动脚本
  25. [root@master ~]# cp /usr/local/src/php-7.1.24/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  26. # 设置php-fpm启动和自启动
  27. [root@master ~]# chmod +x /etc/init.d/php-fpm
  28. [root@master ~]# chkconfig php-fpm on
  29. [root@master ~]# /etc/init.d/php-fpm start
  30. Starting php-fpm done
  31. # 检验php-fpm是否启动
  32. [root@master ~]# netstat -antup | grep php-fpm
  33. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 77628/php-fpm: mast
  34. # 测试LNMP的PHP支持
  35. [root@master ~]# echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/index.php
  36. [root@master ~]# iptables -F
  37. # 重启服务器访问以下地址:
  38. http://192.168.8.116/index.php
  39. # 访问php页面报错了!!!报错HTTP 404未找到
  40. # 用netstat -antup | grep nginx mysql php-fpm检测下服务是否开启
  41. # 用netstat -antup | grep 9000 检测下端口是否被php-fpm占用
  42. # 查看下nginx错误日志,如下:
  43. 2020/06/20 18:49:04 [error] 9408#0: *26 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.8.102, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.8.116"
  44. # 貌似说的是配置文件出错了,改成如下
  45. 65 location ~ \.php$ {
  46. 66 root html;
  47. 67 fastcgi_pass 127.0.0.1:9000;
  48. 68 fastcgi_index index.php;
  49. 69 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  50. 70 include fastcgi_params;
  51. 71 }
  52. # 终于可以支持php了!!!
  53. # 上述配置文件中的内容已修改,你们测试的时候应该是不会报错的。
  54. # 把$document_root改为/usr/local/nginx/html/也是可以的,也就是上面的做法。

在这里插入图片描述
至此为止,LNMP架构已经搭建完成了,这一天也过去了!
如果是虚拟机搭建的,赶紧打个快照,以便之后使用。

发表评论

表情:
评论列表 (有 0 条评论,137人围观)

还没有评论,来说两句吧...

相关阅读

    相关 lnmp 架构

    一、lnmp 架构 LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。 Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMA

    相关 计算平台架构

    云计算的体系结构由5部分组成,分别为应用层,平台层,资源层,用户访问层和管理层,云计算的本质是通过网络提供服务,所以其体系结构以服务为核心。 如下图: ![20110722