nginx&lnmp部署

nginx


1.nginx简介

Nginx是一款自由的、开源的、高性能的HTTP服务器和 反向代理 服务器;同时也是一个IMAP、POP3、SMTP代理服务器;Nginx可以作为一个HTTP服务器进行网站的发布处理,另外Nginx可以作为反向代理进行负载均衡的实现。

  • Nginx使用基于事件驱动架构,使得其可以支持数以百万级别的TCP连接
  • 高度的模块化和自由软件许可证使得第三方模块层出不穷(开源)
  • Nginx是一个跨平台服务器,可以运行在Linux,Windows,FreeBSD,Solaris,AIX,Mac OS等操作系统上
  • 稳定性极高

2.nginx特性

nginx是一个很牛的高性能Web和反向代理服务器,它具有很多非常优越的特性:

  • 在高连接并发的情况下,nginx是Apache服务器不错的替代品,能够支持高达50000个并发连接数的响应
  • 使用epoll and kqueue作为开发模型
  • nginx作为负载均衡服务器:nginx既可在内部直接支持和PHP程序对外进行服务,也可支持作为HTTP代理服务器对外进行服务
  • nginx采用C进行编写,不论系统资源开销还是CPU使用效率都比Perlbal要好很多

3.nginx优点

  • 高并发连接:官方测试能够支撑5万并发连接,在实际生产环境中跑到2-3万并发连接数
  • 内存消耗少:在3万并发连接下,开启的10个nginx进程才消耗150M内存(15M*10=150M)
  • 配置文件非常简单:风格跟程序一样通俗易懂
  • 成本低廉:nginx为开源软件,可以免费使用。而购买F5 BIG-IP、NetScaler等硬件负载均衡交换机则需要十多万至几十万人民币
  • 支持Rewrite重写规则:能够根据域名、URL的不同,将HTTP请求分到不同的后端服务器群组
  • 内置的健康检查功能:如果Nginx Proxy后端的某台Web服务器宕机了,不会影响前端访问
  • 节省带宽:支持GZIP压缩,可以添加浏览器本地缓存的Header头
  • 稳定性高:用于反向代理,宕机的概率微乎其微
  • 模块化设计:模块可以动态编译
  • 外围支持好:文档全,二次开发和模块较多
  • 支持热部署:可以不停机重载配置文件
  • 支持事件驱动、AIO(AsyncIO,异步IO)、mmap(Memory Map,内存映射)等性能优化

4.nginx的功能及应用类别

4.1nginx的基本功能
  • 静态资源的web服务器,能缓存打开的文件描述符
  • http、smtp、pop3协议的反向代理服务器
  • 缓存加速、负载均衡
  • 支持FastCGI(fpm,LNMP),uWSGI(Python)等
  • 模块化(非DSO机制),过滤器zip、SSI及图像的大小调整
  • 支持SSL
4.2 nginx的扩展功能
  • 基于名称和IP的虚拟主机
  • 支持keepalive
  • 支持平滑升级
  • 定制访问日志、支持使用日志缓冲区提高日志存储性能
  • 支持URL重写
  • 支持路径别名
  • 支持基于IP及用户的访问控制
  • 支持速率限制,支持并发数限制
4.3nginx的应用类别
  • 使用nginx结合FastCGI运行PHP、JSP、Perl等程序
  • 使用nginx作反向代理、负载均衡、规则过滤
  • 使用nginx运行静态HTML网页、图片
  • nginx与其他新技术的结合应用

5. nginx的工作原理

nginx的模块直接被编译进nginx,因此属于静态编译方式。

启动nginx后,nginx的模块被自动加载,与Apache不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。

在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。

nginx的进程架构:
启动nginx时,会启动一个Master进程,这个进程不处理任何客户端的请求,主要用来产生worker线程,一个worker线程用来处理n个request。

6.部署nginx

