LNMP环境部署

LNMP环境部署

1. LNMP部署规划

  1. 软件规划
版本
NginxNginx 1.16.1
MySQLMySQL 5.7.28
PHPPHP 7.0.33
  1. 系统规划

操作系统:CentOS Linux release 7.3.1611

主机IP地址
node07.host.com172.24.248.19
node08.host.com172.24.248.20
node09.host.com172.24.248.21

2. Nginx部署

2.1 Nginx介绍

nginx官网

WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务。 WWW 是 Internet的多媒体信息查询工具,是 Internet 上近年才发展起来的服务,也是发展最快和目前用的最广泛的服务。正是因为有了WWW工具,才使得近年来 Internet 迅速发展,且用户数量飞速增长。

Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

它已经在众多流量很大的俄罗斯网站上使用了很长时间,这些网站包括YandexMail.RuVKontakte,以及Rambler。据Netcraft统计,在2012年8月份,世界上最繁忙的网站中有11.48%使用Nginx作为其服务器或者代理服务器。目前互联网主流公司360、百度、新浪、腾讯、阿里等,目前中国互联网企业70%以上公司都在使用nginx作为自己的web服务器。

Nginx特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。

Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是Nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。

Nginx相对于Apache优点

1)   高并发响应性能非常好,官方Nginx处理静态文件并发5w/s

2)   反向代理性能非常强。(可用于负载均衡)

3)   内存和cpu占用率低。(为Apache的1/5-1/10)

4)   对后端服务有健康检查功能。

5)   支持PHP cgi方式和fastcgi方式。

6)   配置代码简洁且容易上手。

2.2 Nginx 工作原理

Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是Nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作.

Nginx的模块从结构上分为核心模块、基础模块和第三方模块

核心模块:HTTP模块、EVENT模块和MAIL模块

基础模块:HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块,

第三方模块:HTTP Upstream Request Hash模块、Notice模块和HTTP Access Key模块。

Nginx的高并发得益于其采用了epoll模型,与传统的服务器程序架构不同,epoll是linux内核2.6以后才出现的。Nginx采用epoll模型,异步非阻塞,而Apache采用的是select模型:

Select特点:select 选择句柄的时候,是遍历所有句柄,也就是说句柄有事件响应时,select需要遍历所有句柄才能获取到哪些句柄有事件通知,因此效率是非常低。

epoll的特点:epoll对于句柄事件的选择不是遍历的,是事件响应的,就是句柄上事件来就马上选择出来,不需要遍历整个句柄链表,因此效率非常高

2.3 Nginx安装

  1. 环境依赖
[root@node08.host.com ~]#  yum -y install vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-comletion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed gd-devel GeoIP GeoIP-devel GeoIP-data 

  1. 编译安装nginx
[root@node08.host.com /usr/local/src]# tar xf nginx-1.16.1.tar.gz
[root@node08.host.com /usr/local/src]# useradd nginx -s  /sbin/nologin -u 2000 
[root@node08.host.com /usr/local/src]# cd nginx-1.16.1
[root@node08.host.com /usr/local/src/nginx-1.16.1]# cat src/core/nginx.h |grep NGINX_VER
#define NGINX_VERSION      "1.16.1"   
define NGINX_VER          "nginx/" NGINX_VERSION   #开启server_tokens显示此信息
[root@node08.host.com /usr/local/src/nginx-1.16.1]# cat src/http/ngx_http_header_filter_module.c |grep "NGINX_VER_BUILD"
static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF; #关闭server_tokens显示此信息
[root@node08.host.com /usr/local/src/nginx-1.16.1]#  

[root@node08.host.com /usr/local/src/nginx-1.16.1]# ./configure --prefix=/apps/nginx  --with-http_ssl_module --with-http_v2_module  --with-http_realip_module --with-http_addition_module  --with-http_image_filter_module --with-http_geoip_module --with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@node08.host.com /usr/local/src/nginx-1.16.1]# make && make install

  1. 配置nginx,自定义json日志,error页面,并启动nginx
