1.安装nginx环境
uname -a
Linux localhost.localdomain 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
2.安装nginx需提前安装的软件
#查询软件安装目录
rpm -qa
rpm -ql xxxxxxxxxxx
#安装gcc编译器
yum -y install gcc
#nginx使用到正则表达式的话必须安装的软件,用于解析正则表达式。默认安装在 /usr/lib64/
yum -y install pcre pcre-devel
#安装zlib。用于响应体重压缩gzib。默认安装 /usr/lib64/
yum -y install zlib zlib-devel
#安装openssl。安装目录 /usr/lib64/openssl
yum -y install openssl oepnssl-devel /usr/lib64/
3.安装nginx
cd ~
mkdir soft
cd soft
wget http://nginx.org/download/nginx-1.11.4.tar.gz
tar -zxvf nginx-1.11.4.tar.gz
cd nginx-1.11.4
./configure --prefix=/usr/local/nginx
make
make install
4.修改nginx配置
cd /usr/local/nginx/
cd conf/
vi ./nginx.conf
nginx 配置
#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;
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;
# }
#}
}
5.启动,访问和停止nginx
cd ../sbin
./nginx
curl http://127.0.0.1:80
6.常用命令
#启动指定配置文件
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#指定nginx 安装目录
/usr/local/nginx/sbin/nginx -p /usr/local/nginx
#临时全局指定一些全局配置项。不能和已有的写在配置文件中的配置项相冲突,执行其他命令也要带上-g
/usr/local/nginx/sbin/nginx -g "pid /usr/local/nginx/log/test.pid"
#测试配置文件是否正确
/usr/local/nginx/sbin/nginx -t
#显示版本信息
/usr/local/nginx/sbin/nginx -v
#快速停止服务
/usr/local/nginx/sbin/nginx -s stop
#==等效于直接向master进程发送TERM和INT信号量
ps -ef | grep nginx
#显示如下进程:
root 7309 1 0 19:05 ? 00:00:00 nginx: master process ./nginx
nobody 7310 7309 0 19:05 ? 00:00:00 nginx: worker process
root 7405 2269 0 21:54 pts/0 00:00:00 grep --color=auto nginx
#执行:
kill -s SIGTERM 7309
or
kill -s SIGINT 7309
#优雅的退出服务。关闭监听端口,停止接收请求。处理完目前的任务后停止服务
/usr/local/nginx/sbin/nginx -s quit
#==等效于直接向master进程发送QUIT信号量。
#或者优雅的停止worker进程发送WINCH信号量
#nginx重读配置项并生效。实际上会检查配置项,优雅的关闭,再启动服务
/usr/local/nginx/sbin/nginx -s reload
#==等效于直接向master进程发送HUP信号量。
#日志文件回滚。重新生成日志文件,我们可以拷贝日志文件,以至于日志文件不会过大
/usr/local/nginx/sbin/nginx -s reopen
#==等效于直接向master进程发送USR1信号量。
#平滑升级
#向master进程发送USR2信号量。此时同时存在两个进程,可以手工平滑停止老进程
7.总结
无。