CentOS8安装Nginx

前言:

Nginx(engine x)是一个高性能的HTTP和反向代理web服务器,它同时提供了IMAP/POP3/SMTP服务,Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的。

Nginx是一款轻量级的Web服务器 / 反向代理服务器以及电子邮件(IMAP/POP3)代理服务器。在BSK-like 协议下发行。其特点是占有内存少、并发能力强。

nginx的并发能力在同类型的网页服务器中表现较好。

 

 

由于在CentOS8中安装nginx比较简单,接下来直接进入主题

1、安装必要的插件

1.1、安装gcc

GCC(GNU Compiler Collection,GNU编译器套件)是由GNU开发的编程语言编译器。它可以编译 C、C++、ada、object-c、java、Go等语言。

一般来说 CentOS8中都自带有gcc,可以使用命令:gcc -v 来查看gcc是否安装

 如果gcc没有安装,可使用命令:yum install gcc -y 来进行安装

1.2、安装pcre pcre-devel

pcre是一个 perl 库,它包括了 perl 兼容的正则表达式库,nginx中的http模块需要使用 pcre 来解析正则表达式,所以,安装  pcre 库是必须的。

命令:yum install pcre pcre-devel -y

 

 1.3、安装zlib zlib-devel

zlib库提供了很多种压缩和解压缩的方式,nginx需要使用zlib库来对http包的内容进行gzip,所以zlib插件也必须要安装。

命令:yum install zlib zlib-devel -y

 1.4、安装openssl openssl-devel

openssl是web安全通信的基石,如果没有spenssl,我们的所有信息都相当于是在裸奔,会全部暴露出来,其重要程度可想而知,所以也必须要安装。

命令:yum install openssl openssl-devel -y

 

 2、安装nginx

2.1、安装nginx,首页进入 src目录,然后创建一个nginx文件夹,进入nginx目录后再使用命令安装nginx。当然安装目录可以自行选择。

命令:

cd usr/local/src

mkdir nginx

cd nginx

2.1、使用下列命令下载nginx安装包

命令:wget http://nginx.org/download/nginx-1.18.0.tar.gz

 

 2.2、使用下列命令将nginx安装包进行解压,将解压后的nginx文件重命名,然后删除原装安装包。

tar -zxf nginx-1.18.0.tar.gz 
 

2.3 进入nginx-1.18.0目录,然后执行下列命令

./configure --prefix=/usr/local/src/nginx
 

 --prefix=/usr/local/src/nginx  是指指定路径安装

 

 

执行命令: make  进行编译

 

 执行命令:make install  进行安装

 2.4、安装完成后,进入conf目录,执行命令:vim nginx.conf   查看端口

 

 2.5、启动nginx

切换到下图中的路径

 然后进入sbin目录执行命令:./nginx  启动nginx,然后执行命令:ps -ef | grep nginx  查看nginx是否启动成功。

然后在虚拟机的浏览器中输入地址:http://localhost:80访问nginx。

 

 3、如果想要在外部主机访问nginx,需要关闭服务器防火墙或者开放nginx服务端口,nginx的服务端口为nginx.conf中配置的端口。

命令:systemctl stop firewalld.service     #关闭防火墙

最后在外部主机的浏览器输入nginx地址就可以访问了。

 

4、扩展