[root@node08.host.com /apps/nginx/conf]# egrep -v "#|^$" nginx.conf
user  nginx;
worker_processes  2;
worker_cpu_affinity 01 10;
error_log  /apps/nginx/logs/error.log  error;
pid        /apps/nginx/logs/nginx.pid;
worker_priority 0;
worker_rlimit_nofile 65536;
events {
  worker_connections  65536;
  use epoll;
  accept_mutex on;
  multi_accept on;
}
http {
  include       mime.types;
  default_type  application/octet-stream;

  log_format access_json '{"@timestamp":"$time_iso8601",'
     '"host":"$server_addr",'
     '"clientip":"$remote_addr",'
     '"size":$body_bytes_sent,'
     '"responsetime":$request_time,'
     '"upstreamtime":"$upstream_response_time",'
     '"upstreamhost":"$upstream_addr",'
     '"http_host":"$host",'
     '"uri":"$uri",'
     '"domain":"$host",'
     '"xff":"$http_x_forwarded_for",'
     '"referer":"$http_referer",'
     '"tcp_xff":"$proxy_protocol_addr",'
     '"http_user_agent":"$http_user_agent",'
     '"status":"$status"}';
   access_log /apps/nginx/logs/access_json.log access_json;
  sendfile        on;
  keepalive_timeout  65 65;
  server_tokens off;
  server {
      listen       80;
      server_name  localhost;
      charset utf-8;
      location / {
          root   html;
          index  index.html index.htm;
      }
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }
  }
