1.下载最新版本的nginx
curl -O http://nginx.org/download/nginx-1.19.0.tar.gz
2.解压
3.配置
nginx将很多的功能都进行了模块化的划分,开启某个功能其实就是加载某个模块到nginx主程序里
–with 开头的表示nginx默认情况下没有带这个功能 --》启用某个功能,默认没有开启
–without 开头的表示nginx默认情况带有这个功能 --》禁用某个功能,默认开启了
–prefix=path 指定安装路径(安装到哪个文件夹里)
–user=name 指定启动nginx使用哪个用户,此用户需要提前新建
–with-http_ssl_module 开启对https的支持
–with-stream 对4层负载均衡的支持 --》根据端口号进行转发数据
IO多路复用模型(算法):解决大并发情况下,nginx如何来响应用户的请求
[root@sc-mysql nginx-1.19.0]# ./configure --prefix=/usr/local/nginx2 --with-http_ssl_module --with-stream --user=guozy
4.安装依赖关系包
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make
5.新建用户
useradd guozy
6.编译安装方式启动和关闭nginx
[root@sc-mysql nginx-1.19.0]# cd /usr/local/nginx2/
[root@sc-mysql nginx2]# ls
conf html logs sbin
[root@sc-mysql nginx2]# cd sbin/
[root@sc-mysql sbin]# ls
nginx
[root@sc-mysql sbin]# ./nginx
[root@sc-mysql sbin]# ./nginx -s stop
7.检验是否启动
1)看进程
[root@sc-mysql system]# ps aux|grep nginx
root 13001 0.0 0.0 46084 1140 ? Ss 12:03 0:00 nginx: master process /usr/local/nginx2/sbin/nginx
guozy 13002 0.0 0.0 46512 1888 ? S 12:03 0:00 nginx: worker process
root 13079 0.0 0.1 149848 5444 pts/1 S+ 14:41 0:00 vim one_key_install_nginx.sh
root 13081 0.0 0.0 112824 980 pts/2 S+ 14:43 0:00 grep --color=auto nginx
master 进程是主进程,是一个管理进程,是worker进程的父进程
worker 是工作进程,提供web服务的进程
nginx是一个多线程提供web服务的软件,所以是一个高效的web服务器软件
2)看端口
[root@sc-mysql sbin]# lsof -i:80 查看端口
1、通过配置实现:编译安装的能使用systemctrl enable nginx去设置开机启动
文件里的路径修改为编译安装的路径
1)创建配置文件
[root@sc-nginx nginx]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx9/logs/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /usr/local/nginx9/logs/nginx.pid
ExecStartPre=/usr/local/nginx9/sbin/nginx -t
ExecStart=/usr/local/nginx9/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2)刷新服务配置文件
[root@sc-nginx nginx]# systemctl daemon-reload
2、链接
1)HTML文件存放路径
[root@sc-mysql html]# pwd
/usr/local/nginx2/html -->是nginx存放网页内容的目录
3、nginx软件的配置
1)配置文件:
/usr/local/nginx2/conf
nginx.conf
2)配置文件有何作用?
初始化一些变量
其实就是给主程序传递参数,告诉主程序如何运行,例如:启用什么端口,启用什么功能等这些配置
3)设置
http {} 对http协议相关的配置
server{} 针对一个网站的配置
4)页面解析
nginx负责解析静态页面–》.html .css .js
动态页面需要其他的解析程序去解析 --》.php .py
LNMP架构:
L --》linux
N -->NGINX
M -->MYSQL
P -->PHP PYTHON PERL
[root@sc-mysql conf]# cat nginx.conf
#user nobody; 以哪个用户启动nginx,默认情况下使用nobody启动nginx
worker_processes 2; #启动多少个工作进程,进程数建议和cpu核心数量一致,如果你的主机是8核的,启动8个进程
#为什么要和cpu的核心数量一致?
#尽量的让worker进程同时在不同的cpu里运行,不在就绪队列里排队
#error_log logs/error.log; 指定错误日志的路径和名字
#error_log logs/error.log notice; 记录notice以上消息级别的日志
#error_log logs/error.log info;
pid logs/nginx.pid; #存放nginx的master进程的pid
#采用epoll模型,去处理大并发请求
events {
worker_connections 1024; #最大的连接数是1024,同时最多处理1024个请求,一个worker进程启动1024个线程
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#定义日志记录的格式,记录那些内容
#remote_addr 用户的ip地址
#request 用户访问的页面地址->url
#http_user_agent -->浏览器和系统
access_log logs/access.log main; #指定访问日志的路径和格式
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80; #指定监听的端口
server_name www.sanchuang.com; #指定网站对应的域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html; #网站的内容的根目录在html文件夹里
index shouye.html index.html index.htm; #指定首页为index.html或者index.htm
}
error_page 404 /404.html; #404 错误的时候,当页面不存在的时候,就返回这个这个页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #500 502 503 504 错误 ,返回/50x.html页面
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}