安装 配置 Nginx

1.安装Nginx

首先到这个网站可以下载源码包:

http://www.nginx.cn/

我是下在root目录下的,输入:

[root@localhost software]# tar -zxvf nginx-0.6.33

然后进入目录输入:

[root@localhost software]# cd nginx-0.6.33

接着开始编译源代码:

[root@localhost nginx-0.6.33]#./configure\
--prefix=/usr/local/wwwtools/nginx/\
--with-http_stub_status_module\
--with-pcre


具体编译具体参数见文章尾

[root@localhost nginx-0.6.33]#make

[root@localhost nginx-0.6.33]#make install

[root@localhost nginx-0.6.33]#cd /usr/local/nginx/

[root@localhost nginx-0.6.33]./nginx &。

打开浏览器在地址栏输入127.0.0.1然后会出现一句话 welcome to nginx

就是成功了。


配置 优化

server {
        listen       80;
        server_name  192.168.1.131 localhost;	

	set $weburl /home/wwwroot/;#设置变量 保存网站根目录		
        location / {
			
            root   $weburl;
            index  index.html index.htm index.php;
        }
		
		# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
			include        fastcgi_params;
            root           $weburl;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $weburl$fastcgi_script_name;#注意 这里一定要加上网站根目录  否则无法正确解析PHP文件   			
        }

        #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;
        }
    }

nginx 日常管理

Nginx 安装后只有一个程序文件,本身并不提供各种管理程序,它是使用参数和系统信号机制对 Nginx 进程本身进行控制的。 Nginx 的参数包括有如下几个:
-c <path_to_config>:使用指定的配置文件而不是 conf 目录下的 nginx.conf 。
-t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。
-v:显示 nginx 版本号。
-V:显示 nginx 的版本号以及编译环境信息以及编译时的参数。
例如我们要测试某个配置文件是否书写正确,我们可以使用以下命令
sbin/nginx – t – c conf/nginx2.conf

新版本的Nginx 新加了一个参数 -s 具体详情 执行./nginx -h 查看

Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/wwwtools/nginx//)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

该参数可以实现 重新加载 开 关 等操作


2通过信号对 Nginx 进行控制
Nginx 支持下表中的信号:
信号名  作用描述  
TERM, INT  快速关闭程序,中止当前正在处理的请求  
QUIT  处理完当前请求后,关闭程序  
HUP  重新加载配置,并开启新的工作进程,关闭就的进程,此操作不会中断请求  
USR1  重新打开日志文件,用于切换日志,例如每天生成一个新的日志文件  
USR2  平滑升级可执行程序  
WINCH  从容关闭工作进程  

有两种方式来通过这些信号去控制 Nginx,第一是通过 logs 目录下的 nginx.pid 查看当前运行的 Nginx 的进程 ID,通过 kill – XXX <pid> 来控制 Nginx,其中 XXX 就是上表中列出的信号名。如果您的系统中只有一个 Nginx 进程,那您也可以通过 killall 命令来完成,例如运行 killall – s HUP nginx 来让 Nginx 重新加载配置。

NGINX conf 配置文件中的变量大全 可用变量列表及说明


$args #这个变量等于请求行中的参数。
$content_length #请求头中的Content-length字段。
$content_type #请求头中的Content-Type字段。
$document_root #当前请求在root指令中指定的值。
$host #请求主机头字段,否则为服务器名称。
$http_user_agent #客户端agent信息
$http_cookie #客户端cookie信息
$limit_rate #这个变量可以限制连接速率。
$request_body_file #客户端请求主体信息的临时文件名。
$request_method #客户端请求的动作,通常为GET或POST。
$remote_addr #客户端的IP地址。
$remote_port #客户端的端口。
$remote_user #已经经过Auth Basic Module验证的用户名。
$request_filename #当前请求的文件路径,由root或alias指令与URI请求生成。
$query_string #与$args相同。
$scheme #HTTP方法(如http,https)。
$server_protocol #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr #服务器地址,在完成一次系统调用后可以确定这个值。
$server_name #服务器名称。
$server_port #请求到达服务器的端口号。
$request_uri #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
$uri #不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
$document_uri #与$uri相同。 


以下是./configure的选项

--prefix=<path> - Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。

--sbin-path=<path> - Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为<prefix>/sbin/nginx。

--conf-path=<path> - 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。

--pid-path=<path> - 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 <prefix>/logs/nginx.pid。

--lock-path=<path> - nginx.lock文件的路径。

--error-log-path=<path> - 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。

--http-log-path=<path> - 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log。

--user=<user> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。

--group=<group> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。

--builddir=DIR - 指定编译的目录

--with-rtsig_module - 启用 rtsig 模块

--with-select_module --without-select_module - Whether or not to enable the select module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.