include /apps/nginx/conf/conf.d/*.conf;
}
[root@node08.host.com /apps/nginx/conf]#
[root@node08.host.com /apps/nginx]# cd html/
[root@node08.host.com /apps/nginx/html]# echo "access page error" > error.html
[root@node08.host.com /apps/nginx/html]# chown nginx.nginx -R /apps/nginx/
# 查看版本以及编译参数
[root@node08.host.com ~]# /apps/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
# 检查nginx配置语法是否正确
[root@node08.host.com ~]# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@node08.host.com ~]# 
# 启动nginx
[root@node08.host.com ~]# /apps/nginx/sbin/nginx
  1. 页面验证nginx

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gk6DeCFP-1612149734988)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210126173130434.png)]

3. MySQL部署

官网地址

  1. 二进制安装mysql
[root@node08.host.com ~]# tar xf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz 
[root@node08.host.com ~]# ln -sv /usr/local/mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql-5.6.36
"/usr/local/mysql-5.6.36" -> "/usr/local/mysql-5.6.36-linux-glibc2.5-x86_64"
[root@node08.host.com ~]# useradd mysql -s /sbin/nologin
[root@node08.host.com ~]# mkdir -pv /opt/mysql /var/lib/mysql
mkdir: 已创建目录 "/opt/mysql"
[root@node08.host.com ~]# chown -R mysql.mysql /opt/mysql /var/lib/mysql -R

  1. 初始化数据库
[root@node08.host.com ~]# /usr/local/mysql-5.6.36/scripts/mysql_install_db --user=mysql --datadir=/opt/mysql --basedir=/usr/local/mysql-5.6.36
[root@node08.host.com ~]# cp /usr/local/mysql-5.6.36/support-files/mysql.server /etc/init.d/mysqld
[root@node08.host.com ~]# chmod a+x /etc/init.d/mysqld 
  1. 配置文件
[root@node08.host.com /usr/local/mysql-5.6.36]# cat my.cnf 
[mysqld]
basedir = /usr/loca/mysql
datadir = /opt/mysql
port = 3306
# server_id = .....
socket = /opt/mysql/mysql.sock
user=mysql
symbolic-links=0
innodb_file_per_table=1
max_connections=10000

[client]
port=3306
socket = /var/lib/mysql/mysql.sock

[mysqld_safe]
log-error=/usr/local/mysql/log/mysqld.log
pid-file=/tmp/mysql.sock
[root@node08.host.com /usr/local/mysql-5.6.36]# 

[root@node08.host.com /usr/local/mysql-5.6.36/log]# touch mysqld.log
[root@node08.host.com /usr/local/mysql-5.6.36]# ./mysql.server start
Starting MySQL..... SUCCESS!

4. PHP部署

  1. 官网地址
  2. PHP各版本下载地址
  1. 软件环境依赖
[root@node08.host.com ~]# yum -y install pcre pcre-devel openssl openssl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel  libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn-devel  openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison libevent automake libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel openjpeg bzip2 bzip2-devel
  1. 解压安装
[root@node08.host.com /usr/local/src]# wget https://museum.php.net/php7/php-7.2.15.tar.gz
[root@node08.host.com /usr/local/src]# tar xf php-7.2.15.tar.gz
[root@node08.host.com /usr/local/src]# cd php-7.2.15
[root@node08.host.com /usr/local/src/php-7.2.15]# ./configure --prefix=/apps/php-7.2.15 --with-config-file-path=/apps/php-7.2.15/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-pear --with-curl --with-png-dir --with-freetype-dir --with-iconv --with-mhash --with-zlib --with-xmlrpc --with-xsl --with-openssl --with-mysqli  --with-pdo-mysql --disable-debug --enable-zip --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-exif --enable-wddx --enable-bcmath --enable-calendar --enable-shmop --enable-dba --enable-sysvsem --enable-sysvshm --enable-sysvmsg  --enable-libxml --with-gd --with-jpeg-dir --with-bz2  --with-mcrypt  --enable-gd-native-ttf --enable-fastcgi --enable-pdo --enable-mbstring --enable-exif --enable-opcache --enable-mbregex  --enable-pcntl  --with-gettext --enable-session --enable-ctype  --enable-dom --with-libdir=lib64 --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd  --disable-fileinfo
[root@node08.host.com /usr/local/src/php-7.2.15]# make && make install

  1. 生成配置文件
[root@node08.host.com ~]# cd /apps/php-7.2.15/etc/php-fpm.d/
[root@node08.host.com /apps/php-7.2.15/etc/php-fpm.d]# cp www.conf.default www.conf
[root@node08.host.com /apps/php-7.2.15/etc/php-fpm.d]# cp /usr/local/src/php-7.2.15/php.ini-production /apps/php-7.2.15/etc/php.ini
[root@node08.host.com /apps/php-7.2.15/etc/php-fpm.d]# useradd www -s /sbin/nologin -u 1001
[root@node08.host.com /apps/php-7.2.15/etc/php-fpm.d]# cat www.conf
[www]
user = www
group = www
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 30
pm.min_spare_servers = 30
pm.max_spare_servers = 35
pm.status_path = /status
ping.path = /ping
ping.response = pong
access.log = log/$pool.access.log
slowlog = log/$pool.log.slow
[root@node08.host.com /apps/php-7.2.15/etc/php-fpm.d]#
[root@node08.host.com /apps/php-7.2.15/etc]# mkdir /apps/php-7.2.15/log
[root@node08.host.com /apps/php-7.2.15/etc]# cp php-fpm.conf.default php-fpm.conf
[root@node08.host.com /apps/php-7.2.15/etc]#
  1. 启动并验证php-fpm
[root@node08.host.com ~]# /apps/php-7.2.15/sbin/php-fpm -t
[29-Jan-2021 13:59:28] NOTICE: configuration file /apps/php-7.2.15/etc/php-fpm.conf test is successful
# 验证php-fpm
[root@node08.host.com ~]# /apps/php-7.2.15/sbin/php-fpm -c /apps/php-7.2.15/etc/php.ini

[root@node08.host.com ~]# netstat -lntp |grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      1630/php-fpm: maste

4.1 Nginx整合Php

  1. 准备测试页
[root@node08.host.com ~]# cat /apps/nginx/html/index.php
<?php
 phpinfo();
?>
[root@node08.host.com ~]#
  1. 整合php
[root@node08.host.com ~]# cat /apps/nginx/conf/nginx.conf|egrep -v "#|^$"
user  nginx;
worker_processes  2;
worker_cpu_affinity 01 10;
error_log  /apps/nginx/logs/error.log  error;
pid        /apps/nginx/logs/nginx.pid;
worker_priority 0;
worker_rlimit_nofile 65536;
events {
   worker_connections  65536;
   use epoll;
   accept_mutex on;
   multi_accept on;
}
http {
   include       mime.types;
   default_type  application/octet-stream;
   
   log_format access_json '{"@timestamp":"$time_iso8601",'
      '"host":"$server_addr",'
      '"clientip":"$remote_addr",'
      '"size":$body_bytes_sent,'
      '"responsetime":$request_time,'
      '"upstreamtime":"$upstream_response_time",'
      '"upstreamhost":"$upstream_addr",'
      '"http_host":"$host",'
      '"uri":"$uri",'
      '"domain":"$host",'
      '"xff":"$http_x_forwarded_for",'
      '"referer":"$http_referer",'
      '"tcp_xff":"$proxy_protocol_addr",'
      '"http_user_agent":"$http_user_agent",'
      '"status":"$status"}';
    access_log /apps/nginx/logs/access_json.log access_json;
   sendfile        on;
   keepalive_timeout  65 65;
   server_tokens off;
   server {
       listen       80;
       server_name  localhost;
       charset utf-8;
       location / {
           root   html;
           index  index.php index.html index.htm;
           if ($http_user_agent ~ "ApacheBench|WebBench|TurnitinBot|Sogou web spider|Grid service"){
             return 403;
          }
       }
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   html;
       }
       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;
       }
   }
include /apps/nginx/conf/conf.d/*.conf;
}
[root@node08.host.com ~]# 
  1. 重启nginx,验证php
[root@node08.host.com ~]# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@node08.host.com ~]# /apps/nginx/sbin/nginx -s reload

在这里插入图片描述

4.2 增加虚拟主机

  1. 修改配置文件,在配置文件增加虚拟主机,配置参数和虚拟主机一旦很多,很容易很长,采用多配置分开的模式如在nginx.conf http模块中引用
[root@node08.host.com ~]# cd /apps/nginx/conf/
include /apps/nginx/conf/conf.d/*.conf;
  1. 增加虚拟主机访问节点配置
[root@node08.host.com /apps/nginx/conf]# mkdir conf.d
[root@node08.host.com /apps/nginx/conf/conf.d]# cat pc.conf 
server {
   listen       80;
   server_name  172.24.248.20;
 
   location / {
       root   /data/nginx/html/pc;
       index  index.html;
      
    }

    location /images {
       root /data/nginx/images;
       index index.html;
   }
   
    error_page 500 502 503 504 404 /error.html;
    location = /error.html{
       root html;
   }
  
}    
[root@node08.host.com /apps/nginx/conf/conf.d]#

[root@node08.host.com /data/nginx]# cat /data/nginx/images/index.html 
images
[root@node08.host.com /data/nginx]# cat /data/nginx/html/pc/index.html 
pc  web
[root@node08.host.com /data/nginx]# curl 172.24.248.20
pc  web
[root@node08.host.com /data/nginx]# curl 172.24.248.20/images
images png

总结:
LNMP整体安装相对比较简单,而源码安装需要提前准备好源码包以及环境依赖。另外环境安装可以不需要过多去关注,更多的则是配置方面,因为安装好之后就不会在去处理,当然安装的方式分可选择自动化运维工具一键安装,也可以使用脚本一键安装。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值