4.1、nginx.conf解释说明:

  1.  
    #user nobody;
  2.  
    worker_processes 1; #工作进程:数目。根据硬件调整,通常等于cpu数量或者2倍cpu数量。
  3.  
     
  4.  
    #错误日志存放路径
  5.  
    #error_log logs/error.log;
  6.  
    #error_log logs/error.log notice;
  7.  
    #error_log logs/error.log info;
  8.  
     
  9.  
    #pid logs/nginx.pid; # nginx进程pid存放路径
  10.  
     
  11.  
     
  12.  
    events {
  13.  
    worker_connections 1024; # 工作进程的最大连接数量
  14.  
    }
  15.  
     
  16.  
     
  17.  
    http {
  18.  
    include mime.types; #指定mime类型,由mime.type来定义
  19.  
    default_type application/octet-stream;
  20.  
     
  21.  
    # 日志格式设置
  22.  
    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  23.  
    # '$status $body_bytes_sent "$http_referer" '
  24.  
    # '"$http_user_agent" "$http_x_forwarded_for"';
  25.  
     
  26.  
    #access_log logs/access.log main; #用log_format指令设置日志格式后,需要用access_log来指定日志文件存放路径
  27.  
     
  28.  
    sendfile on; #指定nginx是否调用sendfile函数来输出文件,对于普通应用,必须设置on。
  29.  
    如果用来进行下载等应用磁盘io重负载应用,可设着off,以平衡磁盘与网络io处理速度,降低系统uptime。
  30.  
    #tcp_nopush on; #此选项允许或禁止使用socket的TCP_CORK的选项,此选项仅在sendfile的时候使用
  31.  
     
  32.  
    #keepalive_timeout 0; #keepalive超时时间
  33.  
    keepalive_timeout 65;
  34.  
     
  35.  
    #gzip on; #开启gzip压缩服务
  36.  
     
  37.  
    #虚拟主机
  38.  
    server {
  39.  
    listen 80; #配置监听端口号
  40.  
    server_name localhost; #配置访问域名,域名可以有多个,用空格隔开
  41.  
     
  42.  
    #charset koi8-r; #字符集设置
  43.  
     
  44.  
    #access_log logs/host.access.log main;
  45.  
     
  46.  
    location / {
  47.  
    root html;
  48.  
    index index.html index.htm;
  49.  
    }
  50.  
    #错误跳转页
  51.  
    #error_page 404 /404.html;
  52.  
     
  53.  
    # redirect server error pages to the static page /50x.html
  54.  
    #
  55.  
    error_page 500 502 503 504 /50x.html;
  56.  
    location = /50x.html {
  57.  
    root html;
  58.  
    }
  59.  
     
  60.  
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  61.  
    #
  62.  
    #location ~ \.php$ {
  63.  
    # proxy_pass http://127.0.0.1;
  64.  
    #}
  65.  
     
  66.  
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  67.  
    #
  68.  
    #location ~ \.php$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
  69.  
    # root html; #根目录
  70.  
    # fastcgi_pass 127.0.0.1:9000; #请求转向定义的服务器列表
  71.  
    # fastcgi_index index.php; # 如果请求的Fastcgi_index URI是以 / 结束的, 该指令设置的文件会被附加到URI的后面并保存在变量$fastcig_script_name中
  72.  
    # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  73.  
    # include fastcgi_params;
  74.  
    #}
  75.  
     
  76.  
    # deny access to .htaccess files, if Apache's document root
  77.  
    # concurs with nginx's one
  78.  
    #
  79.  
    #location ~ /\.ht {
  80.  
    # deny all;
  81.  
    #}
  82.  
    }
  83.  
     
  84.  
     
  85.  
    # another virtual host using mix of IP-, name-, and port-based configuration
  86.  
    #
  87.  
    #server {
  88.  
    # listen 8000;
  89.  
    # listen somename:8080;
  90.  
    # server_name somename alias another.alias;
  91.  
     
  92.  
    # location / {
  93.  
    # root html;
  94.  
    # index index.html index.htm;
  95.  
    # }
  96.  
    #}
  97.  
     
  98.  
     
  99.  
    # HTTPS server
  100.  
    #
  101.  
    #server {
  102.  
    # listen 443 ssl; #监听端口
  103.  
    # server_name localhost; #域名
  104.  
     
  105.  
    # ssl_certificate cert.pem; #证书位置
  106.  
    # ssl_certificate_key cert.key; #私钥位置
  107.  
     
  108.  
    # ssl_session_cache shared:SSL:1m;
  109.  
    # ssl_session_timeout 5m;
  110.  
     
  111.  
    # ssl_ciphers HIGH:!aNULL:!MD5; #密码加密方式
  112.  
    # ssl_prefer_server_ciphers on; # ssl_prefer_server_ciphers on; #
  113.  
     
  114.  
     
  115.  
    # location / {
  116.  
    # root html;
  117.  
    # index index.html index.htm;
  118.  
    # }
  119.  
    #}
  120.  
     
  121.  
    }

