Centos7 安装nginx1.16.0

18 篇文章 1 订阅
10 篇文章 0 订阅

一、 环境配置

nginx 使用C语言进行开发,建议在linux环境下运行,本文只介绍linux下的安装

1、gcc 安装

安装nginx需要先将官网上的源码下载下来进行编译,编译依赖gcc环境,如果系统中未装有gcc,则需要进行安装。
执行如下命令安装gcc环境:

yum install gcc-c++

2、pcre pcre-devel安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块需要使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。
命令:

yum install -y pcre pcre-devel

3、zlib安装

zlib提供了很多种压缩和解压的方法,nginx使用zlib对http包的内容记性gzip,所以需要在centos上安装zlib库。
命令:

yum install -y zlib zlib-devel

4、OpenSSL安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
命令:

yum install -y openssl openssl-devel

二、安装包下载

  1. 直接下载tar.gz安装包。地址:http://nginx.org/en/download.html
    在这里插入图片描述
  2. 使wget命令下载(优先推荐)
    wget -c http://nginx.org/download/nginx-1.16.0.tar.gz
    在这里插入图片描述
    我下载的是1.16版本,是当前最新的稳定版。

三、解压

直接使用tar命令进行解压

tar -zxvf nginx-1.16.0.tar.gz
cd nginx-1.16.0

四、配置

在ngxin 1.16.0中你可以不再配置其它东西,直接使用默认的配置即可。当然最为一个优秀的程序员,肯定不会满足于默认的配置。
使用默认配置:

./configure

自定义配置:


./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

五、编译安装

执行命令:

make
make install

查找默认安装路径:

whereis nginx

在这里插入图片描述

六、 启动和停止ngxin

cd /usr/local/nginx/sbin/
./nginx  /*启动ngixn*/
./nginx -s quit  /*此方式停止步骤是待nginx进程处理任务完毕进行停止*/
./nginx -s stop  /*此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程*/

七、重启ngxin

ngxin重启推荐操作步骤是:先停止在重启

./nginx -s reload            # 重新载入配置文件
./nginx -s reopen            # 重启 Nginx

ngxin启动成后我们可以使用浏览器访问当前系统的80端口,显示界面如下在这里插入图片描述

八、ngxin配置

创建 Nginx 运行使用的用户 www:

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

配置nginx.conf ,将/usr/local/webserver/nginx/conf/nginx.conf替换为以下内容

[root@bogon conf]#  cat /usr/local/webserver/nginx/conf/nginx.conf

user www www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
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';
  
#charset gb2312;
     
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on; 
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
 
  #limit_zone crawler $binary_remote_addr 10m;
 #下面是server虚拟主机的配置
 server
  {
    listen 80;#监听端口
    server_name localhost;#域名
    index index.html index.htm index.php;
    root /usr/local/webserver/nginx/html;#站点目录
      location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
    }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
   # access_log off;
    }
    access_log off;
  }

}

检查配置文件nginx.conf的正确性命令:

/usr/local/webserver/nginx/sbin/nginx -t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值