中间件Linux学习笔记

小白学nginx之安装与启动

2019-12-06  本文已影响0人  乔治大叔

安装Nginx

1. yum方式(官方)
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

[root@web01 ~]# yum install nginx -y

#查看版本
[root@web01 ~]# nginx -v
nginx version: nginx/1.14.2
2. 源码编译安装
[root@web01 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@web01 ~]# tar xf nginx-1.14.2.tar.gz
[root@web01 ~]# cd nginx-1.14.2/
[root@web01 nginx-1.14.2]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
[root@web01 nginx-1.14.2]# make && make install

启动Nginx

先关闭之前已启动的httpd服务
[root@web01 ~]# systemctl stop httpd
[root@web01 ~]# systemctl disable httpd

然后启动Nginx服务
[root@web01 ~]# systemctl enable nginx
[root@web01 ~]# systemctl start nginx

启动方式有两种:
nginx  启动
nginx -s stop 停止
nginx -s reload |restart 重载服务
systemctl start nginx
systemctl restart nginx
systemctl stop nginx 

nginx 配置文件

查看配置文件命令

cat /etc/nginx/nginx.conf

[root@web01 ~]# cat /etc/nginx/nginx.conf
---------------核心模块
user  nginx;                                    #nginx进程运行的用户
worker_processes  1;                            #nginx工作的进程数量
error_log  /var/log/nginx/error.log warn;       #nginx的错误日志【警告及其警告以上的都记录】
pid        /var/run/nginx.pid;              #nginx进程运行后的进程id
--------------

---------------事件模块
events {
    worker_connections  1024;               #一个work进程的最大连接数
    use epool;                              #使用epool网络模型
}
--------------


---------------http核心层模块
http {
    include       /etc/nginx/mime.types;            #包含资源类型文件
    default_type  application/octet-stream;         #默认以下载方式传输给浏览器(前提是该资源在mime.types中无法找到)

    日志格式定义
    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;        #访问日志

    sendfile        on;     
    #tcp_nopush     on;
    keepalive_timeout  65;      #长连接超时时间
    #gzip  on;                  #是否开启压缩功能
    
    include /etc/nginx/conf.d/*.conf;   #包含哪个目录下面的*.conf文件
    
    server {  定义一个网站
        listen       80;            #监听端口
        server_name  localhost;     #域名

        #charset koi8-r;            #字符集

        location / {                #位置
            root   /usr/share/nginx/html;   #代码的主文件位置
            index  index.html index.htm;    #服务端默认返回给用户的文件
        }
        location /test {                #位置
            root   /code/test/123/; #代码的主文件位置
            index  index.html index.htm;    #服务端默认返回给用户的文件
        }
    }

总结

http server location扩展了解项
http{}层下允许有多个Server{}层,一个Server{}层下又允许有多个Location
http{} 标签主要用来解决用户的请求与响应。
server{} 标签主要用来响应具体的某一个网站。
location{} 标签主要用于匹配网站具体URL路径。

上一篇下一篇

猜你喜欢

热点阅读