一,Nginx安装
1.安装nginx
#yum -y install gcc pcre-devel openssl-devel //安装nginx依赖
#useradd -s /sbin/nologin nginx
#tar -xf nginx-1.10.3.tar.gz
#cd nginx-1.10.3/
通过help查看该软件支持的模块
#./configure --help
#./configure --prefix=/usr/local/nginx //指定安装路径(默认也是安装在这个目录下)
--user=nginx \ //指定用
--group=nginx \ //指定组
--with-http_ssl_module //开启ssl加密
--with-stream //开启tcp/ip功能
--with-http_stub_status_module //查看nginx的状态信息
#make && make install
#ln -s /usr/local/sbin/nginx /sbin
#nginx
#nginx -V //查看nginx已安装的模块、看版本
2,启动服务
/usr/local/nginx/sbin/nginx
# /usr/local/nginx/sbin/nginx -s stop
# /usr/local/nginx/sbin/nginx -s reload
# /usr/local/nginx/sbin/nginx -V
# ln -s /usr/local/nginx/sbin/nginx /sbin/
查看进程及端口
# netstat -antulp | grep nginx
# ps -aux | grep nginx
3,测试
# curl http://127.0.0.1
二,Nginx升级
1)编译新版本nginx软件
#tar -xf nginx-1.12.2.tar.gz
# cd nginx-1.12.2
# ./configure --with-http_ssl_module
# make
2) 备份老的nginx主程序,并使用编译好的新版本nginx替换老版本
# cd /usr/local/nginx/sbin/
# mv nginx nginxold //改名旧脚本
# cd /root/nginx-1.12.2
# cp objs/nginx /usr/local/nginx/sbin/ //拷贝新脚本
# make upgrade //升级软件
# /usr/local/nginx/sbin/nginx -V //查看软件版本、安装模块
# ifconfig
# curl http://192.168.1.108 //命令行工具测试nginx页面
三,Nginx配置文件的解析
# vim /usr/local/nginx/conf/nginx.conf
- # vim /usr/local/nginx/conf/nginx.conf
- 全局配置(用户名,日志,进程)
- http{
- server{
- listen 80;
- server_name localhost;
- root html;
- }
- server{
- listen 80;
- server_name www.xyz.com;
- root www;
- }
- }
Nginx实现Web页面的认证,需要修改Nginx配置文件,在配置文件中添加auth语句实现用户认证。最后使用htpasswd命令创建用户及密码即可
# vim /usr/local/nginx/conf/nginx.conf
35 server {
36 listen 80;
37 server_name localhost;
38 auth_basic "Input Passwrod"; #认证提示符
39 auth_basic_user_file "/usr/local/nginx/pass"; #认证密码文件
40
41 charset koi8-r;
生成密码文件,创建用户及密码
使用htpasswd命令创建账户文件,需要确保系统中已经安装了httpd-tools。
# yum -y install httpd-tools
# htpasswd -c /usr/local/nginx/pass zl //创建密码文件
# htpasswd /usr/local/nginx/pass Nicoke //追加用户,不能加-c
# cat /usr/local/nginx/pass //查看创建的用户
# nginx -s reload //重新加载配置文件
测试访问
Firefox http://139.159.213.217/ //输入密码可以访问
===============================完工=======================================