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解释说明:
-
#user nobody;
-
worker_processes 1; #工作进程:数目。根据硬件调整,通常等于cpu数量或者2倍cpu数量。
-
-
#错误日志存放路径
-
#error_log logs/error.log;
-
#error_log logs/error.log notice;
-
#error_log logs/error.log info;
-
-
#pid logs/nginx.pid; # nginx进程pid存放路径
-
-
-
events {
-
worker_connections 1024; # 工作进程的最大连接数量
-
}
-
-
-
http {
-
include mime.types; #指定mime类型,由mime.type来定义
-
default_type application/octet-stream;
-
-
# 日志格式设置
-
#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 logs/access.log main; #用log_format指令设置日志格式后,需要用access_log来指定日志文件存放路径
-
-
sendfile on; #指定nginx是否调用sendfile函数来输出文件,对于普通应用,必须设置on。
-
如果用来进行下载等应用磁盘io重负载应用,可设着off,以平衡磁盘与网络io处理速度,降低系统uptime。
-
#tcp_nopush on; #此选项允许或禁止使用socket的TCP_CORK的选项,此选项仅在sendfile的时候使用
-
-
#keepalive_timeout 0; #keepalive超时时间
-
keepalive_timeout 65;
-
-
#gzip on; #开启gzip压缩服务
-
-
#虚拟主机
-
server {
-
listen 80; #配置监听端口号
-
server_name localhost; #配置访问域名,域名可以有多个,用空格隔开
-
-
#charset koi8-r; #字符集设置
-
-
#access_log logs/host.access.log main;
-
-
location / {
-
root html;
-
index index.html index.htm;
-
}
-
#错误跳转页
-
#error_page 404 /404.html;
-
-
# redirect server error pages to the static page /50x.html
-
#
-
error_page 500 502 503 504 /50x.html;
-
location = /50x.html {
-
root html;
-
}
-
-
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
-
#
-
#location ~ \.php$ {
-
# proxy_pass http://127.0.0.1;
-
#}
-
-
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
-
#
-
#location ~ \.php$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
-
# root html; #根目录
-
# fastcgi_pass 127.0.0.1:9000; #请求转向定义的服务器列表
-
# fastcgi_index index.php; # 如果请求的Fastcgi_index URI是以 / 结束的, 该指令设置的文件会被附加到URI的后面并保存在变量$fastcig_script_name中
-
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
-
# include fastcgi_params;
-
#}
-
-
# deny access to .htaccess files, if Apache's document root
-
# concurs with nginx's one
-
#
-
#location ~ /\.ht {
-
# deny all;
-
#}
-
}
-
-
-
# another virtual host using mix of IP-, name-, and port-based configuration
-
#
-
#server {
-
# listen 8000;
-
# listen somename:8080;
-
# server_name somename alias another.alias;
-
-
# location / {
-
# root html;
-
# index index.html index.htm;
-
# }
-
#}
-
-
-
# HTTPS server
-
#
-
#server {
-
# listen 443 ssl; #监听端口
-
# server_name localhost; #域名
-
-
# ssl_certificate cert.pem; #证书位置
-
# ssl_certificate_key cert.key; #私钥位置
-
-
# ssl_session_cache shared:SSL:1m;
-
# ssl_session_timeout 5m;
-
-
# ssl_ciphers HIGH:!aNULL:!MD5; #密码加密方式
-
# ssl_prefer_server_ciphers on; # ssl_prefer_server_ciphers on; #
-
-
-
# location / {
-
# root html;
-
# index index.html index.htm;
-
# }
-
#}
-
-
}
4.2 Linux中防火墙、端口等相关操作命令
-
防火墙基本操作
-
-
firewall-cmd --state
-
-
-
systemctl stop firewalld.service
-
-
-
systemctl start firewalld.service
-
-
-
systemctl disable firewalld.service
-
-
-
systemctl enable firewalld.service
-
-
开启443端口
-
开启防火墙
-
systemctl start firewalld
-
-
开放指定端口
-
firewall-cmd --zone=public --add-port=443/tcp --permanent
-
-
命令含义:
-
--zone #作用域
-
--add-port=1935/tcp #添加端口,格式为:端口/通讯协议
-
--permanent #永久生效,没有此参数重启后失效
-
-
重启防火墙
-
firewall-cmd --reload
-
-
firewall防火墙
-
查看firewall服务状态
-
-
systemctl status firewalld
-
-
出现Active: active (running)切高亮显示则表示是启动状态。
-
出现 Active: inactive (dead)灰色表示停止,看单词也行。
-
-
查看firewall的状态
-
-
firewall-cmd --state
-
-
开启、重启、关闭、firewalld.service服务
-
-
-
service firewalld start
-
-
service firewalld restart
-
-
service firewalld stop
-
-
查看防火墙规则
-
-
firewall-cmd --list-all
-
-
查询、开放、关闭端口
-
-
-
firewall-cmd --query-port=8080/tcp
-
lsof -i:8080(如果没有lsof,可以使用 yum install lsof 下载)
-
-
-
-
firewall-cmd --permanent --add-port=80/tcp
-
-
-
-
firewall-cmd --permanent --remove-port=8080/tcp
-
-
-
-
firewall-cmd --reload
-
-
参数解释
-
-
firwall-cmd:是Linux提供的操作firewall的一个工具;
-
-
--permanent:表示设置为持久;
-
-
--add-port:标识添加的端口;
-
-
常用命令
-
-
-
service iptables status
-
-
-
-
service iptables stop
-
-
-
-
service iptables start
-
-
-
-
service iptables restart
-
-
-
-
chkconfig iptables off
-
-
-
-
chkconfig iptables on
注意:本文内容来源于https://blog.csdn.net/weixin_45236540/article/details/130407889,通过参考此文章成功安装特此记录


浙公网安备 33010602011771号