4.2 Linux中防火墙、端口等相关操作命令

  1.  
    防火墙基本操作
  2.  
    #查看防火墙是否开启(如果显示running,则防火墙为开启状态):
  3.  
    firewall-cmd --state
  4.  
     
  5.  
    #关闭防火墙:
  6.  
    systemctl stop firewalld.service
  7.  
     
  8.  
    #开启防火墙
  9.  
    systemctl start firewalld.service
  10.  
     
  11.  
    #关闭开机自启动:
  12.  
    systemctl disable firewalld.service
  13.  
     
  14.  
    #开启开机自启动:
  15.  
    systemctl enable firewalld.service
  16.  
     
  17.  
    开启443端口
  18.  
    开启防火墙
  19.  
    systemctl start firewalld
  20.  
  21.  
    开放指定端口
  22.  
    firewall-cmd --zone=public --add-port=443/tcp --permanent
  23.  
     
  24.  
    命令含义:
  25.  
    --zone #作用域
  26.  
    --add-port=1935/tcp #添加端口,格式为:端口/通讯协议
  27.  
    --permanent #永久生效,没有此参数重启后失效
  28.  
  29.  
    重启防火墙
  30.  
    firewall-cmd --reload
  31.  
     
  32.  
    firewall防火墙
  33.  
    查看firewall服务状态
  34.  
     
  35.  
    systemctl status firewalld
  36.  
     
  37.  
    出现Active: active (running)切高亮显示则表示是启动状态。
  38.  
    出现 Active: inactive (dead)灰色表示停止,看单词也行。
  39.  
     
  40.  
    查看firewall的状态
  41.  
     
  42.  
    firewall-cmd --state
  43.  
     
  44.  
    开启、重启、关闭、firewalld.service服务
  45.  
     
  46.  
    # 开启
  47.  
    service firewalld start
  48.  
    # 重启
  49.  
    service firewalld restart
  50.  
    # 关闭
  51.  
    service firewalld stop
  52.  
     
  53.  
    查看防火墙规则
  54.  
     
  55.  
    firewall-cmd --list-all
  56.  
     
  57.  
    查询、开放、关闭端口
  58.  
     
  59.  
    # 查询端口是否开放
  60.  
    firewall-cmd --query-port=8080/tcp
  61.  
    lsof -i:8080(如果没有lsof,可以使用 yum install lsof 下载)
  62.  
     
  63.  
     
  64.  
    # 开放80端口
  65.  
    firewall-cmd --permanent --add-port=80/tcp
  66.  
     
  67.  
     
  68.  
    # 移除端口
  69.  
    firewall-cmd --permanent --remove-port=8080/tcp
  70.  
     
  71.  
     
  72.  
    #重启防火墙(修改配置后要重启防火墙)
  73.  
    firewall-cmd --reload
  74.  
     
  75.  
    参数解释
  76.  
     
  77.  
    firwall-cmd:是Linux提供的操作firewall的一个工具;
  78.  
     
  79.  
    --permanent:表示设置为持久;
  80.  
     
  81.  
    --add-port:标识添加的端口;
  82.  
     
  83.  
    常用命令
  84.  
    # 查看防火墙状态
  85.  
     
  86.  
    service iptables status
  87.  
     
  88.  
    # 停止防火墙
  89.  
     
  90.  
    service iptables stop
  91.  
     
  92.  
    # 启动防火墙
  93.  
     
  94.  
    service iptables start
  95.  
     
  96.  
    # 重启防火墙
  97.  
     
  98.  
    service iptables restart
  99.  
     
  100.  
    # 永久关闭防火墙
  101.  
     
  102.  
    chkconfig iptables off
  103.  
     
  104.  
    # 永久关闭后重启
  105.  
     
  106.  
    chkconfig iptables on

 注意:本文内容来源于https://blog.csdn.net/weixin_45236540/article/details/130407889,通过参考此文章成功安装特此记录

 

posted @ 2023-07-13 13:50  Fast & Furious  阅读(907)  评论(0)    收藏  举报