前提: 由于资源有限,本文中提到的CentOS是安装在vmware中。
1. 将当前用户paas添加到/etc/sudoers文件中
(这是为了后续操作中如果需要root权限时可能直接在命令前加sudo)
a) 使用su切换到root用户
b) cd /etc/ --> chmod +w sudoers -->注释Defaults requiretty -->在root ALL=(ALL) ALL下添加 paas ALL=(ALL) NOPASSWD:ALL
2. 使用sudo yum install –y nginx安装nginx
[paas@localhost Desktop]$ sudo yum install -y nginx
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
* base: mirrors.skyshe.cn
* extras: mirrors.cqu.edu.cn
* updates: mirrors.163.com
Setting up Install Process
No package nginx available.
Error: Nothing to do
没有nginx的yum源包,需要安装一个nginx的yum源文件
3. 安装一个nginx的yum源文件
[paas@localhost Desktop]$ sudo rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
Retrieving http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
warning: /var/tmp/rpm-tmp.CV8O5s: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing... ########################################### [100%]
1:nginx-release-centos ########################################### [100%]
[paas@localhost Desktop]$
再次安装nginx
[paas@localhost Desktop]$ sudo yum install -y nginx
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.skyshe.cn
* extras: mirrors.cqu.edu.cn
* updates: mirrors.163.com
nginx | 2.9 kB 00:00
nginx/primary_db | 6.8 kB 00:01
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:1.8.0-1.el6.ngx will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
nginx x86_64 1.8.0-1.el6.ngx nginx 352 k
Transaction Summary
================================================================================
Install 1 Package(s)
Total download size: 352 k
Installed size: 872 k
Downloading Packages:
nginx-1.8.0-1.el6.ngx.x86_64.rpm | 352 kB 00:16
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
Installing : nginx-1.8.0-1.el6.ngx.x86_64 1/1
----------------------------------------------------------------------
Thanks for using nginx!
Please find the official documentation for nginx here:
* http://nginx.org/en/docs/
Commercial subscriptions for nginx are available on:
* http://nginx.com/products/
----------------------------------------------------------------------
Verifying : nginx-1.8.0-1.el6.ngx.x86_64 1/1
Installed:
nginx.x86_64 0:1.8.0-1.el6.ngx
Complete!
[paas@localhost Desktop]$
4. 修改nginx配置文件
a)查看nginx安装文件
[paas@localhost /]$ sudo find ./ -name nginx
./var/lib/yum/repos/x86_64/6/nginx
./var/cache/yum/x86_64/6/nginx
./var/cache/nginx
./var/log/nginx
./home/paas/nginx 自己建的nginx目录
./usr/sbin/nginx 可执行程序
./usr/share/nginx 系统目录,存放html,查看里面的内容
./etc/rc.d/init.d/nginx 系统启用初始化的可执行程序
./etc/nginx 安装目录,查看里面的内容
./etc/logrotate.d/nginx
./etc/sysconfig/nginx 系统配置文件,查看里面的内容
Nginx默认安装在/etc/nginx目录下,配置文件在conf目录下,可执行文件是/usr/sbin/nginx(可通过which nginx查看),html文件在/usr/share/nginx/html文件下
b)修改nginx配置
vi nginx.conf
user nginx;
worker_processes 1;
error_log /home/paas/nginx/log/error.log warn; #modify 重新指向log文件
pid /home/paas/nginx/run/nginx.pid; #modify重新指向pid文件
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /home/paas/nginx/log/access.log main; #modify重新指向log文件
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
include /home/paas/nginx/config/*.conf; #add添加配置文件
}
5. 启动/关闭nginx
a) 查看nginx是否已经运行
ps –ef | grep nginx
b) 关闭nginx进程
运行ps –ef | grep nginx后获取nginx进程号,采用sudo kill -9 进程号的方式结束nginx的master进程和worker进程
sudo nginx –s shutdown或sudo nginx –s quit的方式结束nginx进程 (本机运行不成功)
c)开启nginx
sudo nginx
在当前服务器的浏览器中访问localhost,出现welcome to nginx页面表示nginx安装启动成功,接下来的工作就是结合工作进行进一步的配置了。
这里CentOS是在VMware上安装的,如果需要在windows上访问Nginx,还需要进一步的配置。
1. 检查在CentOS上能否ping能window机器。
2. 检查window上能否ping通CentOS
3. 检查80端口是否被CentOS防火墙过滤
配置防火墙命令
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT 加tcp 80端口加入防火墙中,允许外部访问tcp 80端口
/etc/init.d/iptables save 保存防火墙设置
/etc/init.d/iptables restart 重启防火墙
查看CentOS防火墙信息:/etc/init.d/iptables status
关闭CentOS防火墙服务:/etc/init.d/iptables stop
永久关闭防火墙: chkconfig –level 35 iptables off