nginx部署(超详细)
1.卸载,找到nginx位置并删除文件。
检查是否运行,停止该进程。
[root@192 sbin]# ps -ef | grep nginx
[root@192 sbin]# ./nginx -s stop
[root@192 sbin]# whereis nginx
nginx: /usr/local/nginx
[root@192 ~]# rm -rf /usr/local/nginx
查看usr/loc目录发现nginx文件无了。可以再用yum清理一下。
🆗完全清理干净了。
2.重新安装nginx。确保一切正常。
[root@192 ~]# wget https://nginx.org/download/nginx-1.23.3.tar.gz
下载此版本nginx。
没有wget
命令的可通过yum
命令安装:
# yum -y install wget
然后解压下载的文件
[root@192 ~]# tar -zxvf nginx-1.23.3.tar.gz
🆗搞定
接下来安装目前所需的依赖包,搞定
# yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
然后开始编译,搞定。必须要在nginx所在的目录内才能找到configure脚本文件执行。
[root@192 nginx-1.23.3]# ./configure
接着编译并安装nginx
[root@192 nginx-1.23.3]# make && make install
到这里基本上就差不多了,查看一下。无论你创建了什么文件夹,nginx始终会安装在usr/local/nginx 目录下,这并不影响使用,只是一个单纯的备份行为非常正常。可以在conf文件夹里使用 ls -al命令查看到非常多的配置文件。
[root@192 ~]# whereis nginx
nginx: /usr/local/nginx
[root@192 ~]# cd /usr/local/nginx
[root@192 nginx]# ll
最后最后一步回到主目录下查看nginx1.23.3文件可以看到make命令编译生成的相关配置文件。这个时候需要在usr/local/nginx 目录下启动nginx。
[root@192 ~]# cd /usr/local/nginx/
[root@192 nginx]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 2月 4 00:55 conf
drwxr-xr-x. 2 root root 40 2月 4 00:55 html
drwxr-xr-x. 2 root root 6 2月 4 00:55 logs
drwxr-xr-x. 2 root root 19 2月 4 00:55 sbin
[root@192 nginx]# cd sbin/
[root@192 sbin]# ll
总用量 3820
-rwxr-xr-x. 1 root root 3911152 2月 4 00:55 nginx
[root@192 sbin]#
启动并且查看nginx运行状态,访问本机ip主页验证成功与否。
[root@192 nginx]# cd sbin/
[root@192 sbin]# ll
总用量 3820
-rwxr-xr-x. 1 root root 3911152 2月 4 00:55 nginx
[root@192 sbin]# ./nginx
然后关闭防火墙,必须关闭不然无法访问本机nginx网页。
停止firewall,并且使用来开启关闭nginx,可以看到成功。优雅使用-s quit关闭会无法访问页面。
[root@192 sbin]# ./nginx
[root@192 sbin]# ./nginx -s quit
systemctl stop firewalld.service
nginx相关配置
反向代理-负载均衡
轮询(Round Robin,RR)
所有请求被依次分发到每台应用服务器上,即每台服务器需要处理的请求数目都相同,适合于所有服务器硬件都相同的场景。
加权轮询(Weighted Round Robin,WRR)
根据应用服务器硬件性能的情况,在轮询的基础上,按照配置的权重将请求分发到每个服务器,高性能的服务器能分配更多请求。
随机(Random)
请求被随机分配到各个应用服务器,在许多场合下,这种方案都很简单实用,因为好的随机数本身就很均衡。即使应用服务器硬件配置不同,也可以使用加权随机算法。
最少连接(Least Connections)
记录每个应用服务器正在处理的连接数(请求数),将新到的请求分发到最少连接的服务器上,应该说,这是最符合负载均衡定义的算法。同样,最少连接算法也可以实现加权最少连接。
源地址散列(Source Hashing)
根据请求来源的IP地址进行Hash计算,得到应用服务器,这样来自同一个IP地址的请求总在同一个服务器上处理,该请求的上下文信息可以存储在这台服务器上,在一个会话周期内重复使用,从而实现会话黏滞。
正向代理——为客户端向代理服务器发送请求,不想让对方服务器知晓自己的ip而是用中间件来作为依托转发。
反向代理------和正向代理刚好相反原理相同,服务器不想让客户端知道ip,所以给找代理服务器转发给客户端,刚好相反,这个情况客户端是没有任何感受的。
首先准备3台centos配置好nginx环境,前两台为web集群服务器,第三台144为负载均衡服务器,我们不需要做客户端,只是为了查看学习三台服务器端不同角色和状态。
集群服务器两台
负载均衡服务器页面如下,这里的144已经拿到142的数据了,都是已经做好了配置的
权重方法,
在nginx.conf主文件里修改两台机器的ip和weight值
配置文件非常简单,正向代理和反向代理其实基本上原理是没什么大的差别,都是为了想对方隐藏自己的IP。给137和142,1:2的有限制意味着142在本机144上的出现次数为两次137一次
nginx高可用问题
上如果采用单个节点的方式部署Nginx
,难免会出现天灾人祸,比如系统异常、程序宕机、服务器断电、机房爆炸、地球毁灭....哈哈哈,夸张了。但实际生产环境中确实存在隐患问题,由于Nginx
作为整个系统的网关层接入外部流量,所以一旦Nginx
宕机,最终就会导致整个系统不可用,这无疑对于用户的体验感是极差的,因此也得保障Nginx
高可用的特性。
接下来则会通过
keepalived
的VIP
机制,实现Nginx
的高可用。VIP
并不是只会员的意思,而是指Virtual IP
,即虚拟IP
。
keepalived
在之前单体架构开发时,是一个用的较为频繁的高可用技术,比如MySQL、Redis、MQ、Proxy、Tomcat
等各处都会通过keepalived
提供的VIP
机制,实现单节点应用的高可用。
keepalived靠什么实现高可用?
心跳检测机制,类似于ofps的hello包,每过30秒给服务器发送。架构如下
由于配置实在是太多了,就不写出来了半夜快猝死了,反正我搞定了
lnmp环境配置
L Linux(centos7)
N nginx
M MySQL(mariadb)
P php
具有成本低廉、部署灵活、快速开发、安全稳定等特点,是 Web 网络应用和环境的优秀组合。若是服务器配置比较低的个人网站,首选 LNMP。
安装nginx,mariadb,php
nginx linux 安装并配置nginx
mariadb,php
yum install nginx mariadb-server php php-mysql php-gd php-fpm -y
systemctl enable mariadb --now(开启)
mysqladmin -uroot password ‘(密码)’
更改nginx配置文件(/usr/local/nginx/conf/nginx.conf)
server {
listen 8084;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm;
}
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
}
location ~ .*\.php(\/.*)*$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
fastcgi_index index.php;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
echo "<?php phpinfo(); ?>" >/usr/share/nginx/html/test.php
重启nginx,mariadb,php-fpm
systemctl restart nginx mariadb php-fpm
测试
ssl证书
申请证书,改文件名 openssl 三个ssl文件
[root@www nginx]# openssl genrsa -des3 -out ssl.key 4096
Generating RSA private key, 4096 bit long modulus
......................................................................................++
........................................................................++
e is 65537 (0x10001)
Enter pass phrase for ssl.key:
Verifying - Enter pass phrase for ssl.key:
[root@www nginx]# openssl req -new -key ssl.key -out ssl.csr
Enter pass phrase for ssl.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:shanxi
Locality Name (eg, city) [Default City]:xianyang
Organization Name (eg, company) [Default Company Ltd]:oupeng
Organizational Unit Name (eg, section) []:IT_security
Common Name (eg, your name or your server's hostname) []:localhost
Email Address []:123456@123
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:123456
[root@www nginx]# openssl x509 -req -days 365 -in ssl.csr -signkey ssl.key -out ssl.crt
Signature ok
subject=/C=cn/ST=shanxi/L=xianyang/O=oupeng/OU=IT_security/CN=localhost/emailAddress=123456@123
Getting Private key
Enter pass phrase for ssl.key:
[root@www nginx]# cd key/
[root@www key]# ll
total 12
-rw-r--r--. 1 root root 1984 Feb 7 08:28 ssl.crt
-rw-r--r--. 1 root root 1805 Feb 7 08:27 ssl.csr
-rw-r--r--. 1 root root 3311 Feb 7 08:23 ssl.key
server {
listen 443 ssl;#https默认使用433端口
server_name 192.168.137.136
#ssl on;
ssl_certificate /usr/local/nginx/key/ssl.crt;
ssl_certificate_key /usr/local/nginx/key/ssl.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers; ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
location / {
...
}
#1.判断是否具有ssl模块 --- 输出含有configure arguments: --with-http_ssl_module
/usr/local/nginx/sbin/nginx -V
#2.cd到nginx源码解压目录
[root@www ~]cd nginx.122.1
[root@www nginx-1.22.1]#./configure --with-http_ssl_module
#3.编译执行
make
#4.备份原有已安装好的nginx
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
#5.然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态)
cp ./objs/nginx /usr/local/nginx/sbin/
#6.测试查看
[root@blackstone nginx-1.20.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_ssl_module
ok失败但我真的努力好多次真动不了,