nginx安装与配置

286 阅读1分钟

安装nginx依赖

Nginx的安装需要确定Linux安装相关的几个库,否则配置和编译会出现错误
一次性检查并安装,若已安装则跳过,若有更新,则进行更新:

yum install gcc openssl openssl-devel pcre pcre-devel zlib zlib-devel -y

检查库是否安装成功

yum list installed | grep gcc  
yum list installed | grep openssl  
yum list installed | grep pcre  
yum list installed | grep zlib

安装nginx

在服务器根目录创建nginx文件夹并进入

mkdir nginx
cd nginx

下载nginx tar包

wget http://nginx.org/download/nginx-1.14.2.tar.gz

解压nginx tar包

tar -zxvf nginx-1.14.2.tar.gz

进入解压文件夹

cd nginx-1.14.2

执行config脚本编译

./configure

脚本执行完,make

make

make安装

make install

进入安装目录

cd /usr/local/nginx

此时能看到四个文件

conf //nginx配置文件
html //静态资源文件
logs  //日志文件
sbin  //启动文件

修改nginx配置

nginx配置文件在/usr/local/nginx/conf/nginx.conf


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    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$ {
        #    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;
        #}
    }


    # 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;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

我们要修改

//赋予root权限
user root;
//配置资源根路径
location / {
            root   /root/web;
            index  index.html index.htm;
        }

进入nginx脚本目录,并启动nginx

cd sbin
./nginx

然后我们就可以在公网IP80端口访问我们的静态资源

nginx命令

启动 
./nginx  
重启
./nginx -s reload  
停止 
./nginx -s stop