nginx部署
//创建nginx用户
[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx
//安装开发工具包
[root@nginx ~]# yum -y groupinstall “Development Tools”
//安装依赖包和一些常用工具
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make zlib-devel wget lrzsz
//创建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx
//解压nginx源码包
[root@nginx ~]# tar -zxvf nginx-1.24.0.tar.gz -C /usr/local/
//编译安装
[root@nginx ~]# cd /usr/local/nginx-1.24.0/
[root@nginx nginx-1.24.0]# ./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.24.0]# make
[root@nginx nginx-1.24.0]# make install
//配置环境变量
[root@nginx nginx-1.24.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
//启动nginx,并查看端口情况
[root@nginx nginx-1.24.0]# nginx
[root@nginx nginx-1.24.0]# ss -anlt
- 配置文件详解
主配置文件:/usr/local/nginx/nginx.conf
默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf
可以在启动nginx时通过-c选项来指定要读取的配置文件
nginx常见的配置文件及其作用:
nginx.conf // nginx的基本配置文件
mime.types //MIME类型关联的扩展文件
fastcgi.conf //与fastcgi相关的配置
proxy.conf //与proxy相关的配置
sites.conf //配置nginx提供的网站,包括虚拟主机
Nginx.conf配置文件详解
main配置段:全局配置段,其中main配置段中可能包含event配置段
events{}:定义event模型工作特性
http{}:定义http协议相关的配置
支持使用变量:
- 内置变量:模块会提供内建变量定义
- 自定义变量:set var_name value
用于调试、定位问题的配置参数
daemon {on|off}; //是否以守护进程方式运行nginx,调试应设置为off
master_process {on|off}; //是否以master/worker模型来运行nginx,调试时可以设置为off
error_log 位置 级别; //配置错误日志
error_log里的位置和级别的一些选项
位置 | 级别 |
file stderr syslog:server=address[,parameter=vaule] memory:size | debug:若要使用debug级别,需要在编译nginx时使用--with-debug选项 info notice warn error crit alert emerg |
正常运行必备的配置参数
user USERNAME [GROUPNAME]; //指定运行worker进程的用户和组
pid /path/to/pid_file; //指定nginx守护进程的pid文件
worker_rlimit_nofile number; //设置所有worker进程最大可以打开的文件数,默认为1024
worker_rlimit_core size; //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可
优化性能的配置参数
worker_processes n; //启动n个worker进程,这里的n为了避免上下文切换,通常设置为cpu总核心数-1或等于总核心数
worker_cpu_affinity cpumask ...; //将进程绑定到某cpu中,避免频繁刷新缓存
#cpumask:使用8位二进制表示cpu核心,如:
0000 0001 #第一颗cpu核心
0000 0010 #第二颗cpu核心
0000 0100 #第三颗cpu核心
0000 1000 #第四颗cpu核心
0001 0000 #第五颗cpu核心
0010 0000 #第六颗cpu核心
0100 0000 #第七颗cpu核心
1000 0000 #第八颗cpu核心
timer_resolution interval; //计时器解析度。降低此值,可减少gettimeofday()系统调用的次数
worker_priority number; //指明worker进程的nice值
事件相关的配置:event{}段中的配置参数
accept_mutex {off|on}; //master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流的、序列化的去响应新请求
lock_file file; //accept_mutex用到的互斥锁所文件路径
use [epoll | rtsig | select | poll]; //指明使用的事件模型,建议让nginx自行选择
worker_connections 1024; //每个进程能够接受的最大连接数
网络连接相关的配置参数
keepalive_timeout number; //长连接的超时时长,默认为65s
keepalive_requests number; //在一个长连接上所能够允许请求的最大资源数
keepalive_disable [msie6|safari|none]; //为指定类型的UserAgent禁用长连接
tcp_nodelay on|off; //是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on
client_header_timeout number; //读取http请求报文首部的超时时长
client_body_timeout number; //读取http请求报文body部分的超时时长
send_timeout number; //发送响应报文的超时时长
Fastcgi的相关配置参数
LNMP:php要启用fpm模型
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000; //定义反向代理
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
常需要进行调整的参数
worker_processes
worker_connections
worker_cpu_affinity
worker_priority
http{....}:配置http相关,由ngx_http_core_module模块引入。Nginx的HTTP配置主要包括4个区块
http { //协议级别
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
gzip on;
upstream { //负载均衡配置
...
}
server { //服务器级别,每个server类似于httpd中的一个<VirtualHost>
listen 80;
server_name localhost;
location / { //请求级别,类似于httpd中的<Location>,用于定义URL与本地文件系统的映射关系
root html;
index index.html index.htm;
}
}
}
http{}端配置指定
server{}:定义一个虚拟主机
server {
listen 80;
server_name www.example.com;
root “/vhost/web”;
}
listen: 指定监听的地址和端口
listen address[:port]
listen port;
mysql部署
//部署好mysql
[root@mysql2 ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@mysql2 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37 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> exit
Bye
//配置文件
[root@mysql2 ~]# vim /etc/profile
MANDATORY_MANPATH /usr/local/mysql/man
[root@mysql2 ~]# vim /etc/man_db.conf
/usr/local/mysql/lib
php部署
//安装依赖包
[root@node3 ~]#yum -y install 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
//安装php
[root@node3 ~]#yum -y install php-*
//配置php
[root@php3 ~]# vim /etc/php-fpm.d/www.conf
listen = 0.0.0.0:9000
listen.allowed_clients = 192.168.35.142
//生成测试页面,修改所有者所属组,并重启服务
[root@php3 ~]# vim /var/www/html/index.php
[root@php3 ~]# chown -R apache.apache /var/www/html/
[root@php3 ~]# systemctl restart php-fpm.service
[root@php3 ~]# systemctl enable php-fpm.service
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
//回到nginx那台主机
//整合nginx和php
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html; //更改目录
index index.php index.html index; //添加一个index.php
}
location ~ \.php$ {
root /var/www/html; //更改目录
fastcgi_pass 192.168.100.30:9000; //这里为PHP服务器的地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
//测试nginx配置文件,重启服务
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# nginx -s stop
[root@nginx ~]# nginx