上次写的nginx安装是转载的,但是发现安装新版的nginx出现一些问题,此次自己博客记录一份,来做记录
安装流程参考博客原网址: https://www.cnblogs.com/zhang-shijie/p/5294162.html
下一篇文章我将会介绍一下nginx配置多个tomcat如何进行优化和配置
1、环境准备:先安装准备环境
yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel
2、下载nginx 安装包: 官网地址:http://nginx.org/
截止得到当前,最新的版本为1.8.1,下载下来得到nginx-1.8.1.tar.gz
3、解压安装包:
[[email protected] ~]# tar xvf nginx-1.8.1.tar.gz [[email protected] ~]# cd nginx-1.8.1 [[email protected] nginx-1.8.1]$ ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
4、编译nginx:./configure
直接将下面代码粘贴复制就行,主要是初始化一下配置
编译是为了检查系统环境是否符合编译安装的要求,比如是否有gcc编译工具,是否支持编译参数当中的模块,并根据开启的参数等生成Makefile文件为下一步做准备:
[[email protected] nginx-1.8.1]# ./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=/var/run/nginx/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
5、生成脚本及配置文件:make
编译步骤,根据Makefile文件生成相应的模块
6、安装:make install
创建目录,并将生成的模块和文件复制到相应的目录:
备注:nginx完成安装以后,有四个主要的目录:
conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件,配置文件一般都有个样板配置文件,是文件名.default结尾,使用的使用将其复制为并将default去掉即可。 html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。 logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。 sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。
这样nginx就安装完成了
下面是nginx的启动和重启以及关闭
首先我会先修改一下nginx的配置
我的方法是下载下来/usr/local/nginx/conf/nginx.conf,然后修改成如下方式:
#user nobody;
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;
upstream tomcats {
server 127.0.0.1:9080; #tomcat1的端口 此处默认是轮换轮询,可以自己设置根据ip或者有权重比,可以自行百度
server 127.0.0.1:8080; #tomcat2的端口
}
server {
listen 8090; #此处是监听端口号,默认是80,我一般会修改一下,防止冲突
server_name localhost1;
#charset koi8-r;
#access_log logs/host.access.log main;
location / { #默认是/拦截所有 此处可随意修改,比如/test,是拦截以/test开头的接口,
proxy_pass http://tomcats; #tomcats对应上面的upstream后面的tomcats
}
#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;
# }
#}
}
然后覆盖到linux下的nginx.conf
分别启动nginx和tomcat就可以分发了
启动nginx
[[email protected] ~]# cd /usr/local/nginx/sbin/
[[email protected] sbin]# ./nginx
重启
[[email protected] sbin]# ./nginx -s reload
停止:
可以杀死端口号,自行百度,也可以
[[email protected] sbin]# ./nginx -s stop
常见问题:
/usr/local/nginx/logs/nginx.pid路径下找不到nginx.pid
/usr/local/nginx/logs/nginx.pid
路径下找不到nginx.pid
错误信息
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
解决方法
执行一下nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf