1.下载并安装

  1. # 1.解压缩文件 添加依赖
  2. sudo apt-get install libpcre3 libpcre3-dev zlib1g-dev build-essential gcc
  3. mkdir /usr/local/nginx
  4. tar -zxvf nginx-1.16.0.tar.gz
  5. # 2.编译安装
  6. cd nginx-1.16.0
  7. ./configure --prefix=/usr/local/nginx
  8. make && make install
  9. #3.开机自启、启动、停止
  10. sudo systemctl enable nginx.service
  11. sudo systemctl start nginx.service
  12. sudo systemctl stop nginx.service
  13. # 重启
  14. /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  15. or
  16. cd /usr/local/nginx/sbin
  17. ./nginx
  1. # nginx的热重载
  2. nginx -s signal
  3. signal 的值如下:
  4. stopfast shutdown,快速的停止 nginx
  5. quitgraceful shutdown,不再接受新的请求,等正在处理的请求出完成后在进行停止(优雅的关闭)
  6. reloadreloading the configuration file,重新加载配置文件
  7. reopenreopening the log files,重新写入日志文件
  8. ./nginx -s reload # 修改后config配置文件之后,重新加载配置文件

2.其他操作

  1. ## 查看进程号
  2. cd /usr/local/nginx/sbin
  3. ./nginx
  4. # nginx服务启动后默认的进程号会放在/usr/local/nginx/logs/nginx.pid文件
  5. cat nginx.pid 查看进程号
  6. ## ./nginx -v 显示nginx的版本号
  7. ./nginx -V 显示nginx的版本号和编译信息
  8. ./nginx -t 检查nginx配置文件的正确性
  9. ./nginx -t 检查nginx配置文件的正确定及配置文件的详细配置内容
  10. ./nginx -s 向主进程发送信号,如:./nginx -s reload 配置文件变化后重新加载配置文件并重启nginx服务
  11. ./nginx -p 设置nginx的安装路径
  12. ./nginx -c 设置nginx配置文件的路径
  13. ## 显示nginx的版本号
  14. root@ubuntu:/usr/local/src/nginx-1.6.2# sudo /usr/local/nginx/sbin/nginx -V
  15. nginx version: nginx/1.6.2
  16. built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.11)
  17. configure arguments: --prefix=/usr/local/nginx

3.加装SSL模块

  1. # 查看nginx中包含有哪些模块功能
  2. cd nginx/sbin
  3. ./nginx -V
  4. # 1.需要用到的依赖包
  5. apt install openssl libssl-dev
  6. # 2.在原来的nginx接压缩包中,添加ssl模块
  7. cd /usr/local/src/nginx_1.6.0
  8. ./configure
  9. ./configure --with-http_ssl_module
  10. make # 这里只能make,不能make install 否则会被覆盖
  11. # 备份nginx并用新的nginx文件替换
  12. cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
  13. cp -rfp objs/nginx /usr/local/nginx/sbin/nginx
  14. # 此时如果 ./nginx -s reload 你会发现没有生效;我们需要将原来的服务停了,重新启动
  15. ./nginx -V
  16. # 可以查看到新添加的模块TLS SNI support enabled configure arguments: --with-http_ssl_module

4. 加装stream模块,用来转发数据库

  1. # 1. 在安装Nginx的时候,需要额外安装几个依赖包
  2. sudo apt-get gcc g++ install libpcre3 libpcre3-dev zlib1g-dev build-essential
  3. # 额外的包包括(pcre-8.43、 zlib-1.2.11、openssl-1.0.2u)-openssl在16.03LTS已安装
  4. # 安装方式 (root权限)
  5. ./config
  6. make && make install
  7. # 安装Nginx(文件夹要存在):加装stream和ssl模块
  8. cd nginx-1.16.0
  9. ./configure --prefix=/usr/local/nginx --with-pcre=../pcre-8.43 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.0.2u --with-http_ssl_module --with-stream --conf-path=/usr/local/nginx/conf/nginx.conf
  10. # 检查nginx的安装是否成功
  11. cd /usr/local/nginx/sbin
  12. ./nginx -t
  1. # 在nginx.conf中添加的内容,该内容添加在了HTTP的上面,跟Http同一层级
  2. #user nobody;
  3. worker_processes 1;
  4. events {
  5. worker_connections 10240;
  6. }
  7. #使用nginx做数据库端口转发
  8. stream {
  9. upstream sql {
  10. # 配置数据库的ip和端口
  11. server 172.16.116.100:23306 weight=1 max_fails=2 fail_timeout=30s;
  12. }
  13. server {
  14. # 配置本机暴露端口
  15. listen 13000;
  16. proxy_connect_timeout 1s;
  17. proxy_timeout 3s;
  18. proxy_pass sql;
  19. }
  20. }
  21. http {
  22. include mime.types;
  23. default_type application/octet-stream;
  24. ... ...
  25. ## 配置完效果: 用户可以通过navicat,访问虚拟机内的数据库; 无需在宿主机上安装工具才能访问到数据库。