20.Nginx 服务器
文章目录
- Nginx 服务器
- 安装 nginx
- 虚拟主机
- 根据名称
- 根据 port
- 配置SSL/TLS
- 生成证书
- 配置站点
- 支持动态脚本
- 使用 PHP
- 项目实战:ecshop
- ecshop 介绍
- ecshop 安装
- 准备 LNMP 环境
- 准备 Nginx
- 准备 PHP
- 准备 Mariadb
- 准备数据库
- 准备 ecshop 站点
- 反向代理
- 角色说明
- 代理服务器配置
Nginx 服务器
Nginx是一款高性能的HTTP和反向代理服务器,能够选择高效的epoll、kqueue、eventport最为网络I/O模型,在高连接并发的情况下,能够支持高达5万个并发连接数的响应,而内存、CPU等系统资源消耗却非常低,运行非常稳定。
安装 nginx
# 安装 nginx
[root@server ~ 11:05:17]# yum install -y nginx# 启动 nginx
[root@server ~ 11:05:32]# systemctl enable nginx.service --now
虚拟主机
同一个web服务器提供多个站点。
根据名称
# 参考
[root@server ~ 11:37:59]# vim /etc/nginx/nginx.conf[root@server ~ 11:41:59]# mkdir /usr/share/nginx/www{1,2}
[root@server ~ 11:42:11]# echo hello www1 > /usr/share/nginx/www1/index.html
[root@server ~ 11:42:25]# echo hello www2 > /usr/share/nginx/www2/index.html[root@server ~ 11:38:53]# vim /etc/nginx/conf.d/vhost-name.conf
server {server_name www1.ghl.cloud;root /usr/share/nginx/www1;
}
server {server_name www2.ghl.cloud;root /usr/share/nginx/www2;
}[root@server ~ 11:42:32]# systemctl restart nginx.service
根据 port
[root@server ~ 11:45:35]# mkdir /usr/share/nginx/808{1,2}
[root@server ~ 11:45:47]# echo 8081 >/usr/share/nginx/8081/index.html
[root@server ~ 11:45:54]# echo 8082 >/usr/share/nginx/8082/index.html[root@server ~ 11:43:34]# vim /etc/nginx/conf.d/vhost-port.conf
server {listen 8081;server_name www.ghl.cloud;root /usr/share/nginx/8081;
}
server {listen 8082;server_name www.ghl.cloud;root /usr/share/nginx/8082;
}[root@server ~ 11:46:05]# systemctl restart nginx
配置SSL/TLS
生成证书
[root@server ~ 11:46:13]# cd /etc/nginx/conf.d/
[root@server conf.d 11:46:45]# mkdir vhosts
[root@server conf.d 11:46:54]# mv vhost-* vhosts#--1--生成私钥
[root@server ~ 11:05:34]# openssl genrsa -out www.key 2048
Generating RSA private key, 2048 bit long modulus
...............+++
..............+++
e is 65537 (0x10001)#--2--生成请求文件csr
[root@server ~ 11:05:53]# openssl req -new -key www.key -out www.csr -subj "/C=CN/ST=JS/L=NJ/O=LM/OU=DEVOPS/CN=www.ghl.cloud/emailAddress=ghl@ghl.cloud"#--3--使用自己的私钥对请求文件签名,以生成证书
[root@server ~ 11:06:18]# openssl x509 -req -days 3650 -in www.csr -signkey www.key -out www.crt
配置站点
[root@server ~ 11:06:35]# mkdir /etc/ssl/certs/www.ghl.cloud
[root@server ~ 11:06:53]# mv www* /etc/ssl/certs/www.ghl.cloud/[root@server ~ 11:07:26]# cp /etc/nginx/nginx.conf /etc/nginx/conf.d/www.ghl.cloud-ssl.conf
[root@server ~ 11:09:43]# vim /etc/nginx/conf.d/www.ghl.cloud-ssl.confserver {listen 443 ssl http2;listen [::]:443 ssl http2;server_name www.ghl.cloud;root /usr/share/nginx/html;ssl_certificate "/etc/ssl/certs/www.ghl.cloud/www.crt";ssl_certificate_key "/etc/ssl/certs/www.ghl.cloud/www.key";ssl_session_cache shared:SSL:1m;ssl_session_timeout 10m;ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;
}[root@server ~ 11:10:55]# systemctl restart nginx.service
[root@server ~ 11:11:05]# echo hello > /usr/share/nginx/html/index.html
# 用网页搜索域名,发现打不开# 配置HTTP重定向到https.
[root@server ~ 12:52:50]# vim /etc/nginx/conf.d/www.ghl.cloud-ssl.conf
server {listen 80;listen [::]:80;server_name www.ghl.cloud;root /usr/share/nginx/html;# 添加重定向return 301 https://$host$request_uri;
}
[root@server ~ 12:49:02]# systemctl restart nginx.service # 测试
[root@client ~ 12:41:07]# curl http://www.ghl.cloud
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@client ~ 13:02:02]# curl -k http://www.ghl.cloud
支持动态脚本
使用 PHP
# 安装PHP和php-fpm
[root@server ~ 12:58:55]# yum install -y php php-fpm
# php-fpm: 负责接收web程序发来的php代码
# php:负责解析和执行php代码,并将结果返回给php-fpm
# php-fpm 将结果返回给web程序,web程序将结果返回给客户端# 建议把其他的扩展包一起安装
[root@server ~ 13:03:31]# yum install -y php-gd php-common php-pear php-mbstring php-mcrypt# 查看 php 版本
[root@server ~ 13:06:00]# php -v
PHP 5.4.16 (cli) (built: Apr 1 2020 04:07:17)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies# 测试 php 是否正常
[root@server ~ 13:07:58]# echo "<?php echo 'PHP Test Page'.\"\n\"; ?>" > php_test.php
[root@server ~ 13:08:16]# php php_test.php
PHP Test Page# 准备测试页,使用phpinfo查看详细信息
[root@server ~ 13:08:46]# echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php# 修改配置文件
[root@server ~ 13:09:02]# vim /etc/nginx/conf.d/www.ghl.cloud-ssl.conf
# add into the [server] section
server {.....location ~ \.php$ {try_files $uri =404;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}
}# 测试
[root@client ~ 13:02:14]# curl -k https://www.ghl.cloud/info.php
<?php phpinfo(); ?>
项目实战:ecshop
ecshop 介绍
官网
ECShop多场景在线商城。
ecshop 安装
准备 LNMP 环境
准备 Nginx
# 安装
[root@server ~ 14:41:30]# yum install -y nginx
[root@server ~ 14:41:34]# systemctl enable nginx.service --now
[root@server ~ 14:41:34]# echo "Nginx" > /usr/share/nginx/html/index.html
# 测试
[root@server ~ 14:45:24]# curl http://www.ghl.cloud/
Nginx
准备 PHP
# PHP 5.4.16
[root@server ~ 16:39:22]# yum install -y php php-fpm
[root@server ~ 16:41:32]# systemctl enalbe php-fpm.service --now# 配置虚拟主机
[root@server ~ 16:41:52]# vim /etc/nginx/conf.d/vhost-www.ghl.cloud.conf
server {listen 80;listen [::]:80;server_name www.ghl.cloud;root /usr/share/nginx/html;index index.php;location ~ \.php$ {try_files $uri =404;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}
}
[root@server ~ 16:43:13]# systemctl restart nginx# 准备测试页面
[root@server ~ 16:45:00]# echo "<?php echo 'PHP Test Page'.\"\n\"; ?>" > /usr/share/nginx/html/test.php# 客户端测试
[root@client-test ~ 14:45:35]# curl http://www.ghl.cloud/test.php
PHP Test Page
准备 Mariadb
# Mariadb 5.5.68-1.el7
[root@server ~ 17:09:49]# yum install -y mariadb-server
[root@server ~ 17:09:54]# systemctl enable mariadb --now## 安全初始化
# 设置root密码为redhat
# 删除匿名用户
# 删除测试数据库
[root@server ~ 17:10:25]# mysql_secure_installation
准备数据库
[root@server ~ 13:43:27]# mysql -u root -p123
MariaDB [(none)]> create database ecshop;
MariaDB [(none)]> create user ecshop@localhost identified by '123';
MariaDB [(none)]> grant all privileges on ecshop.* to ecshop@localhost;
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit
准备 ecshop 站点
准备 ecshop 站点配置文件。
[root@server ~ 13:56:03]# vim /etc/nginx/conf.d/vhost-www.ghl.cloud-ssl.conf
server {listen 80;listen [::]:80;server_name www.ghl.cloud;root /usr/share/nginx/html;index index.php;location ~ \.php$ {try_files $uri =404;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}
}
[root@server ~ 13:55:26]# systemctl restart nginx.service
准备 ecshop 站点数据文件,这里使用 ECShop_V4.1.20 版本。
[root@server ~ 13:43:49]# wget http://192.168.49.100/01.softwares/ECShop_V4.1.20_UTF8.zip# 上传到 root 用户家目录
[root@server ~ 13:46:24]# unzip ECShop_V4.1.20_UTF8.zip
[root@server ~ 15:44:18]# mv /usr/share/nginx/html/ /usr/share/nginx/html.ori
[root@server ~ 15:44:32]# cp -a ECShop_V4.1.20_UTF8_release20250416/source/ecshop/ /usr/share/nginx/html
[root@server ~ 15:45:22]# chown nginx:nginx -R /usr/share/nginx/html# 安装站点需要的各种包
[root@server ~ 15:45:44]# yum install -y php-gd php-common php-pear php-mbstring php-mcrypt php-mysqlnd# 修改 php-fpm运行用户身份
[root@server ~ 15:45:56]# vim /etc/php-fpm.d/www.conf
# 更改以下两条记录
# user=apacha
user=nginx# group=apacha
group=nginx[root@server ~ 15:46:41]# chown nginx:nginx -R /var/lib/php/# 重启nginx和php
[root@server ~ 15:47:10]# systemctl restart nginx.service php-fpm.service
客户端登录 http://www.ghl.cloud
反向代理
客户端访问代理服务器,代理服务器会将客户端请求发送给真实服务器。
反向代理实现了隐藏内部服务器。
角色说明
-
代理服务器 proxy 10.1.8.20
-
真实服务器 server 10.1.8.10
代理服务器配置
# 新建一个虚拟机,配置IP地址为10.1.8.20
[root@proxy ~ 15:55:02]# ip -br a
lo UNKNOWN 127.0.0.1/8 ::1/128
ens33 UP 10.1.8.20/24 fe80::20c:29ff:fe1c:ad0e/64 # 安装 nginx
[root@proxy ~ 15:55:08]# yum install -y nginx# 启动 nginx
[root@proxy ~ 15:56:22]# systemctl enable --now nginx[root@proxy ~ 16:00:24]# vim /etc/hosts
......
10.1.8.10 www.ghl.cloud# 配置代理,server部分更改如下
[root@proxy ~ 16:00:42]# vim /etc/nginx/conf.d/proxy.conf
[root@proxy ~ 16:28:31]# systemctl restart nginx
server {listen 80;listen [::]:80;server_name _;root /usr/share/nginx/html;#添加如下内容#proxy_redirect off;#proxy_set_header X-Real-IP $remote_addr;#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#proxy_set_header Host $http_host;#新增 location 规则location /proxy/ {proxy_pass http://www.ghl.cloud/;}
}[root@proxf ~ 16:08:57]# systemctl restart nginx.service