RHEL 8 搭建 Nginx Web 服务

简介: RHEL 8 搭建 Nginx Web 服务前请把 yum 源配好。环境

RHEL 8 搭建 Nginx Web 服务前请把 yum 源配好。
环境

Red Hat Enterprise Linux release 8.0
VMware Workstation Pro 14

搭建步骤
[root@localhost ~]# systemctl stop httpd #把 httpd 停掉,防止它影响 Nginx
[root@localhost ~]# yum install -y nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# iptables -F
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# ifconfig
ens33: flags=4163 mtu 1500

    inet 192.168.10.118  netmask 255.255.255.0  broadcast 192.168.10.255
    inet6 fe80::e09a:769b:83f0:8efa  prefixlen 64  scopeid 0x20
    ether 00:50:56:34:0d:74  txqueuelen 1000  (Ethernet)
    RX packets 2908  bytes 1777392 (1.6 MiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 1800  bytes 244006 (238.2 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73 mtu 65536

    inet 127.0.0.1  netmask 255.0.0.0
    inet6 ::1  prefixlen 128  scopeid 0x10
    loop  txqueuelen 1000  (Local Loopback)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099 mtu 1500

    inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
    ether 52:54:00:9c:ef:c6  txqueuelen 1000  (Ethernet)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

在浏览器输入 192.168.10.118 查看 Nginx Web 服务器的状态

RHEL 8 搭建 Nginx Web 服务RHEL 8 搭建 Nginx Web 服务

查看 nginx 软件包的文件列表
[root@localhost ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
...省略部分内容...
自定义首页内容
RHEL 8 搭建 Nginx Web 服务RHEL 8 搭建 Nginx Web 服务

[root@localhost ~]# echo "HLLO RHEL8" > /usr/share/nginx/html/index.html
[root@localhost ~]# systemctl restart nginx
在浏览器输入 192.168.10.118 查看

设置文件共享服务
[root@localhost ~]# mv /usr/share/nginx/html/* /var/lib/nginx/tmp/
[root@localhost ~]# touch /usr/share/nginx/html/file{1..10}
[root@localhost ~]# ls /usr/share/nginx/html/
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
[root@localhost ~]# systemctl restart nginx
RHEL 8 搭建 Nginx Web 服务RHEL 8 搭建 Nginx Web 服务

遇到 403 Forbidden 报错,原因是配置文件没配好,解决方法如下:

[root@localhost html]# grep -v "#" /etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {

worker_connections 1024;

}

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;

sendfile            on;
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;

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  localhost;
    root         /usr/share/nginx/html;

    include /etc/nginx/default.d/*.conf;
    location / {
         index  index.html index.htm;
         autoindex on;
         autoindex_exact_size on;
         autoindex_localtime on;
         charset utf-8;
         } 
     }

}
参考以上配置进行修改

[root@localhost ~]# vim /etc/nginx/nginx.conf
[root@localhost ~]# systemctl restart nginx
在浏览器输入 192.168.10.118 查看文件共享状态

RHEL 8 搭建 Nginx Web 服务RHEL 8 搭建 Nginx Web 服务

设置端口映射
RHEL 8 搭建 Nginx Web 服务RHEL 8 搭建 Nginx Web 服务
查看宿主机IP

RHEL 8 搭建 Nginx Web 服务RHEL 8 搭建 Nginx Web 服务

在浏览器输入 192.168.0.7:118 测试文件共享服务状态

RHEL 8 搭建 Nginx Web 服务RHEL 8 搭建 Nginx Web 服务

在 RHEL8 上用 yum 安装的 Nginx Web 服务对中文的支持比较好
RHEL 8 搭建 Nginx Web 服务RHEL 8 搭建 Nginx Web 服务

[root@localhost ~]# touch /usr/share/nginx/html/你好红帽8.txt
[root@localhost ~]# systemctl restart nginx

相关文章
|
20天前
|
安全 前端开发 API
【Azure 应用服务】Azure Web App 服务默认支持一些 Weak TLS Ciphers Suite,是否有办法自定义修改呢?
【Azure 应用服务】Azure Web App 服务默认支持一些 Weak TLS Ciphers Suite,是否有办法自定义修改呢?
|
9天前
|
负载均衡 网络协议 应用服务中间件
web群集--rocky9.2源码部署nginx1.24的详细过程
Nginx 是一款由 Igor Sysoev 开发的开源高性能 HTTP 服务器和反向代理服务器,自 2004 年发布以来,以其高效、稳定和灵活的特点迅速成为许多网站和应用的首选。本文详细介绍了 Nginx 的核心概念、工作原理及常见使用场景,涵盖高并发处理、反向代理、负载均衡、低内存占用等特点,并提供了安装配置教程,适合开发者参考学习。
|
16天前
|
JSON 定位技术 数据格式
web GIS神器,一行命令快捷构建精灵图服务
web GIS神器,一行命令快捷构建精灵图服务
|
19天前
|
负载均衡 应用服务中间件 网络安全
Django后端架构开发:Nginx服务优化实践
Django后端架构开发:Nginx服务优化实践
33 2
|
14天前
|
Java 缓存 数据库连接
揭秘!Struts 2性能翻倍的秘诀:不可思议的优化技巧大公开
【8月更文挑战第31天】《Struts 2性能优化技巧》介绍了提升Struts 2 Web应用响应速度的关键策略,包括减少配置开销、优化Action处理、合理使用拦截器、精简标签库使用、改进数据访问方式、利用缓存机制以及浏览器与网络层面的优化。通过实施这些技巧,如懒加载配置、异步请求处理、高效数据库连接管理和启用GZIP压缩等,可显著提高应用性能,为用户提供更快的体验。性能优化需根据实际场景持续调整。
40 0
|
14天前
|
Java 网络架构 数据格式
Struts 2 携手 RESTful:颠覆传统,重塑Web服务新纪元的史诗级组合!
【8月更文挑战第31天】《Struts 2 与 RESTful 设计:构建现代 Web 服务》介绍如何结合 Struts 2 框架与 RESTful 设计理念,构建高效、可扩展的 Web 服务。Struts 2 的 REST 插件提供简洁的 API 和约定,使开发者能快速创建符合 REST 规范的服务接口。通过在 `struts.xml` 中配置 `<rest>` 命名空间并使用注解如 `@Action`、`@GET` 等,可轻松定义服务路径及 HTTP 方法。
30 0
|
14天前
|
前端开发 API 开发者
JSF与RESTful服务的完美邂逅:如何打造符合现代Web潮流的数据交互新体验
【8月更文挑战第31天】随着互联网技术的发展,RESTful架构风格因其实现简便与无状态特性而在Web服务构建中日益流行。本文探讨如何结合JavaServer Faces (JSF) 和 JAX-RS 构建RESTful API,展示从前端到后端分离的完整解决方案。通过定义资源类、配置 `web.xml` 文件以及使用依赖注入等步骤,演示了在JSF项目中实现RESTful服务的具体过程,为Java开发者提供了实用指南。
25 0
|
14天前
|
JSON API 数据库
探索FastAPI:不仅仅是一个Python Web框架,更是助力开发者高效构建现代化RESTful API服务的神器——从环境搭建到CRUD应用实战全面解析
【8月更文挑战第31天】FastAPI 是一个基于 Python 3.6+ 类型提示标准的现代 Web 框架,以其高性能、易用性和现代化设计而备受青睐。本文通过示例介绍了 FastAPI 的优势及其在构建高效 Web 应用中的强大功能。首先,通过安装 FastAPI 和 Uvicorn 并创建简单的“Hello, World!”应用入门;接着展示了如何处理路径参数和查询参数,并利用类型提示进行数据验证和转换。
31 0
|
16天前
|
安全 JavaScript 前端开发
Nginx服务扫描漏洞修复
Nginx服务扫描漏洞修复
|
16天前
|
数据可视化 Python
通过python建立一个web服务查看服务器上的文本、图片、视频等文件
通过python建立一个web服务查看服务器上的文本、图片、视频等文件
16 0