【图文教程】Nginx

1. Nginx介绍

Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
  
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

Nginx 可以在大多数 UnixLinux OS 上编译运行,并有 Windows 移植版。 Nginx 的1.4.0稳定版已经于2013年4月24日发布,一般情况下,对于新建站点,建议使用最新稳定版作为生产版本,已有站点的升级急迫性不高。Nginx 的源代码使用 2-clause BSD-like license。

2. Nginx安装(二进制免编译安装)

Nginx包:wget http://nginx.org/download/nginx-1.17.2.tar.gz

讲义

1. cd /usr/local/src
2. wget http://nginx.org/download/nginx-1.12.1.tar.gz
3. tar zxf nginx-1.12.1.tar.gz
4. ./configure --prefix=/usr/local/nginx
5. make &&  make install
6. vim /etc/init.d/nginx //复制如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx )
7. chmod 755 /etc/init.d/nginx
8. chkconfig --add nginx
9. chkconfig nginx on
10. cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak
11. vim nginx.conf //写入如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf)
12. /usr/local/nginx/sbin/nginx -t
13. /etc/init.d/nginx  start
14. netstat -lntp |grep 80
  • 下载源码包,下载命令
[root@sc src]# wget http://nginx.org/download/nginx-1.17.2.tar.gz
  • 下载好后解压
[root@test01 src]# tar zxvf nginx-1.17.2.tar.gz
  • 进入解压后的目录
[root@test01 src]# cd nginx-1.17.2/
  • 查看 nginx 安装文档
[root@sc-01 src]# cd nginx-1.18.0/
[root@sc-01 nginx-1.18.0]# vim README 


Documentation is available at http://nginx.org                   #去访问这个网址

右侧-documentation-Installing nginx-Building nginx from Sources  #里面有编译参数的介绍

Example of parameters usage (all of this needs to be typed in one line):

./configure                                                      #安装示例 
    --sbin-path=/usr/local/nginx/nginx                           
    --conf-path=/usr/local/nginx/nginx.conf
    --pid-path=/usr/local/nginx/nginx.pid
    --with-http_ssl_module
    --with-pcre=../pcre-8.44
    --with-zlib=../zlib-1.2.11
  • ./configure --prefix=/usr/local/nginx 检查编译环境,参数方面根据自己的需求去配置
[root@sc-01 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@test01 nginx-1.17.2]# echo $?
0
  • 执行成功后编译安装
[root@sc nginx-1.16.0]# make && make install
[root@sc nginx-1.16.0]# echo $?
0
  • 查看 nginx
[root@test01 nginx-1.17.2]# cd /usr/local/nginx/
[root@test01 nginx]# ls
conf  html  logs  sbin

nginx目录下有4个目录分别是:

conf:配置文件目录
html:索引页样例目录
logs:日志目录
sbin:核心文件目录
  • 安装成功后,编辑 nginx 启动脚本
[root@sc nginx]# vim /etc/init.d/nginx

编辑内容:
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() 
{
   
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}

stop() 
{
   
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}

reload()
{
   
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

restart()
{
   
    stop
    start
}

configtest()
{
   
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

exit $RETVAL
  • 编辑完成后,给这个启动脚本文件设置 755 权限
[root@sc nginx]# chmod 755 /etc/init.d/nginx
  • 把 nginx 服务添加到服务列表,并设置开机启动
[root@sc nginx]# chkconfig --add nginx
[root@sc nginx]# chkconfig nginx on
[root@sc nginx]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
nginx          	0:off	1:off	2:off	3:off	4:off	5:off	6:off
php-fpm        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@sc nginx]# 
  1. 如果添加到服务列表出现这个错误:
    service nginx does not support chkconfig
    那么就检查一下你的启动脚本文件里的这两行有没有问题:
    LNMP架构介绍与搭建一般都是因为这两行没有写,或者写错了

在这里插入图片描述

  • 进入 nginx 的 conf 目录,然后重命名一下配置文件
[root@sc nginx]# cd /usr/local/nginx/conf
[root@sc conf]# mv nginx.conf nginx.conf.1  
  • 因为不使用 nginx 自带的配置文件,所以需要编辑一个配置文件
[root@sc conf]# vim nginx.conf

编辑内容:
user nobody nobody;                       
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
   
    use epoll;
    worker_connections 6000;
}

http
{
   
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;

    server
    {
   
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;

        location ~ \.php$ 
        {
   
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}
  • 检查配置文件有没有错误
[root@sc conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  • 显示没有 nginx.pid
问题:

[root@master conf]# /usr/local/nginx/sbin/nginx -s reload
nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"


解决方法:

[root@master logs]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@master logs]# ls
access.log  error.log  nginx_error.log  nginx.pid
  • 没有问题就可以启动 nginx 了
[root@sc conf]# /etc/init.d/nginx start
  • 查看一下进程

在这里插入图片描述

  • 检查一下有没有在监听 80 端口

在这里插入图片描述

  • 确认启动成功后,使用 curl 测试一下是否能访问 nginx
[root@sc conf]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
   
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@sc conf]#
  • 在 nginx 的 html 目录下创建一个 php 文件
[root@sc conf]# vim /usr/local/nginx/html/1.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值