linux安装nginx环境及常用配置和命令

当我们项目上线部署的时候,肯定少不了在linux环境中安装服务器,而nginx就是现在比较主流功能比较强大的一款服务器。这次教程带大家从基础的linux环境一步一步安装nginx,及nginx的常用配置和常用命令。

一、用yum进行安装linux必要环境

如果你Linux用的很熟练的话,我想这些程序你一定已经用yum安装过了,但是如果你还不熟悉Linux,你可以直接用yum进行安装就可以了。(然后自己百度一下这些东西的用处,这里不做过多的介绍了,只要照做就可以了)

yum -y install gcc gcc-c++ autoconf pcre-devel make automake
yum -y install wget httpd-tools vim

二、Nginx的快速搭建

先查看下yum中是否有nginx

yum list | grep nginx

可以看到yum中有nginx,但并不是我们想要的最新版
我们进入nginx官网,设置一个最新版的yum源
里面有关于centos和Ubuntu的不同设置教程,我这里以centos为列设置

1.yum安装yum-utils

yum install yum-utils -y

2. 新建并编辑yum源中nginx的配置文件

vi /etc/yum.repos.d/nginx.repo

3.设置其中内容为

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

4.默认是稳定版,如果要使用nginx主线版本(如果是生产环境,建议跳过不设置)

yum-config-manager --enable nginx-mainline

5.安装刚刚设置好的nginx

yum install nginx -y

此时我们可以通过

nginx -v

来查看nginx是否已经安装好

三、Nginx的常用配置

1.nginx.conf文件解读

nginx.conf 文件是Nginx总配置文件,在我们搭建服务器时经常调整的文件。

vi /etc/nginx/nginx.conf

下面是文件的详细注释

#运行用户,默认即是nginx,可以不进行设置
user  nginx;
#Nginx进程,一般设置为和CPU核数一样
worker_processes  1;   
#错误日志存放目录
error_log  /var/log/nginx/error.log warn;
#进程pid存放位置
pid        /var/run/nginx.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; #包含的子配置项位置和文件

2.nginx的解析及反向代理配置

nginx的解析及反向代理配置全在在conf.d文件夹里面

cd /etc/nginx/conf.d

里面有一个默认的配置文件,default.conf,用vi 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下,如果我们想修改的话就是修改这里,但是此处有个小坑,直接修改项目目录后会出现403错误,需要将/etc/nginx/nginx.conf中默认user: nginx改为user: root即可

三、Nginx的常用命令

1.启动nginx

nginx

2.立即停止nginx

nginx -s stop

3.从容停止,比stop温和,即完成当前工作后停止

nginx -s quit

4.重启nginx,修改配置后执行

nginx -s reload

恭喜你,看到这,说明你已经学会了linux安装nginx环境及常用配置和命令

你可能感兴趣的:(linux服务器)