//允许或不允许开启SELECT模式,如果 configure 没有找到更合适的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(实时信号)或者/dev/poll(一种类似select的模式,底层实现与SELECT基本相 同,都是采用轮训方法) SELECT模式将是默认安装模式

--with-poll_module --without-poll_module - Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.

--with-http_ssl_module - Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev.

//开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl

--with-http_realip_module - 启用 ngx_http_realip_module

--with-http_addition_module - 启用 ngx_http_addition_module

--with-http_sub_module - 启用 ngx_http_sub_module

--with-http_dav_module - 启用 ngx_http_dav_module

--with-http_flv_module - 启用 ngx_http_flv_module

--with-http_stub_status_module - 启用 "server status" 页

--without-http_charset_module - 禁用 ngx_http_charset_module

--without-http_gzip_module - 禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。

--without-http_ssi_module - 禁用 ngx_http_ssi_module

--without-http_userid_module - 禁用 ngx_http_userid_module

--without-http_access_module - 禁用 ngx_http_access_module

--without-http_auth_basic_module - 禁用 ngx_http_auth_basic_module

--without-http_autoindex_module - 禁用 ngx_http_autoindex_module

--without-http_geo_module - 禁用 ngx_http_geo_module

--without-http_map_module - 禁用 ngx_http_map_module

--without-http_referer_module - 禁用 ngx_http_referer_module

--without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。

--without-http_proxy_module - 禁用 ngx_http_proxy_module

--without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module

--without-http_memcached_module - 禁用 ngx_http_memcached_module

--without-http_limit_zone_module - 禁用 ngx_http_limit_zone_module

--without-http_empty_gif_module - 禁用 ngx_http_empty_gif_module

--without-http_browser_module - 禁用 ngx_http_browser_module

--without-http_upstream_ip_hash_module - 禁用 ngx_http_upstream_ip_hash_module

--with-http_perl_module - 启用 ngx_http_perl_module

--with-perl_modules_path=PATH - 指定 perl 模块的路径

--with-perl=PATH - 指定 perl 执行文件的路径

--http-log-path=PATH - Set path to the http access log

--http-client-body-temp-path=PATH - Set path to the http client request body temporary files

--http-proxy-temp-path=PATH - Set path to the http proxy temporary files

--http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files

--without-http - 禁用 HTTP server

--with-mail - 启用 IMAP4/POP3/SMTP 代理模块

--with-mail_ssl_module - 启用 ngx_mail_ssl_module

--with-cc=PATH - 指定 C 编译器的路径

--with-cpp=PATH - 指定 C 预处理器的路径

--with-cc-opt=OPTIONS  - Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-cc-opt="-I /usr/local/include". If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: --with-cc-opt="-D FD_SETSIZE=2048".

--with-ld-opt=OPTIONS - Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib".

--with-cpu-opt=CPU - 为特定的 CPU 编译,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64

--without-pcre - 禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 "location" 配置指令中的正则表达式也需要 PCRE 。

--with-pcre=DIR - 指定 PCRE 库的源代码的路径。

--with-pcre-opt=OPTIONS - Set additional options for PCRE building.

--with-md5=DIR - Set path to md5 library sources.

--with-md5-opt=OPTIONS - Set additional options for md5 building.

--with-md5-asm - Use md5 assembler sources.

--with-sha1=DIR - Set path to sha1 library sources.

--with-sha1-opt=OPTIONS - Set additional options for sha1 building.

--with-sha1-asm - Use sha1 assembler sources.

--with-zlib=DIR - Set path to zlib library sources.

--with-zlib-opt=OPTIONS - Set additional options for zlib building.

--with-zlib-asm=CPU - Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro

--with-openssl=DIR - Set path to OpenSSL library sources

--with-openssl-opt=OPTIONS - Set additional options for OpenSSL building

--with-debug - 启用调试日志

--add-module=PATH - Add in a third-party module found in directory PATH


