美文网首页
Linux(二)搭建Nginx服务器

Linux(二)搭建Nginx服务器

作者: 枫叶丶落 | 来源:发表于2021-03-10 10:12 被阅读0次

    系统环境:Centos 7.0

    1.下载相关包

    • 安装依赖包
    > yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel  
    
    • 创建存放nginx安装包的文件夹
    > mkdir /usr/local/nginx  
    
    • 进入nginx文件夹
    > cd /usr/local/nginx
    
    • 下载nginx的tar包
    > wget http://nginx.org/download/nginx-1.13.7.tar.gz
    
    • 将解压的tar包解压至当前文件夹
    > tar -xvf nginx-1.13.7.tar.gz
    

    3.安装nginx

    • 进入解压完的目录
    > cd nginx-1.13.7
    
    • 生成makefile文件
    > ./configure
    
    • 编译
    > make
    
    • 安装
    > make install
    

    4.配置nginx.conf

    > vi /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;  # 修改这里 对应的是端口号,默认的端口是80
           server_name  localhost; #  # 修改这里 对应的是ip,默认的是本地的
    
           #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$ {
    "nginx.conf" 117L, 2664C
    

    4.启动nginx服务

    • 启动nginx服务
    > /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    > /usr/local/nginx/sbin/nginx -s reload  # 启动nginx服务
    
    • 关闭防火墙
    > systemctl stop firewalld.service
    
    • 查看nginx进程
    > ps -ef | grep nginx
             root      4460     1  0 19:30 ?       00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -                        
             c /usr/local/nginx/conf/nginx.conf
             nobody    5123  4460  0 19:33 ?        00:00:00 nginx: worker process
             root     11075 11759  0 20:05 pts/0    00:00:00 vi nginx.conf
             root     11529 11759  0 20:08 pts/0    00:00:00 vi //usr/local/nginx/conf
             root     11654 11759  0 20:08 pts/0    00:00:00 vi nginx.conf
             root     13807 11759  0 20:20 pts/0    00:00:00 grep --color=auto nginx
    
    • 服务启动成功后 在网页中输入ip地址和端口 如果成功则出现下方展示页


      image.png
    • nginx 服务配置成功后 一些常规操作

    > cd /usr/local/nginx/sbin  # 进入此文件夹
    > ./nginx  # 启动nginx服务
    > ./nginx -s stop  # 关闭nginx服务
    > ./nginx -s reload  # 重启nginx服务

    相关文章

      网友评论

          本文标题:Linux(二)搭建Nginx服务器

          本文链接:https://www.haomeiwen.com/subject/uldbqltx.html