安装环境:Centos 6.4 32位 nginx1.6.3
一、安装prce(重定向支持)和openssl(https支持)
[ptt003@pfq ~]$ sudo yum install -y pcre pcre-devel[ptt003@pfq ~]$ sudo yum install -y openssl openssl-devel
二、下载解压nginx1.6.3
[ptt003@pfq ~]$ wget http://nginx.org/download/nginx-1.6.3.tar.gz[ptt003@pfq ~]$ tar zxf nginx-1.6.3.tar.gz
三、编译安装nginx
[ptt003@pfq ~]$ cd nginx-1.6.3[ptt003@pfq nginx-1.6.3]$ lltotal 624drwxr-xr-x. 6 ptt003 ptt003 4096 Nov 20 23:57 auto-rw-r--r--. 1 ptt003 ptt003 236608 Apr 7 2015 CHANGES-rw-r--r--. 1 ptt003 ptt003 360501 Apr 7 2015 CHANGES.rudrwxr-xr-x. 2 ptt003 ptt003 4096 Nov 20 23:57 conf-rwxr-xr-x. 1 ptt003 ptt003 2369 Apr 7 2015 configuredrwxr-xr-x. 4 ptt003 ptt003 4096 Nov 20 23:57 contribdrwxr-xr-x. 2 ptt003 ptt003 4096 Nov 20 23:57 html-rw-r--r--. 1 ptt003 ptt003 1397 Apr 7 2015 LICENSEdrwxr-xr-x. 2 ptt003 ptt003 4096 Nov 20 23:57 man-rw-r--r--. 1 ptt003 ptt003 49 Apr 7 2015 READMEdrwxr-xr-x. 8 ptt003 ptt003 4096 Nov 20 23:57 src创建一个nginx用户[ptt003@pfq nginx-1.6.3]$ sudo useradd -s /sbin/nologin -M nginx编译安装nginx[ptt003@pfq nginx-1.6.3]$ sudo ./configure --user=nginx --group=nginx --prefix=/home/ptt003/nginx --with-http_stub_status_module --with-http_ssl_module[ptt003@pfq nginx-1.6.3]$ echo $?0 若不是0,查找并解决错误后重试[ptt003@pfq nginx-1.6.3]$ sudo make[ptt003@pfq nginx-1.6.3]$ echo $?0[ptt003@pfq nginx-1.6.3]$ sudo make install[ptt003@pfq nginx-1.6.3]$ echo $?0
四、启动nginx
[ptt003@pfq nginx-1.6.3]$ cd ../nginx[ptt003@pfq nginx]$ lltotal 16drwxr-xr-x. 2 root root 4096 Nov 21 00:23 confdrwxr-xr-x. 2 root root 4096 Nov 21 00:23 htmldrwxr-xr-x. 2 root root 4096 Nov 21 00:23 logsdrwxr-xr-x. 2 root root 4096 Nov 21 00:23 sbin[ptt003@pfq nginx]$ sudo sbin/nginx -tnginx: the configuration file /home/ptt003/nginx/conf/nginx.conf syntax is oknginx: configuration file /home/ptt003/nginx/conf/nginx.conf test is successful[ptt003@pfq nginx]$ sudo sbin/nginx查看nginx是否启动成功[ptt003@pfq nginx]$ ps aux|grep nginxroot 7691 0.0 0.0 7536 796 ? Ss 00:26 0:00 nginx: master process sbin/nginxnginx 7692 0.0 0.1 7704 1160 ? S 00:26 0:00 nginx: worker processptt003 7709 0.0 0.0 4356 736 pts/2 S+ 00:27 0:00 grep nginxnginx的重启[ptt003@pfq nginx]$ sudo sbin/nginx -s reload
五、开放防火墙80端口,重启后访问测试
[ptt003@pfq nginx]$ sudo vim /etc/sysconfig/iptables# Firewall configuration written by system-config-firewall# Manual customization of this file is not recommended.*filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [0:0]-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT-A INPUT -p icmp -j ACCEPT-A INPUT -i lo -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 3690 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 8090 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -j REJECT --reject-with icmp-host-prohibited-A FORWARD -j REJECT --reject-with icmp-host-prohibitedCOMMIT:wq[ptt003@pfq nginx]$ sudo /etc/init.d/iptables restart[ptt003@pfq nginx]$ sudo sbin/nginx -s reload
打开浏览器访问服务器ip,出现以下内容即成功
Welcome to nginx!
若出现403 Forbidden错误,一般为权限错误,请重新配置后重启nginx访问!
如:nginx安装目录是/home/ptt003/nginx,则nginx用户要对./nginx/html/index.html有可读权限,对/home/ptt003/nginx/html,/home/ptt003/nginx,/home/ptt003/,/home等目录均有可执行权限。
六、修改nginx配置文件,使可以域名访问
[ptt003@pfq nginx]$ sudo vim conf/nginx.confserver { listen 80; server_name bbs.ptteng.com; location / { root /html/bbs; index index.php index.html; :wq[ptt003@pfq nginx]$ cd html/ [ptt003@pfq html]$ sudo mkdir bbs[ptt003@pfq html]$ cd bbs[ptt003@pfq bbs]$ sudo cp ../index.html .[ptt003@pfq bbs]$ sudo sed -i 's#Welcome to nginx#I LOVE YOU#g' index.html[ptt003@pfq nginx]$ sbin/nginx -s reload
在window中,以管理员身份打开记事本,然后打开C:\Windows\System32\drivers\etc/hosts,添加 192.168.1.118(ip) bbs.ptteng.com(域名) 保存退出
打开浏览器访问 bbs.ptteng.com,出现以下内容即成功