### 回答1: 1. 安装Nginx 首先,我们需要安装Nginx。可以使用以下命令在Linux上安装Nginx: sudo apt-get update sudo apt-get install nginx 2. 配置Nginx 安装完成后,我们需要配置NginxNginx配置文件位于/etc/nginx/nginx.conf。可以使用以下命令打开该文件: sudo nano /etc/nginx/nginx.conf 在该文件中,可以配置Nginx的各种设置,例如端口号、虚拟主机、缓存等。在修改完配置文件后,需要重新启动Nginx以使更改生效: sudo service nginx restart 3. 配置虚拟主机 如果需要在同一台服务器上运行多个网站,可以使用虚拟主机来实现。可以在/etc/nginx/sites-available/目录下创建一个新的虚拟主机配置文件,例如: sudo nano /etc/nginx/sites-available/example.com 在该文件中,可以配置虚拟主机的各种设置,例如域名、SSL证书、反向代理等。然后,需要将该文件链接到/etc/nginx/sites-enabled/目录下,以使Nginx加载该虚拟主机配置文件: sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ 最后,需要重新启动Nginx以使更改生效: sudo service nginx restart 4. 配置SSL证书 如果需要在网站上使用SSL证书,可以使用Let's Encrypt等工具来生成和安装SSL证书。可以使用以下命令安装Let's Encrypt: sudo apt-get update sudo apt-get install certbot python-certbot-nginx 然后,可以使用以下命令为虚拟主机配置SSL证书: sudo certbot --nginx -d example.com 该命令将自动为example.com域名生成和安装SSL证书,并将Nginx配置文件更新为使用SSL证书。 5. 配置缓存 如果需要提高网站的性能,可以使用缓存来减少服务器的负载。可以在Nginx配置文件中配置缓存。例如,可以使用以下配置来启用缓存: proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m; proxy_cache_key "$scheme$request_method$host$request_uri"; proxy_cache_valid 200 60m; proxy_cache_valid 404 1m; 该配置将启用一个名为my_cache的缓存区,缓存有效期为60分钟。如果响应代码为200,则缓存有效期为60分钟;如果响应代码为404,则缓存有效期为1分钟。 以上是Linux安装配置Nginx的基本步骤和常用配置。根据实际需求,还可以进行更多的配置和优化。 ### 回答2: Nginx是一款高性能的Web服务器软件,在Linux系统中也很常用。下面是一份Linux安装配置Nginx的步骤: 1. 安装Nginx 在Linux服务器上通过包管理器安装Nginx: sudo apt-get update sudo apt-get install nginx 2. 配置Nginx Nginx配置文件是/etc/nginx/nginx.conf,最基本的配置只需要修改下面几个选项: user nginx; worker_processes 1; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; 其中user是Nginx进程运行的用户,需要根据系统情况修改。另外,还需要配置Nginx监听的端口和文档根目录: server { listen 80; server_name example.com; root /var/www/html; location / { index index.html; } } 3. 启动Nginx 修改完配置文件后,可以通过以下命令启动Nginx: sudo systemctl start nginx 如果想要设置开机自启动Nginx,可以运行以下命令: sudo systemctl enable nginx 4. 配置防火墙 如果Linux服务器有防火墙,需要允许Nginx监听的端口通过防火墙。如果使用的是iptables防火墙,可以运行以下命令打开80端口: sudo iptables -I INPUT 5 -p tcp --dport 80 -j ACCEPT 5. 验证Nginx是否正常工作 通过浏览器访问服务器的公网IP或者域名,如果能够看到Nginx的欢迎页面,则说明Nginx已经正常工作。 以上是Linux安装配置Nginx的基本步骤,如果需要更复杂的配置,可以参考Nginx官方文档或者一些相关教程。 ### 回答3: nginx是一款高效、轻量级、灵活的HTTP服务器,是目前互联网领域使用较多的服务器软件。在linux系统中安装配置nginx比较简单,下面我来详细介绍一下。 一、安装nginx 在终端输入如下命令: sudo apt-get install nginx 这里以Ubuntu系统为例,如果你使用的是其他的Linux系统,可以根据系统版本和发行版本选择对应的nginx安装方式。 二、启动nginx 安装完成后,输入以下命令启动nginx: sudo service nginx start 如果没有错误提示,表示nginx已经开始运行成功了。 三、配置nginx安装完成后,需要对nginx进行配置,使其按照我们的需求工作。 1、修改默认配置文件 nginx的默认配置文件可以在/etc/nginx/sites-enabled/default中进行修改。 首先打开该文件: sudo nano /etc/nginx/sites-enabled/default 在该文件中,可以对服务器的根目录、端口等进行修改,例如修改服务器端口为8080: server { listen 8080 default_server; } 修改完成后,保存修改并退出。 2、开启gzip压缩 在nginx配置文件中,开启gzip压缩可以有效增加传输速度并减少流量消耗。配置如下: gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary on; 3、设置缓存 缓存可以有效提高网站的访问速度,减轻服务器的负担。在nginx中设置缓存可以在配置文件中添加以下内容: proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m; #缓存设置 proxy_cache_key "$scheme$request_method$host$request_uri"; proxy_cache_valid 200 60m; proxy_cache_valid 404 1m; proxy_cache_valid any 1m; 4、其他配置nginx配置文件中,还可以进行很多其他设置,例如开启SSL、设置反向代理等等,具体可以根据需要进行修改。 四、重启nginx 在进行配置修改后,需要重启nginx,使其生效。 输入以下命令重启nginx: sudo service nginx restart 五、总结 以上就是对Linux系统安装配置nginx的详细介绍,如果你需要使用nginx来搭建网站,可以根据上述内容进行配置和修改。总体来说,nginx是一款非常优秀的服务器软件,在性能、安全性和稳定性方面都有着优秀的表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值