NGINX-新手小白快速上手Centos搭建Nginx服务器

之前博主运维过windows下的nginx,这次打算再linux下使用,踩了几个坑,记录一下顺利搞定的笔记

前言:

因为是快速上手,所以环境是很简单的centos7+可以访问外网+rpm包安装,并且是用的root权限下操作

环境:

机器:一台虚拟机
虚拟机 系统:Centos7.6 
网络:内网192.168.1.132+外网,可以访问百度这些 
官网下载地址:http://nginx.org/packages/rhel/7/x86_64/RPMS/

需求:

1.使用NGINX创建一个hello world网页
2.使用NGINX创建一个文件服务器,直接web端可以下载文件

最终效果:

访问http://192.168.1.132 可以访问文件服务器
访问http://192.168.1.132:8081 可以访问hello world网页
 

实际操作:

1.基础设置:安装系统、配置网络、关闭防火墙、关闭selinux、配置SSH-略过

2.安装NGINX

网上有几种方法,这里使用rpm安装
官网下载地址:http://nginx.org/packages/rhel/7/x86_64/RPMS/

wget http://nginx.org/packages/rhel/7/x86_64/RPMS/nginx-1.16.0-1.el7.ngx.x86_64.rpm
这里我下载1.16版本来操作
ll -a                                     查看一下
    nginx-1.16.0-1.el7.ngx.x86_64.rpm            成功下载会有这个包
chmod 777 nginx-1.16.0-1.el7.ngx.x86_64.rpm      添加权限
rpm -ivh nginx-1.16.0-1.el7.ngx.x86_64.rpm        安装,注意没外网会需要很多依赖

3.启动nginx先试试

systemctl start nginx        启动NGINX
systemctl enable nginx      开机自启
systemctl stop nginx        停止
nginx -s reload            重新加载配置文件

4.访问测试一下

输入地址:http:192.168.1.132 正常情况会出现nginx的欢迎界面

 

 

 

5.搞定需求

  1:发布一个网页,网页hello world

mkdir /usr/nginxweb1 && vim /usr/nginxweb1/index.html        在/usr创建一个nginxwe1的目录;并比纳基index.html文件,输入hello world
    hello world
cd /usr/nginxweb1 && ll -a                        进入目录,并查看一下,正常会看到刚才创建的index.html文件
编辑配置文件,把8081端口与路径修改
vi /etc/nginx/conf.d/default.conf                    编辑默认配置文件
复制server{}这一大段,并在最下方重新粘贴一个新的server{}配置文件
修改其中的 
listen       8081;
和 root   /usr/nginxweb1;

注意是复制一大段再重新粘贴

保存退出,重新启动nginx并重新加载配置文件

nginx -s reload && systemctl start nginx

输入地址 http://192.168.1.131:8081测试访问

 

 

6.搞定需求2.修改默认端口80,搭建一个文件服务器

创建一个文件存放的目录/usr/nginxweb2,并放入一个文件

mkdir /usr/nginxweb2
cp /root/nginx-1.16.0-1.el7.ngx.x86_64.rpm /usr/nginxweb2
ll -a /usr/nginxweb2

修改配置文件,修改端口80,并添加参数

vi /etc/nginx/conf.d/default.conf    
root指向刚才创建的目录
autoindex on;                          #开启索引功能 
autoindex_exact_size off;            # 关闭计算文件确切大小(单位bytes),只显示大概大小(kb mb gb)
autoindex_localtime on;               # 显示本机时间而非 GMT 时间

保存退出,重新启动nginx并重新加载配置文件

nginx -s reload && systemctl start nginx

输入地址 http://192.168.1.131:80测试访问

 

 

 

 

7.卸载,如果有没成功的,就卸载重新来一次

systemctl stop nginx
yum remove nginx
rpm -e nginx
cd / && find -name *nginx*
rm -rf /etc/nginx

 

8.博主踩坑:nginx相关的配置文件探索与进程查看

1.最开始配置conf文件,配置在/etc/nginx/nginx.conf;老是报错,最后网上一搜索才发现rpm包安装在default.conf修改,cao

2.命令在nginx -s reload的时候可能会出现pid报错

要去nginx.conf修改配置文件,用find -name区查找pid的位置或者给注释掉

3.防火墙一定要放通或者关闭,不然访问下载报错,用netstat -lnpt 查看端口

cd / && find -name nginx                查找nginx被安装在什么目录
    ./etc/sysconfig/nginx                
    ./etc/logrotate.d/nginx
    ./etc/nginx
    ./var/log/nginx
    ./var/cache/nginx
    ./usr/sbin/nginx
    ./usr/lib64/nginx
    ./usr/share/nginx
    ./usr/libexec/initscripts/legacy-actions/nginx
find -name nginx.conf                查看nginx的配置文件在什么目录
    ./etc/nginx/nginx.conf                
结合以上两条文件结果,大概知道NGINX的主要配置文件在/etc/nginx目录
cd /etc/nginx && ll -a                进入目录,并看下所有文件
cat /etc/nginx/nginx.conf                查看一下配置文件

 

总结:其实nginx发布一些网页,简单代理不难,就是加了linux环境不熟悉导致的,多把linux的命令练熟悉,问题不大

posted @ 2021-11-20 16:31  李宗盛  阅读(681)  评论(0编辑  收藏  举报