Linux中nginx安装基础教程

6 篇文章 0 订阅
3 篇文章 0 订阅

Linux中nginx安装基础教程

1.安装所需的运行环境

(1) 需要安装gcc的环境。

yum install gcc-c++

(2)第三方包。

  Perl

    PCRE(Perl Compatible Regular Expressions), perl 兼容的正则表达式库。nginxhttp模块使用pcre来解析正则表达式,所以需要 在linux上安装pcre库。

yum install -y pcre pcre-devel

   zlib

     zlib库提供了很多种压缩和解压缩的方式,nginx使用zlibhttp包的内容进行gzip,所以需要在linux上安装zlib库。

 yum install -y zlib zlib-devel

  OpenSSL 

      OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试  或其它目的使用。

nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

yum install -y openssl openssl-devel

2.安装部署NGINX

(1)下载NGINX

NGINX下载地址:http://nginx.org/en/download.html

  •     Nginx官网提供了三个类型的版本
  •     Mainline versionMainline Nginx 目前主力在做的版本,可以说是开发版
  •     Stable version:最新稳定版,生产环境上建议使用的版本
  •     Legacy versions:遗留的老版本的稳定版
wget http://nginx.org/download/nginx-1.4.2.tar.gz

(2)解压NGINX

tar -zxvf nginx-1.4.2.tar.gz

(3)进入安装包目录

cd nginx-1.4.2
(4)编译安装 

./configure
 make && make install

   安装成功后可以在/usr/local/看到nginx目录

   

(5)查看pcre版本

nginx -v

(6)启动NGINX

./nginx

 (7)检查配置文件nginx.conf的正确性命令

./nginx -t

 (8)启NGINX

./nginx -s reload

 (9)停止NGINX

./nginx -s stop

 (10)配置基础nginx.conf模版

#user  nobody;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/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/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;
  }

}

 (11)启动完成后,在外网访问不到,必须关掉防火墙或设置防火墙端口白名单,否则外网不能访问

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT  
/etc/init.d/iptables save  
/etc/init.d/iptables restart

 (12)访问NGINX欢迎界面,确定顺利启动



总结不好多多担待,文章只单纯个人总结,如不好勿喷,技术有限,有错漏麻烦指正提出。本人QQ:373965070


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我作为AI语言模型无法进行实验,但是我可以提供一些关于LinuxNginx的基础知识。 Linux是一种开源的操作系统,它的核心是由Linus Torvalds在1991年开发出来的。它的优点在于稳定性、安全性、可定制性和开放性。Linux是免费的,因此可以节省企业和个人的成本。 Nginx是一个高性能的HTTP和反向代理服务器,它最初在2004年由Igor Sysoev开发。它的优点在于高并发能力、低内存消耗、动态模块化和灵活的配置选项。Nginx也可以作为负载均衡器和Web服务器使用。 要在Linux安装Nginx,可以使用以下命令: 1. 更新系统软件包列表:sudo apt-get update 2. 安装Nginx:sudo apt-get install nginx 3. 启动Nginx服务:sudo systemctl start nginx 要配置Nginx,可以编辑/etc/nginx/nginx.conf文件。该文件包含了Nginx的全局配置选项和服务器块配置。每个服务器块定义一个虚拟主机,它可以有自己的域名、SSL证书和代理规则。 例如,以下是一个Nginx服务器块的示例: server { listen 80; server_name example.com; root /var/www/example.com; index index.html; location / { try_files $uri $uri/ /index.html; } location /api/ { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } 该服务器块监听80端口,使用example.com域名作为主机名,将根目录设置为/var/www/example.com,并使用index.html作为默认文档。它还定义了两个位置块:/和/api/。/位置使用try_files指令来查找静态文件或调用/index.html,而/api/位置代理到本地的3000端口,并设置一些代理头部。 如果您需要在Linux上学习更多关于Nginx的实验,请查看相关的在线教程和文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值