一、Nginx安装
1、Nginx版本
- Mainline version :开发版,主要是给广大Nginx爱好者,测试、研究和学习的,但是不建议使用于生产环境。
- Stable version : 稳定版,也就是我们说的长期更新版本。这种版本一般比较成熟,经过长时间的更新测试,所以这种版本也是主流版本。
- legacy version : 历史版本,如果你需要以前的版本,Nginx也是有提供的。
2、安装版本
先来查看一下yum是否已经存在
yum list | grep nginx
返回
collectd-nginx.x86_64 5.8.0-4.el7 epel
munin-nginx.noarch 2.0.40-4.el7 epel
nextcloud-nginx.noarch 10.0.4-2.el7 epel
nginx.x86_64 1:1.12.2-2.el7 epel
nginx-all-modules.noarch 1:1.12.2-2.el7 epel
nginx-filesystem.noarch 1:1.12.2-2.el7 epel
nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7 epel
nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7 epel
nginx-mod-http-perl.x86_64 1:1.12.2-2.el7 epel
nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7 epel
nginx-mod-mail.x86_64 1:1.12.2-2.el7 epel
nginx-mod-stream.x86_64 1:1.12.2-2.el7 epel
owncloud-nginx.noarch 9.1.5-1.el7 epel
pcp-pmda-nginx.x86_64 3.12.2-5.el7 base
python2-certbot-nginx.noarch 0.27.1-1.el7 epel
系统原来的源只支持1.1.12版本,这版本有些低。
如果不存在,或者不是你需要的版本,那我们可以自行配置yum源。
创建repo文件
repo文件是yum源(软件仓库)的配置文件,通常一个repo文件定义了一个或者多个软件仓库的细节内容,例如我们将从哪里下载需要安装或者升级的软件包,repo文件中的设置内容将被yum读取和应用。
vim /etc/yum.repos.d/nginx.repo
复制
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
需要修改一下对应的操作系统和版本号,因为我的是centos和7的版本,所以改为
baseurl=http://nginx.org/packages/centos/7/$basearch/
可以根据你的系统或需要的版本进行修改
3、安装Nginx
yum install nginx
检测Nginx的版本
nginx -v
返回
nginx version: nginx/1.14.0
现在Nginx已经顺利安装完成了,之后的任务就是要学习配置和使用它。
二、Nginx基本配置文件
1、查看Nginx的安装目录
安装完Nginx后,需要知道系统中多了那些文件,它们都安装到了那里。可以使用下面的命令进行查看:
rpm -ql nginx
rpm 是linux的rpm包管理工具,-q 代表询问模式,-l 代表返回列表,这样我们就可以找到nginx的所有安装位置了
2、配置文件解读
nginx.conf 文件是Nginx总配置文件,在我们搭建服务器时经常调整的文件。
vi /etc/nginx/nginx.conf
user nginx; #运行用户,默认即是nginx,可以不进行设置
worker_processes 1; #Nginx进程,一般设置为和CPU核数一样
error_log /var/log/nginx/error.log warn; #错误日志存放目录
pid /var/run/nginx.pid; #进程pid存放位置
events {
worker_connections 1024; # 单个后台进程的最大并发数
}
http {
include /etc/nginx/mime.types; #文件扩展名与类型映射表
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 /var/log/nginx/access.log main; #nginx访问日志存放位置
sendfile on; #开启高效传输模式
#tcp_nopush on; #减少网络报文段的数量
keepalive_timeout 65; #保持连接的时间,也叫超时时间
#gzip on; #开启gzip压缩
include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件
default.conf 配置项讲解 我们看到最后有一个子文件的配置项,那我们打开这个include子文件配置项看一下里边都有些什么内容。
vi /etc/nginx/conf.d/default.conf
server {
listen 80; #配置监听端口
server_name localhost; //配置域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; #服务默认启动目录
index index.html index.htm; #默认访问文件
}
#error_page 404 /404.html; # 配置404页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #错误状态码的显示页面,配置后需要重启
location = /50x.html {
root /usr/share/nginx/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$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# 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;
#}
}
我们知道我们的服务目录放在了/usr/share/nginx/html下,可以使用命令进入看一下目录下的文件。可以看到目录下面有两个文件,50x.html 和 index.html。
Vultr默认80端口没有打开,需要在防火墙中添加80端口。
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
在浏览器中用ip访问,可以打开Nginx欢迎页,说明配置成功了。