
作用:外网能访问了!!耶
[tocm]
sudo apt-get install nginx
服务器里nginx会创建 /var/www/
二进制rpm安装包长名字)nginx-1.22.0-1.el7.ngx.x86_64.rpm# 安装命令:rpm -ivh nginx-1.22.0-1.el7.ngx.x86_64.rpm如果安装不了提示:Header V4 RSA/SHA256 Signature后面加 --force --nodepsrpm -ivh nginx-1.22.0-1.el7.ngx.x86_64.rpm--force --nodeps
find / -name nginx/etc/logrotate.d/nginx/etc/nginx/var/log/nginx/access.log # 成功日志/var/log/nginx/error.log # 错误日志/var/cache/nginx/usr/sbin/nginx # 主进程/usr/lib64/nginx/usr/share/nginx/index.html # 欢迎页/usr/share/nginx/500.html # 错误页/usr/libexec/initscripts/legacy-actions/nginx
可以立刻用systemctlsystemctl status nginxsystemctl start nginxsystemctl stop nginxsystemctl reload nginx
编译安装包(短名字)http://nginx.org/download/http://nginx.org/en/download.html
auto # auto文件夹中主要有4个目录:cc、lib、os、types,auto目录主要是为了辅助configure执行时Nginx要支持哪些模块、当前系统支持哪些特性来供Nginx使用;GHNAGE # 当前版本相较于前面版本有哪些变化,新增支持哪些特性(Feature)和修复了哪些bug(Bugfix)CHANGE.ru # 俄语版的CHANGE文件conf # 实例文件。在Nginx安装好后,会把configure的实例文件拷贝到conf文件中,方便运维使用configure # 配置脚本,用来生成中间文件,执行编译前的必备动作contrib # Perl脚本,使Nginx的语法在vim中显示html # 包括 50x.html, index.html 两个html文件LICENSEman # Nginx的帮助文件READMEsrc # Nginx的源代码(core, event, http, mail, misc, os, stream)
gcc --version # gcc有无安装g++ --version # g++有无安装yum -y install pcre-devel zlib-devel gcc gcc-c++ maketar -zxvf nginx-1.17.6.tar.gz # 解压2层直到文件夹,其他解压软件也可以cd nginx-1.17.6/ # 进入文件夹useradd -M -s /sbin/nologin nginx # 新建用户 nginx
无参数的./configure命令:./configure有参数的./configure命令:./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module./configure --help | more# 第一类参数:指定Nginx的安装路径:--prefix=/home/nginx (默认)--prefix=PATH # set installation prefix指定安装目录--sbin-path=PATH--modules-path=PATH--conf-path=PATH# 第二类参数:确定Nginx要使用哪些模块、不使用哪些模块:# "--with" 开头的模块是默认 编译进Nginx的模块,使用with可指定其加入Nginx# "--without" 开头的模块是默认 不 编译进Nginx的模块,使用without可指定其不加入Nginx--with-select_module--with-http_ssl_module# --with-http_stub_status_module # 启用此模块支持状态统计--without-http_gzip_module--without-http_charset_module# 第三类参数:一些特殊选项:--with-cc=PATH # 指定C编译器路径--with-cpp=PATH # 指定CPP编译器路径--with-ld-opt=OPTIONS
configure 后会生成一些中间文件objs/ngx_module.c 它决定了在后面编译时有哪些模块会被编译进Nginx。
#include <ngx_config.h>#include <ngx_core.h>extern ngx_module_t ngx_core_module;extern ngx_module_t ngx_errorlog_module;...ngx_module_t *ngx_modules[] = {&ngx_core_module,&ngx_errorlog_module,...};char *ngx_module_names[] = {"ngx_core_module","ngx_errlog_module",...};
make执行 make 编译。编译后,会生成大量的中间文件(例如 .o 文件) 和 最后的可执行二进制文件(nginx)。在 objs 目录中可以看到 可执行二进制文件 nginx;在 objs/src 目录下可以看到C语言编译时生成的所有中间文件(.o 文件)。
make install执行 make install 安装。(首次安装时可以通过执行 make install 安装)随后,在 --prefix 指定的目录中就可以看到 安装好后的 Nginx。
# 可以看到nginx的全部目录find / -name nginx/etc/nginx/nginx.conf/usr/local/sbin/nginx 最主要的可执行二进制文件var/logs/access.logvar/logs/error.log
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
nginx -t # 检查配置文件是否配置正确
nginx -v # 版本号
### 加入systemctl服务(推荐)
cd /usr/lib/systemd/system
cp httpd.service nginx.service
vim nginx.service
或者自己修改拖进去
```[Unit] 服务描述模块Description=The Nginx HTTP ServerAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forking ////后台执行模式EnvironmentFile=/usr/local/nginx/logs/nginx.pid ////PID存放路径ExecStart=/usr/local/nginx/sbin/nginx ///开启命令ExecReload=/usr/local/nginx/sbin/nginx -s reload ///重启命令ExecStop=/usr/local/nginx/sbin/nginx -s stop ///停止命令# We want systemd to give httpd some time to finish gracefully, but still want# it to kill httpd after TimeoutStopSec if something went wrong during the# graceful stop. Normally, Systemd sends SIGTERM signal right after the# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give# httpd time to finish.PrivateTmp=true ///给服务分配临时空间[Install]WantedBy=multi-user.target ////服务用户的模式
chmod +x nginx.servicekill -9 123456 # kill掉之前的进程systemctl start nginx
vi /etc/init.d/nginx复制以下代码,成为service
#!/bin/bash# nginx Startup script for the Nginx HTTP Server## chkconfig: - 85 15# description: Nginx is a high-performance web and proxy server.# It has a lot of features, but it's not for everyone.# processname: nginx# pidfile: /var/run/nginx.pid# config: /usr/local/nginx/conf/nginx.conf########### need to modify the path ###################nginxd=/usr/local/nginx/sbin/nginxnginx_config=/usr/local/nginx/conf/nginx.confnginx_pid=/usr/local/nginx/nginx.pid# # ################################RETVAL=0prog="nginx"# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ ${NETWORKING} = "no" ] && exit 0[ -x $nginxd ] || exit 0# Start nginx daemons functions.start() {if [ -e $nginx_pid ];thenecho "nginx already running...."exit 1fiecho -n $"Starting $prog: "daemon $nginxd -c ${nginx_config}RETVAL=$?echo[ $RETVAL = 0 ] && touch /var/lock/subsys/nginxreturn $RETVAL}# Stop nginx daemons functions.stop() {echo -n $"Stopping $prog: "killproc $nginxdRETVAL=$?echo[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid}# reload nginx service functions.reload() {echo -n $"Reloading $prog: "$nginxd -s reload#if your nginx version is below 0.8, please use this command: "kill -HUP `cat ${nginx_pid}`"RETVAL=$?echo}# See how we were called.case "$1" instart)start;;stop)stop;;reload)reload;;restart)stopstart;;status)status $progRETVAL=$?;;*)echo $"Usage: $prog {start|stop|restart|reload|status|help}"exit 1esacexit $RETVAL
cd /etc/rc.d/init.dchmod +x nginx/sbin/chkconfig --level 345 nginx on任何位置都能运行 service nginx start
阿里云esc - 安全组 - 入方向 - 端口范围80/80
授权对象0.0.0.0/0
浏览器
公网地址
就可以看到nginx