💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
1.下载源码包,官网找最新稳定版本 ``` wget http://nginx.org//download/nginx-1.16.0.tar.gz ``` 也可以直接官网下载到PC本地 xshell 使用rz命令上传 或是ftp上传 2.解压.通常下载到/usr/local/src ,直接解压到该目录,用来专门放置安装压缩包和解压源码 3.配置参数. ``` ./configure --help #查看可以设置哪些参数 ``` 其中 --prefix=install_path 这个参数是指定 make install的时候 软件安装的目录 4.编译 ``` make [按照Makefile生成模块] ``` 5.安装 ``` make install ``` ###以nginx为例 1.基础概念和依赖 nginx安装可以使用yum或源码安装,推荐使用源码,一是yum的版本比较旧,二是使用源码可以自定义功能,方便业务的上的使用,源码安装需要提前准备标准的编译器,GCC的全称是(GNU Compiler collection),其有GNU开发,并以GPL即LGPL许可,是自由的类UNIX即苹果电脑Mac OS X操作系统的标准编译器,因为GCC原本只能处理C语言,所以原名为GNU C语言编译器,后来得到快速发展,可以处理C++,Fortran,pascal,objective-C,java以及Ada等其他语言,此外还需要Automake工具,以完成自动创建Makefile的工作,Nginx的一些模块需要依赖第三方库,比如pcre(支持rewrite),zlib(支持gzip模块)和openssl(支持ssl模块) 官网http://nginx.org/en/download.html下载最新稳定版本到本地 本例子 登录Xshell连接服务器[阿里云ECS] ``` yum install lrzsz #用来使用rz命令 cd /usr/local/src #进入用户编译安装源码目录 rz # rz是代表上传 会弹出window的选择文件窗口,找到上面步骤下载的压缩包,确定后会下载到当前目录下 yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel # 安装nginx的编译依赖 ``` 2.解压 ``` tar -vxf nginx-1.16.0.tar.gz # 解压到当前目录 ll #查看解压后的当前目录下的所有文件 # 主要目的看解压后的文件名,一般为去掉压缩为后缀的文件名,此处为nginx-1.16.0 cd nginx-1.16.0 ``` 3.配置编译安装参数[如果想自己管理自己的软件安装,注意配置--prefix=你管理自己编译安装软件的目录路径] ``` ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/usr/local/nginx/logs/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre ``` 4.make [编译] ``` 我在这里出现两个问题 . 解决方案应该都在编译前就执行 >编译参数指定了用户和用户组 如果不存在会报错 所以要先创建nginx用户 > adduser nginx > groupadd nginx >编译参数指定了日志目录 可能该目录目录路径没有可写权限 比如 日志放在 /var/log/nginx/下 但是 /var 目录不可写 会导致安装报错 ``` 5.make install [安装] 此文章仅为学习记录,原文章步骤非常详细和好理解,见https://www.cnblogs.com/zhang-shijie/p/5294162.html 后续使用 ``` /usr/local/nginx/sbin/nginx #启动nginx,通常没有输出任何信息,且下一步满足即成功 #遭遇了netstat -ntlp #查看网络连接进程,看到nginx进程即为成功, # 如果 -bash: netstate: command not found 则 yum install net-tools #在浏览器输入服务器IP或解析过的域名 #会显示/usr/local/nginx/html/index.html ``` 启动过程可能会提示/var/run/nginx/nginx.pid 不存在或是不是一个文件之类的提示 建议调整配置文件/usr/local/nginx/conf/nginx.conf里的pid参数,指定/usr/local/nginx/logs/nginx.pid [记得先创建logs目录] 设置为开机自启 创建nginx启动命令脚本 ``` vim /etc/init.d/nginx #内容如下 ``` 插入以下内容, 注意修改PATH和NAME字段, 匹配自己的安装路径 (这段是从网上copy的) 不过查看了相关参数,跟我的编译参数完全匹配 ``` #! /bin/bash # Startup script for the nginx Web Server # chkconfig: - 85 15 # description: nginx is a World Wide Web server. It is used to serve PATH=/usr/local/nginx DESC="nginx daemon" NAME=nginx DAEMON=$PATH/sbin/$NAME CONFIGFILE=$PATH/conf/$NAME.conf PIDFILE=$PATH/logs/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME set -e [ -x "$DAEMON" ] || exit 0 do_start() { $DAEMON -c $CONFIGFILE || echo -n "nginx already running" } do_stop() { $DAEMON -s stop || echo -n "nginx not running" } do_reload() { $DAEMON -s reload || echo -n "nginx can't reload" } case "$1" in start) echo -n "Starting $DESC: $NAME" do_start echo "." ;; stop) echo -n "Stopping $DESC: $NAME" do_stop echo "." ;; reload|graceful) echo -n "Reloading $DESC configuration..." do_reload echo "." ;; restart) echo -n "Restarting $DESC: $NAME" do_stop do_start echo "." ;; *) echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2 exit 3 ;; esac exit 0 ``` 设置执行权限 ``` chmod a+x /etc/init.d/nginx ``` 注册成服务 ``` chkconfig --add nginx ``` 设置开机启动 ``` chkconfig nginx on ``` 重启, 查看验证nginx服务是否自动启动 ``` #重启 reboot #或 shutdown -h 0 -r #查看进程 ps aux | grep nginx #或 netstat -nplt | grep nginx ``` 对nginx服务执行停止/启动/重新读取配置文件操作 ``` #启动nginx服务 systemctl start nginx.service #停止nginx服务 systemctl stop nginx.service #重启nginx服务 systemctl restart nginx.service #重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效) systemctl reload nginx.service ``` 成功   # 配置将请求转发给php-fpm处理 php-fpm的默认端口号是9000 主配置 ``` user www; worker_processes 1; error_log logs/error.log; error_log logs/error.log notice; error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } 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"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; #开启所有的虚拟主机目录可访问,想要仅指定的主机的目录可访问 去对应的server下配置 #autoindex on; #autoindex_exact_size off; ##autoindex_exact_size off; #虚拟主机配置 自动加载 include /usr/local/nginx/conf/nginx.conf.vhost/*.conf; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } ``` 虚拟主机配置 ``` # another virtual host using mix of IP-, name-, and port-based configuration # server { charset utf-8; listen 80; server_name blog.54skyer.cn; set $root /home/www/blog.54skyer.cn/public; root $root; #root /home/www/blog.54skyer.cn/public; index index.php index.html; location /zhanghaotian/ { autoindex on; } location / { # 如果根目录下匹配不是脚本 默认在根目录后拼一个index.php 这是为了在url中省略index.php if (!-e $request_filename){ rewrite ^(.*) /index.php/$1 last; break; } } #pathinfo配置 使支持tp5的标准url location ~ .+\.php($|/) { fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^((?U).+.php)(/?.+)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name; include fastcgi_params; } # 匹配到php扩展名的url #location ~\.php$ { # include /usr/local/nginx/conf/fastcgi.conf; # fastcgi_intercept_errors on; # #转发给php-fpm,其端口是9000; # fastcgi_pass 127.0.0.1:9000; #} } # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} ```