点击量高的网站,动力网站建设,空间坐标系做图网站,html网页爱心代码文章目录 1.nginx编译安装脚本2.nginx平滑升级#xff0c;以及其步骤3.nginx核心配置#xff0c;及实现nginx多虚拟主机4.nginx日志格式定制5.nginx反向代理及https安全加密6.基于LNMP和Redis的phpmyadmin的会话保持#xff0c;以及其完整步骤 1.nginx编译安装脚本
#编译安… 文章目录 1.nginx编译安装脚本2.nginx平滑升级以及其步骤3.nginx核心配置及实现nginx多虚拟主机4.nginx日志格式定制5.nginx反向代理及https安全加密6.基于LNMP和Redis的phpmyadmin的会话保持以及其完整步骤 1.nginx编译安装脚本
#编译安装脚本
#!/bin/bash
nginx_version1.20.2
nginx_filenginx-${nginx_version}.tar.gz
cpulscpu | grep ^CPU(s): | tr -d |awk -F: { print $2 }
install_dir/data/web
usernginx
groupnginx
urlhttps://nginx.org/download
. /etc/os-release
if [[ $ID ~ rhel|rocky|centos ]];thensystemctl disable --now firewalldyum install gcc make gcc-c wget pcre-devel openssl-devel zlib-devel -y
elif [[ $ID ~ ubuntu ]];thenapt updateapt install gcc make gcc-c wget pcre-devel openssl-devel zlib-devel -y
else echo 不支持此系统退出exit
fi
if [ ! -f ${nginx_file} ];thenwget ${url}/${nginx_file} #下载nginx
fi
useradd -s /sbin/nologin ${user}
mkdir -p ${install_dir}
chown ${user}.${group} -R ${install_dir}
tar zxf ${nginx_file} -C /usr/local/src #解压缩下载的nginx
cd /usr/local/src/nginx-${nginx_version} #进入下载的nginx目录
./configure --prefix${install_dir} --without-http_rewrite_module --without-http_gzip_module --user${user} --group${group} --pid-path${install_dir}/run/nginx.pid --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module #检查当前的环境是否满足要安装软件的依赖关系
make -j ${cpu} make install #编译安装
echo PATH${install_dir}/sbin:${PATH} /etc/profile.d/nginx.sh
ln -s ${install_dir}/sbin/nginx /usr/sbin
echo hostname -I | cut -d -f1 ${install_dir}/html/index.html #创建web站点到初始页面
cat /usr/lib/systemd/system/nginx.service EOF
[Unit]
DescriptionThe nginx HTTP and reverse proxy server
Afternetwork-online.target remote-fs.target nss-lookup.target
Wantsnetwork-online.target[Service]
Typeforking
PIDFile${install_dir}/run/nginx.pid
ExecStartPre/usr/bin/rm -f ${install_dir}/run/nginx.pid
ExecStartPre/usr/sbin/nginx -t
ExecStart/usr/sbin/nginx
ExecReload/usr/sbin/nginx -s reload
KillSignalSIGQUIT
TimeoutStopSec5
KillModemixed
PrivateTmptrue[Install]
WantedBymulti-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx /dev/null
echo 安装成功!,并启动服务
echo 请访问:http://hostname -I | cut -d -f12.nginx平滑升级以及其步骤
#平滑升级脚本
#!/bin/bash
old_version1.20.1
new_version1.22.1
nginx_filenginx-${new_version}.tar.gz
cpulscpu | grep ^CPU(s): | tr -d |awk -F: { print $2 }
install_dir/data/web
usernginx
groupnginx
urlhttps://nginx.org/download/
if [ ! -f ${nginx_file} ];thenwget ${url}${nginx_file} #下载nginx
fi
tar zxf ${nginx_file} -C /usr/local/src #解压缩下载的nginx
cd /usr/local/src/nginx-${new_version} #进入下载的nginx目录
./configure --prefix${install_dir} --without-http_rewrite_module --without-http_gzip_module --user${user} --group${group} --pid-path${install_dir}/run/nginx.pid --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module #检查当前的环境是否满足要安装软件的依赖关系
make -j ${cpu}
mv ${install_dir}/sbin/nginx ${install_dir}/sbin/nginx.old
cp /usr/local/src/nginx-${new_version}/objs/nginx ${install_dir}/sbin/
kill -s USR2 cat ${install_dir}/run/nginx.pid
kill -s WINCH cat ${install_dir}/run/nginx.pid.oldbin
kill -s QUIT cat ${install_dir}/run/nginx.pid.oldbin 步骤
将旧Nginx二进制文件换成新Nginx程序文件注意先备份)向master进程发送USR2信号master进程修改pid文件名加上后缀.oldbin,成为nginx.pid.oldbinmaster进程用新Nginx文件启动新master进程成为旧master的子进程,系统中将有新旧两个Nginx主进程共同提供Web服务,当前新的请求仍然由旧Nginx的worker进程进行处理,将新生成的master进程的PID存放至新生成的pid文件nginx.pid向旧的Nginx服务进程发送WINCH信号使旧的Nginx worker进程平滑停止向旧master进程发送QUIT信号关闭老master并删除Nginx.pid.oldbin文件如果发现升级有问题,可以回滚∶向老master发送HUP向新master发送QUIT
3.nginx核心配置及实现nginx多虚拟主机
[rootserver ~]# vim /etc/nginx/nginx.conf
user nginx nginx; #nginx的启动用户/组
worker_processes auto; #启动工作进程数数量
error_log /var/log/nginx/error.log; #nginx的错误日志路径
pid /run/nginx.pid; #Nginx的PID路径
include /usr/share/nginx/modules/*.conf;
#events设置快主要影响nginx服务器与用户的网络连接比如是否允许同时接受多个网络连接使用哪种事件驱动模型处理请求每个工作进程可以同时支持的最大连接数是否开启对多工作进程下的网络连接进行序列化等。
events {worker_connections 1024; #设置单个nginx工作进程可以接受的最大并发作为web服务器的时候最大并发数为worker_connections * worker_processes作为反向代理的时候为(worker_connections * worker_processes)/2
}
#http块是Nginx服务器配置中的重要部分缓存、代理和日志格式定义等绝大多数功能和第三方模块都可以在这设置http块可以包含多个server块而一个server块中又可以包含多个location块server块可以配置文件引入、MIME-Type定义、日志自定义、是否启用sendfile、连接超时时间和单个链接的请求上限等。
http {log_format main $remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for; #日志自定义格式类型默认为该类型可以自定义access_log /var/log/nginx/access.log main; ##nginx的成功日志路径以及其使用的日志格式sendfile on;#作为web服务器的时候打开sendfile加快静态文件传输指定是否使用sendfile系统调用来传输文件,sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操作)从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝操作效率很高被称之为零拷贝硬盘 kernel buffer (快速拷贝到kernelsocket buffer) 协议栈。tcp_nopush on;tcp_nodelay on;keepalive_timeout 65; #长连接超时时间单位是秒types_hash_max_size 2048;include /etc/nginx/mime.types;default_type application/octet-stream;include /etc/nginx/conf.d/*.conf;
#设置一个虚拟机主机可以包含自己的全局快同时也可以包含多个location模块。比如本虚拟机监听的端口、本虚拟机的名称和IP配置多个server 可以使用一个端口比如都使用80端口提供web服务server {listen 80 default_server; #配置ipv4格式server监听的端口listen [::]:80 default_server; #配置ipv6格式server监听的端口server_name _; #本server的名称当访问此名称的时候nginx会调用当前serevr内部的配置进程匹配。root /usr/share/nginx/html; #相当于默认页面的目录名称默认是安装目录的相对路径可以使用绝对路径配置。include /etc/nginx/default.d/*.conf; #导入其他路径的配置文件location / {#location其实是server的一个指令为nginx服务器提供比较多而且灵活的指令都是在location中体现的主要是基于nginx接受到的请求字符串对用户请求的UIL进行匹配并对特定的指令进行处理包括地址重定向、数据缓存和应答控制等功能都是在这部分实现另外很多第三方模块的配置也是在location模块中配置。}error_page 404 /404.html; #错误页面的文件名称location /40x.html { #location处理对应的不同错误码的页面定义到/40x.html}error_page 500 502 503 504 /50x.html; #错误页面的文件名称location /50x.html { #location处理对应的不同错误码的页面定义到/50x.html}}
#加密相关
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate /etc/pki/nginx/server.crt;
# ssl_certificate_key /etc/pki/nginx/private/server.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILESYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location /50x.html {
# }
# }
#和邮件相关的配置
#mail {
# ...
# } mail 协议相关配置段
#stream {
# ...
# } stream 服务器相关配置段 多虚拟主机配置
[rootserver~]# vim conf.sh
#!/bin/bash
port80
root_dir/data/html
yum install nginx -y
cat /etc/nginx/conf.d/web.conf EOF
server{listen ${port};server_name www.web.com;root ${root_dir}/web/;
}
EOF
cat /etc/nginx/conf.d/app.conf EOF
server {listen ${port};server_name www.app.com;root ${root_dir}/app/;
}
EOF
mkdir -p ${root_dir}/{web,app}
chown -R nginx.nginx ${root_dir}
echo this is website /data/html/web/index.html
echo this is appsite /data/html/app/index.html
[rootserver~]# nginx -s reload
[rootclient ~]# echo 10.0.0.6 www.app.com /etc/hosts
[rootclient ~]# echo 10.0.0.6 www.web.com /etc/hosts
[rootclient ~]# curl www.app.comthis is appsite
[rootclient ~]# curl www.web.comthis is website4.nginx日志格式定制 log_format json {timestamp:$time_iso8601,server_host:$server_addr,client_host:$remote_addr,size:$body_bytes_sent,responsetime:$request_time, upstreamtime:$upstream_response_time,upstreamhost:$upstream_addr, http_url:$host$uri,xff:$http_x_forwarded_for,referer:$http_referer,tcp_xff:$proxy_protocol_addr,http_user_agent:$http_user_agent,status:$status};主配置文件定义日志类型
在/etc/nginx/conf.d/web.conf中进行引用该日志文件格式
[rootserver ~]# cat /etc/nginx/conf.d/web.conf
server{listen 80;server_name www.web.com;root /data/html/web/;access_log /var/log/nginx/web.log json;
}
在客户端进行触发访问操作并在服务器端观察日志生成情况
[rootclient ~]# curl www.web.com
[rootserver ~]# tail -f /var/log/nginx/web.log5.nginx反向代理及https安全加密
https加密实现
创建证书脚本
#!/bin/bash
CA_SUBJECT/Olll/CNca.lll.com
SUBJECT/CCN/STHB/LHB/Olll/CNwww.lll.com
SERIAL34
EXPIRE203006
FILElll.com
openssl req -x509 -newkey rsa:2048 -subj $CA_SUBJECT -keyout ca.key -nodes -days 365 -out ca.crt
openssl req -newkey rsa:2048 -nodes -keyout ${FILE}.key -subj $SUBJECT -out ${FILE}.csr
openssl x509 -req -in ${FILE}.csr -CA ca.crt -CAkey ca.key -set_serial $SERIAL -days $EXPIRE -out ${FILE}.crt
chmod 600 ${FILE}.key ca.key
[rootcentos8 pki]# cat lll.com.crt ca.crt www.lll.com.crt
[rootcentos8 pki]# mv lll.com.key www.lll.com.key
[rootcentos8 pki]# cat /etc/nginx/conf.d/web.conf
server{listen 80;listen 443;ssl_certificate /etc/ssl/pki/www.lll.com.crt;ssl_certificate_key /etc/ssl/pki/www.lll.com.key;ssl_session_cache shared:sslcache:20m;ssl_session_timeout 10m;server_name www.web.com;root /data/html/web/;access_log /data/html/web/log/access_web.log json;}在本机电脑更改hosts文件指向该server的地址并用浏览器进行访问即可 nginx反向代理简单实现 反向代理reverse proxy指的是代理外网用户的请求到内部的指定的服务器并将数据返回给用户的一种方式这是用的比较多的一种方式。
[rootproxy_server ~]# vim /etc/nginx/conf.d/web.conf
server{listen 80;server_name www.web.com;location /{proxy_pass http://10.0.0.4;}
}
[rootproxy_server ~]# nginx -s reload
[rootserver ~]# yum install httpd -y
[rootserver ~]# echo hostnamectl /var/www/html/index.html
[rootclient ~]# echo 10.0.0.6 www.web.com /etc/hosts
[rootclient ~]# curl www.web.com6.基于LNMP和Redis的phpmyadmin的会话保持以及其完整步骤 使用的环境为Ubuntu22.04
[rootubuntu2004 ~]# cat /etc/os-release
PRETTY_NAMEUbuntu 22.04.4 LTS
NAMEUbuntu
VERSION_ID22.04
VERSION22.04.4 LTS (Jammy Jellyfish)
VERSION_CODENAMEjammy
IDubuntu1.安装包
[rootubuntu2004 ~]#apt install nginx mysql-server php8.1-fpm php8.1-mysql php8.1-curl php8.1-gd php8.1-intl php8.1-xml php8.1-zip php-redis redis-server -y 2.对php.ini进行配置
[rootubuntu2004 ~]#vim /etc/php/8.1/fpm/php.inidate.timezone Asia/Shanghaipost_max_size 8Mupload_max_filesize 100Mdisplay_errors Onerror_log syslog
extensionredis.so3.对www.conf进行配置
[rootubuntu2004 ~]#vim /etc/php/8.1/fpm/pool.d/www.conf
user www-data
group www-data
listen 127.0.0.1:9000
pm.status_path /php-status
ping.path /ping
access.log log/$pool.access.log
slowlog log/$pool.log.slow
php_value[session.save_handler] redis
php_value[session.save_path] tcp://127.0.0.1:6379 #指定Redis地址4.创建日志文件配置并授权
[rootubuntu2004 ~]# mkdir -p /usr/log
[rootubuntu2004 ~]# touch /usr/log/www.access.log
[rootubuntu2004 ~]# chmod 644 /usr/log/www.access.log
[rootubuntu2004 ~]# chown www-data:www-data /usr/log/www.access.log5.配置数据库创建用户
[rootubuntu2004 ~]# mysql
mysql create user admin127.0.0.1 identified with mysql_native_password by 123456;
mysql grant all on *.* to admin127.0.0.1;
mysql quit;6.创建nginx文件
[rootubuntu2004 ~]# vim /etc/nginx/conf.d/test.conf
server{listen 80;server_name 10.0.0.5;root /data/www;index index.php;client_max_body_size 20m;location ~ \.php$|/ping|/status {root /data/www;fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;include fastcgi_params;}}7.启动服务
[rootubuntu2004 ~]# systemctl restart nginx.service redis-server.service mysql.service php8.1-fpm.service8.进行验证 9.下载phpmyadmin
[rootubuntu2004 ~]# wget https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.zip
[rootubuntu2004 ~]# unzip phpMyAdmin-5.2.0-all-languages.zip
[rootubuntu2004 ~]# mv phpMyAdmin-5.2.0-all-languages/* /data/www
[rootubuntu2004 ~]# cp /data/www/config.sample.inc.php config.inc.php
[rootubuntu2004 ~]# vim /data/www/config.inc.php
$cfg[Servers][$i][host] 127.0.0.1; 10.测试网站是否注销
[rootubuntu2004 ~]# redis-cli
127.0.0.1:6379 keys *
1) PHPREDIS_SESSION:i5m1ikcn641d7f3mvpo3mnh1i7
127.0.0.1:6379 type PHPREDIS_SESSION:i5m1ikcn641d7f3mvpo3mnh1i7
string#删除session然后刷新浏览器看是否注销
127.0.0.1:6379 flushall
OK