nginx配置与lnmp部署

目录

一.nginx简介

二.nginx的特性与优点

1.特性

2.优点

三.nginx的工作原理

nginx的进程架构

四.lnmp架构

1.安装nginx

安装

配置

2.二进制安装mysql

3.安装php

配置php

浏览器测试访问


一.nginx简介

nginx(发音同engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行。

nginx由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler使用。

第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

nginx的特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等

二.nginx的特性与优点

1.特性

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

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

2.优点

  • 高并发连接:官方测试能够支撑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,内存映射)等性能优化

三.nginx的工作原理

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

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

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

nginx的进程架构

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

下图展示了nginx模块一次常规的HTTP请求和响应的过程

下图展示了基本的WEB服务请求步骤


四.lnmp架构

LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构

1.安装nginx

软件包下载地址:

nginx: download

安装

//关闭防火墙和selinux
[root@localhost ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# reboot

//创建nginx用户以及安装依赖包
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
[root@localhost ~]# yum -y groups mark install 'Development Tools'

//创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx

//下载nginx软件包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://nginx.org/download/nginx-1.20.2.tar.gz

//编译安装
[root@localhost src]# tar xf nginx-1.20.2.tar.gz 
[root@localhost src]# cd nginx-1.20.2/
[root@localhost 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@localhost src]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

[root@localhost ~]# cd /usr/local/ 
[root@localhost local]# ls  //安装完成后安装目录下会有nginx目录
bin  etc  games  include  lib  lib64  libexec  nginx  sbin  share  src

配置

//设置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# source /etc/profile.d/nginx.sh 
[root@localhost ~]# which nginx
/usr/local/nginx/sbin/nginx

//nginx启动
[root@localhost ~]# nginx 
[root@localhost ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port      Peer Address:Port   Process   
LISTEN   0        128              0.0.0.0:80             0.0.0.0:*                
LISTEN   0        128              0.0.0.0:22             0.0.0.0:*                
LISTEN   0        128                 [::]:22                [::]:* 

//nginx关闭
[root@localhost ~]# nginx -s stop
[root@localhost ~]# ss -antl
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                 [::]:22                [::]:*            

//nginx重启
[root@localhost ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port      Peer Address:Port   Process   
LISTEN   0        128              0.0.0.0:80             0.0.0.0:*                
LISTEN   0        128              0.0.0.0:22             0.0.0.0:*                
LISTEN   0        128                 [::]:22                [::]:*  


//配置service文件
[root@localhost ~]# cd /usr/lib/systemd/system/
[root@localhost system]# cp -r sshd.service nginx.service

[root@localhost system]# cat nginx.service 
[Unit]
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

[root@localhost system]# systemctl daemon-reload 

[root@localhost ~]# systemctl enable --now nginx.service   //设置开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

[root@localhost ~]# systemctl status nginx.service 
● nginx.service
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: >
   Active: active (running) since Sat 2022-09-03 21:48:46 CST; 1min 11s ago


2.二进制安装mysql

软件包下载地址:

MySQL :: Download MySQL Community Server (Archived Versions)

//创建用户并且下载依赖包
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# yum install -y ncurses-compat-libs

//下载软件包(软件包较大,建议本机下载后使用工具传到虚拟机内)
[root@localhost src]# ls
mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz  nginx-1.20.2  nginx-1.20.2.tar.gz

//解压软件包并且修改属主组
[root@localhost src]# tar xf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mv mysql-8.0.26-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# chown -R mysql.mysql mysql/

//设置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh 
[root@localhost ~]# source /etc/profile.d/mysql.sh 
[root@localhost ~]# which mysql
/usr/local/mysql/bin/mysql

//软连接 lib库 man文档
[root@localhost ~]# ln -s /usr/local/mysql/include/ /usr/include/mysql

[root@localhost ~]# cat  /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib

[root@localhost ~]# vim /etc/man_db.conf 
MANDATORY_MANPATH                       /usr/local/mysql/man



//建立数据存放目录
[root@localhost ~]# mkdir -p /opt/data
[root@localhost ~]#  chown -R mysql.mysql /opt/data/
[root@localhost ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 9月   3 22:03 data



//格式化
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/
2022-09-03T14:03:56.825183Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.26) initializing of server in progress as process 260931
2022-09-03T14:03:56.833359Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-09-03T14:03:57.477079Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-09-03T14:03:58.287217Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2022-09-03T14:03:58.287519Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2022-09-03T14:03:58.431026Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 2&a?oayBQSqt  //临时密码
[root@localhost ~]# echo '2&a?oayBQSqt' > passwd 
[root@localhost ~]# cat passwd 
2&a?oayBQSqt

//生成配置文件
[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf 
[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

//配置服务启动脚本
[root@localhost ~]# cd /usr/local/mysql/support-files/
[root@localhost support-files]# cp mysql.server mysqld
[root@localhost support-files]# vim mysqld //搜索basedir 并修改成下面这样
basedir=/usr/local/mysql
datadir=/usr/local/data


//启动mysql
[root@localhost ~]# /usr/local/mysql/support-files/mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
 SUCCESS! 
[root@localhost ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port      Peer Address:Port   Process   
LISTEN   0        128              0.0.0.0:80             0.0.0.0:*                
LISTEN   0        128              0.0.0.0:22             0.0.0.0:*                
LISTEN   0        128                 [::]:22                [::]:*                
LISTEN   0        70                     *:33060                *:*                
LISTEN   0        128                    *:3306                 *:*         


//修改mysql的root用户密码
[root@localhost ~]# mysql -uroot -p'2&a?oayBQSqt'
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 8
Server version: 8.0.26

Copyright (c) 2000, 2021, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'youk123!';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye


//测试密码

[root@localhost ~]# mysql -uroot -pyouk123!
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 9
Server version: 8.0.26 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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的主配置文件
[root@localhost ~]# /usr/local/mysql/support-files/mysqld stop  //关闭mysql
Shutting down MySQL. SUCCESS! 

[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service mysqld.service

[root@localhost system]# vim mysqld.service 
[root@localhost system]# cat mysqld.service 
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysqld start
ExecStop=/usr/local/mysql/support-files/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload 

[root@localhost ~]# systemctl enable --now mysqld.service   //设置开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost ~]# systemctl status mysqld.service 
● mysqld.service - mysql server daemon
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset:>
   Active: active (running) since Sat 2022-09-03 22:12:50 CST; 24s ago


3.安装php

软件包下载地址:

PHP: Downloads

//下载php软件包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://www.php.net/distributions/php-8.1.10.tar.gz
[root@localhost src]# ls
mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz  nginx-1.20.2.tar.gz
nginx-1.20.2                                php-8.1.10.tar.gz

//安装依赖包
[root@localhost ~]#  yum -y install autoconf freetype gd libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel net-snmp-devel libjpeg-devel php-ldap openldap-devel openldap-clients freetype-devel gmp-devel libzip libzip-devel sqlite-devel

//安装oniguruma 依赖包
[root@localhost ~]# yum install autoconf automake libtool -y
[root@localhost ~]# wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
[root@localhost ~]# tar xf oniguruma-6.9.4.tar.gz && cd oniguruma-6.9.4
[root@localhost oniguruma-6.9.4]# ./autogen.sh && ./configure --prefix=/usr
Generating autotools files.
[root@localhost oniguruma-6.9.4]# make && make install


//源码安装php
[root@localhost ~]# cd /usr/src/
[root@localhost src]# tar xf php-8.1.10.tar.gz 
[root@localhost src]# cd php-8.1.10/
[root@localhost php-8.1.10]# ./configure --prefix=/usr/local/php8 --with-config-file-path=/usr/local/php8/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-mysqlnd --with-mysqli --with-pdo-mysql --enable-opcache --with-pcre-jit --enable-gd --with-jpeg --with-freetype --with-gettext --with-curl --with-openssl --enable-sockets --enable-mbstring --enable-xml --with-zip --with-zlib --with-snmp --with-mhash --enable-ftp --enable-bcmath --enable-soap --enable-shmop --enable-sysvsem --enable-pcntl --with-gmp



//看到此画面即为成功
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.


[root@localhost php-8.1.10]# make && make install

配置php

//配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@localhost ~]# source /etc/profile.d/php8.sh 
[root@localhost ~]# which php
/usr/local/php8/bin/php

//配置php-fpm
[root@localhost ~]# cd /usr/src/php-8.1.10/
[root@localhost php-8.1.10]# cp php.ini-production /etc/php.ini
cp:是否覆盖'/etc/php.ini'? y
[root@localhost php-8.1.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-8.1.10]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-8.1.10]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@localhost php-8.1.10]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf 

//开启php
[root@localhost ~]# service php-fpm start
Starting php-fpm  done
[root@localhost ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port      Peer Address:Port   Process   
LISTEN   0        128              0.0.0.0:80             0.0.0.0:*                
LISTEN   0        128              0.0.0.0:22             0.0.0.0:*                
LISTEN   0        128            127.0.0.1:9000           0.0.0.0:*                
LISTEN   0        128                 [::]:22                [::]:*                
LISTEN   0        70                     *:33060                *:*                
LISTEN   0        128                    *:3306                 *:*    


[root@localhost ~]# vim /usr/local/nginx/nginx.conf
//在43行到46行
 location / {
   root   html;
     index index.php index.html index.htm;  //添加idnex.php
 }


//取消65行到71行注释
 location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  添加脚本文件请求的路径
            include        fastcgi_params;
        }


//编写php测试文件
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# vim index.php
[root@localhost html]# cat index.php 
<?php
phpinfo();
?>

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

浏览器测试访问

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值