Nginx - nginx 安装及相关命令

Nginx-nginx 介绍

说明

介绍如何编译安装和用apt-get两种方式在ubuntu下安装nginx。


安装前说明

编译安装

编译安装

1.下载最新的稳定版nginx,然后解压。

wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -xzvf nginx-1.16.1.tar.gz

2.更新源,安装相关扩展

  sudo apt-get update
  sudo apt-get install build-essential
  sudo apt-get install libtool
  sudo apt-get install libpcre3 libpcre3-dev
  sudo apt-get install zlib1g-dev
  sudo apt-get install openssl libssl-dev

3.编译

cd nginx-1.16.1/
#编译参数设置
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module --with-stream --with-mail=dynamic
#编译安装
make
make install

4.启动nginx,查看进程

   
   sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
   ps -aux|grep nginx

编译参数

编译选项说明:
--prefix=path 如果在编译的不指定安装位置,那么默认的位置/usr/local/nginx目录
--sbin-path=path 设置nginx执行脚本的位置,这里如果设置在path变量里面,就可以在bash环境下,任意使用nginx命令,默认位置prefix/sbin/nginx  注意这里的prefix是在配置文件里面配置的路径
--conf-path=path 配置nginx配置文件的路径,如果不指定这个选项,那么配置文件的默认路径就会是 prefix/conf/nginx.conf
--pid-path =path 配置nginx.pid file的路径,一般来说,进程在运行的时候的时候有一个进程id,这个id会保存在pid file里面,默认的pid file的放置位置是prefix/logs/nginx.pid
--error-log-path=path 设置错误日志的存放路径,如果不指定,就默认 prefix/logs/error.log
--http-log-path= path 设置http访问日志的路径,如果不指定,就默认 prefix/logs/access.log
--user=name 设置默认启动进程的用户,如果不指定,就默认 nobody
--group=name 设置这个用户所在的用户组,如果不指定,依然是nobody
--with-http_ssl_module 开启HTTP SSL模块,使NGINX可以支持HTTPS请求。需要安装了OPENSSL
--with-http_flv_module
--with-http_stub_status_module 启用 "server status" 页
--without-http_gzip_module 禁用 ngx_http_gzip_module. 如果启用,需要 zlib
--without-http_ssi_module 禁用 ngx_http_ssi_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_browser_module 禁用 ngx_http_browser_module
--http-proxy-temp-path=PATH 设置路径到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-openssl=DIR 设置路径到OpenSSL library sources
--with-stream 用来实现四层协议的转发、代理或者负载均衡等

知识补充

configure 完成后会看到用到的扩展,及nginx的详细信息。

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + 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"

相关命令

#查看nginx命令
/usr/local/nginx/sbin/nginx -h
#直接启动
/usr/local/nginx/sbin/nginx
#停止
/usr/local/nginx/sbin/nginx -s stop
重启
/usr/local/nginx/sbin/nginx -s reload

其他补充:
如果想要使用service nginx restart、service nginx stop 相关的命令,需要将nginx加到service下
参考链接:https://www.cnblogs.com/shihaiming/p/6290219.html

错误记录

编译安装后,在其他地方直接使用nginx命令时,提示需要安装相关nginx的包,但安装后会在/ect/下重新安装nginx,而且会覆盖之前在/usr/local下安装的nginx,网上说是我扩展安装的不对,但是重装后这个问题依然存在。想要在/usr/local/下管理nginx,只能在命令前加上nginx的路径了../sbin/nginx -h -s -c

root@iZ2ze1kijdfc2i0715ucdzZ:~/nginx-1.16.1# nginx -h
The program 'nginx' can be found in the following packages:
 * nginx-core
 * nginx-extras
 * nginx-full
 * nginx-light
Try: apt install <selected package>

这个问题我会再找一找的,看有什么解决的方法。找到了会补充上来。

apt-get 安装

apt-get和apt是ubuntu下自带的包管理工具,apt 算是apt-get的升级版,ununtu16.04之后就可以使用apt,所以这里也可以使用apt instll

直接安装

#更新源
sudo apt-get update
#安装扩展
sudo apt-get install build-essential
sudo apt-get install libtool
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
sudo apt-get install openssl libssl-dev
#安装nginx
apt-get install nginx

补充

#查看目前源内的包的版本及相关扩展
apt-cache showpkg nginx
#安装指定版本的包
apt-cache madison <<package name>>

目录结构

  • 所有的配置文件都在/etc/nginx下,并且每个虚拟主机已经安排在了/etc/nginx/sites-available下
  • 程序文件在/usr/sbin/nginx
  • 日志放在了/var/log/nginx中
  • 并已经在/etc/init.d/下创建了启动脚本nginx
  • 默认的虚拟主机的目录设置在了/var/www/nginx-default (有的版本 默认的虚拟主机的目录设置在了/var/www, 请参考/etc/nginx/sites-available里的配置)
root@iZ2ze1kijdfc2i0715ucdzZ:/etc/nginx# ls
conf.d        fastcgi_params  koi-win     nginx.conf    scgi_params      sites-enabled  uwsgi_params
fastcgi.conf  koi-utf         mime.types  proxy_params  sites-available  snippets       win-utf

命令管理

#nginx -h
-?, -h  打印帮助。
-v  印刷版。
-V  打印NGINX版本,编译器版本并配置参数。
-t  不要运行,只需测试配置文件。NGINX检查配置中的语法是否正确,然后尝试打开配置中引用的文件。
-q  在配置测试期间禁止显示非错误消息。
-s signal(stop, quit, reopen, reload) 向主进程发送信号:停止,退出,重新打开,重新加载。(版本> = 0.7.53)
-p prefix 设置前缀路径(默认值:)/usr/local/nginx/。(版本> = 0.7.53)
-c filename 指定NGINX应该使用哪个配置文件代替默认配置文件。
-g directives 设置全局指

#service命令
service nginx configtest #测试配置文件。NGINX检查配置中的语法是否正确,然后尝试打开配置中引用的文件。
service nginx reload  #重新加载配置文件   
service nginx restart #重启      
service nginx rotate  #重新加载配置 ?    
service nginx start   #启动
service nginx status  #查看状态     
service nginx stop #停止          
service nginx upgrade #检查更新

#/etc/init.d/nginx 命令
/etc/init.d/nginx start|stop|reload|restart

结尾

在最好的年纪不要辜负年华。

添加新评论