//创建用户
[root@nginx ~]# useradd -rMs /sbin/nologin nginx
//安装依赖工具
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@nginx ~]# yum -y groups mark install 'Development Tools'
//创建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx
[root@nginx ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz
//解压&编译安装
[root@nginx ~]# tar -xf nginx-1.20.2.tar.gz
[root@nginx ~]# cd nginx-1.20.2/
[root@nginx nginx-1.20.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log

[root@nginx nginx-1.20.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//配置环境变量
[root@nginx nginx-1.20.2]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx src]# . /etc/profile.d/nginx.sh
[root@nginx src]# nginx
[root@nginx ~]# vim /usr/lib/systemd/system/nginx.service
[root@nginx ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@nginx nginx]# systemctl enable --now nginx
[root@nginx nginx]# ss -anlt
State    Recv-Q   Send-Q       Local Address:Port       Peer Address:Port   Process
LISTEN   0        128                0.0.0.0:22              0.0.0.0:*
LISTEN   0        128                0.0.0.0:80              0.0.0.0:*
LISTEN   0        128                   [::]:22                 [::]:*

//服务控制方式,使用nginx命令
    -t  //检查配置文件语法
    -v  //输出nginx的版本
    -c  //指定配置文件的路径
    -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload}


```text
//服务控制方式,使用nginx命令
    -t  //检查配置文件语法
    -v  //输出nginx的版本
    -c  //指定配置文件的路径
    -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload}

7.部署lnmp,分离部署

服务类型IP应用系统
nginx192.168.159.100nginxcentoos8
mysql192.168.159.101mysqlcentos8
php192.168.159.102phpcentos8
//nginx部署省略,步骤在上面
mysql服务配置
//安装依赖包
[root@mysql ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//创建用户
[root@mysql ~]# useradd -rMs /sbin/nologin mysql
//下载mysql包解压
[root@mysql ~]# ls			//我是将事先下载好的包拉取进来的
anaconda-ks.cfg  mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz  pubic
[root@mysql ~]# tar -xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@mysql ~]# ls
anaconda-ks.cfg                      mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
mysql-5.7.38-linux-glibc2.12-x86_64  pubic
[root@mysql ~]# mv mysql-5.7.38-linux-glibc2.12-x86_64 /usr/local/mysql/
//修改MySQL的属主和属组
[root@mysql mysql]# chown -R mysql.mysql /usr/local/mysql
[root@mysql mysql]# ll
total 272
drwxr-xr-x  2 mysql mysql   4096 Oct 11 14:26 bin
drwxr-xr-x  2 mysql mysql     55 Oct 11 14:27 docs
drwxr-xr-x  3 mysql mysql   4096 Oct 11 14:26 include
drwxr-xr-x  5 mysql mysql    230 Oct 11 14:27 lib
-rw-r--r--  1 mysql mysql 259251 Mar 22  2022 LICENSE
drwxr-xr-x  4 mysql mysql     30 Oct 11 14:26 man
-rw-r--r--  1 mysql mysql    566 Mar 22  2022 README
drwxr-xr-x 28 mysql mysql   4096 Oct 11 14:27 share
drwxr-xr-x  2 mysql mysql     90 Oct 11 14:27 support-files
//添加环境变量
[root@mysql mysql]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@mysql mysql]# source /etc/profile.d/mysql.sh

//配置头文件,库文件,man帮助文档
[root@mysql mysql]# ln -s /usr/local/mysql/include/ /usr/include/mysql
[root@mysql mysql]# vim /etc/ld.so.conf.d/mysql.conf
[root@mysql mysql]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@mysql mysql]# vim /etc/man_db.conf
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/mysql/man

//建立数据存放位置
[root@mysql mysql]# mkdir /opt/data
[root@mysql mysql]# chown -R mysql.mysql /opt/data

//初始化数据库
[root@mysql mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data
2022-10-11T06:36:40.828604Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-11T06:36:41.409159Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-11T06:36:41.577246Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-11T06:36:41.581122Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 10ff4e51-492f-11ed-ba79-000c29163b39.
2022-10-11T06:36:41.581798Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-11T06:36:41.920229Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T06:36:41.920268Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T06:36:41.920590Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-11T06:36:41.989246Z 1 [Note] A temporary password is generated for root@localhost: lja+/T2EBDaM

//生成配置文件
[root@mysql mysql]# cat >/etc/my.cnf <<EOF
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF

//配置mysql的service配置文件
[root@mysql mysql]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service
[root@mysql mysql]# vim /usr/lib/systemd/system/mysqld.service
[root@mysql mysql]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@mysql mysql]# systemctl daemon-reload
[root@mysql mysql]# systemctl restart mysqld.service
//用临时密码登录前需要安装一个软件
[root@mysql mysql]# dnf -y install ncurses-compat-libs
[root@mysql mysql]# mysql -uroot -p'lja+/T2EBDaM'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye
[root@mysql mysql]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

//配置php服务
关闭防火墙和selinux
[root@php ~]# systemctl disable --now firewalld.service
[root@php ~]# vim /etc/selinux/config
SELINUX=disabled
//配置yum源
[root@php yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--   100  2495  100  2495    0     0   3637      0 --:--:-- --:--:-- --:--:--  3631
[root@php yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@php yum.repos.d]# ls
CentOS-Base.repo
[root@php ~]# yum -y install epel-release
//下载php包
[root@php ~]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
[root@php ~]# ls
anaconda-ks.cfg  php-8.1.11.tar.gz
[root@php ~]# tar -xf php-8.1.11.tar.gz -C /usr/local/src
[root@php ~]# ls
anaconda-ks.cfg  php-8.1.11  php-8.1.11.tar.gz
//下载相关依赖包
[root@php ~]# dnf -y install https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/m/mhash-0.9.9.9-20.el8.x86_64.rpm	
[root@php ~]# dnf -y install https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/l/libmcrypt-devel-2.5.8-26.el8.x86_64.rpm
[root@php ~]# dnf -y install https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/l/libmcrypt-2.5.8-26.el8.x86_64.rpm
[root@php ~]# dnf -y install https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/m/mhash-devel-0.9.9.9-20.el8.x86_64.rpm
[root@php ~]# dnf -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
[root@php ~]# rpm -ivh https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/l/libsqlite3x-devel-20071018-26.el8.x86_64.rpm --nodeps
[root@php ~]# dnf -y install wget gcc gcc-c++ make libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel  readline readline-devel libxslt libxslt-devel  php-mysqlnd  libzip-devel  sqlite-devel

//编译安装
[root@php php-8.1.11]#  ./configure --prefix=/usr/local/php  --with-config-file-path=/etc  --enable-fpm  --disable-debug  --disable-rpath  --enable-shared  --enable-soap  --with-openssl  --enable-bcmath  --with-iconv  --with-bz2  --enable-calendar  --with-curl  --enable-exif   --enable-ftp  --enable-gd  --with-jpeg  --with-zlib-dir  --with-freetype  --with-gettext  --enable-mbstring  --enable-pdo  --with-mysqli=mysqlnd  --with-pdo-mysql=mysqlnd  --with-readline  --enable-shmop  --enable-simplexml  --enable-sockets  --with-zip  --enable-mysqlnd-compression-support  --with-pear  --enable-pcntl  --enable-posix

[root@php php-8.1.11]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

//配置服务
[root@php php-8.1.11]# cd /usr/local/php
[root@php php]# ls
bin  etc  include  lib  php  sbin  var
[root@php php]# cp etc/php-fpm.conf.default etc/php-fpm.conf
[root@php php]# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf

[root@php php]# vim /usr/lib/systemd/system/php.service
[root@php php]# cat /usr/lib/systemd/system/php.service
[Unit]
Description=php server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=ps -ef |grep php |grep -v grep|awk '{print}'|xargs kill -9
ExecReload=/bin/kill -HUP

[Install]
WantedBy=multi-user.target
[root@php php]# systemctl daemon-reload
[root@php php]# systemctl enable --now php.service
Created symlink /etc/systemd/system/multi-user.target.wants/php.service → /usr/lib/systemd/system/php.service.

[root@php php]# ss -anlt
State    Recv-Q   Send-Q       Local Address:Port       Peer Address:Port   Process
LISTEN   0        128              127.0.0.1:9000            0.0.0.0:*
LISTEN   0        128                0.0.0.0:22              0.0.0.0:*
LISTEN   0        128                   [::]:22                 [::]:*

php端,nginx连接php

//编写测试主页php端
[root@php php]# mkdir -p /usr/local/nginx/html
[root@php php]# cat > /usr/local/nginx/html/index.php << EOF
> <?php
>     phpinfo();
> ?>
> EOF
[root@php php]# cd /usr/local/nginx/html/
[root@php html]# ls
index.php
[root@php html]# cat index.php
<?php
    phpinfo();
?>

[root@php html]# vim /usr/local/php/etc/php-fpm.d/www.con
listen = 192.168.159.102:9000
listen.allowed_clients = 192.168.159.100
[root@php html]# systemctl restart php.service

nginx端

[root@nginx ~]# vim /usr/local/nginx/html/index.php
[root@nginx ~]# cat /usr/local/nginx/html/index.php
<?php
    phpinfo();
?>


 location / {
            root   html;
            index  index.html index.htm index.php;			//添加index.php
        }
....
//取消注释下面这一段
 location ~ \.php$ {
            root           html;
            fastcgi_pass   192.168.159.102:9000;				//修改为php端的ip
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;	//匹配root指定的目录
            include        fastcgi_params;
        }

//重启nginx服务
[root@nginx ~]# systemctl restart nginx.service

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1we11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值