nginx安装与配置
一、操作环境:
[root@host3 ~]# cat /etc/redhat-release CentOS release 6.7 (Final)
二、安装依赖包:
[root@host3 ~]# yum -y install openssl-devel pcre-devel gcc
三、安装nginx:
[root@host3 ~]# mkdir -p /usr/local/service [root@host3 ~]# cd /usr/local/service/ [root@host3 service]# wget http://nginx.org/download/nginx-1.8.0.tar.gz [root@host3 service]# tar xvf nginx-1.8.0.tar.gz [root@host3 service]# cd nginx-1.8.0 [root@host3 nginx-1.8.0]# ./configure [root@host3 nginx-1.8.0]# make && make install [root@host3 nginx-1.8.0]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ #创建软连接 [root@host3 nginx-1.8.0]# nginx #启动nginx [root@host3 nginx-1.8.0]# netstat -nlptu | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 55896/nginx #查看nginx是否启动成功 [root@host3 nginx-1.8.0]# nginx -s stop #关闭nginx [root@host3 nginx-1.8.0]# nginx -s reload #重新加载nginx [root@host3 nginx-1.8.0]# nginx -v nginx version: nginx/1.8.0 #查看nginx的版本号
四、网页测试nginx是否安装成功:
[root@host3 nginx-1.8.0]# curl http://localhost:80 <!DOCTYPE html> <html> <head> ………… ………… <h1>Welcome to nginx!</h1> ………… ………… <p><em>Thank you for using nginx.</em></p> </body> </html> #已可正常网页访问nginx [root@host3 nginx-1.8.0]# nginx -s stop [root@host3 nginx-1.8.0]# curl http://localhost:80 curl: (7) couldn't connect to host #关闭nginx,再访问就提示无法连接 [root@host3 nginx-1.8.0]# nginx
五、nginx文件配置:
[root@host3 conf]# cd /usr/local/nginx/conf/ [root@host3 conf]# vim nginx.conf …… http{ …… server { #定义侦听端口8080,如服务器有Tomcat,慎用此端口,可换其他的 listen 8080; #定义访问的URL server_name www.nginx1.com; location / { #定义服务器的默认网站根目录位置 root web1; #定义首页索引文件的名称 index index.html; } } server { listen 8080; server_name www.nginx2.com; location / { root web2; index index.html; } } } :wq #一定一定一定注意server{}之间是平级关系,注意{}的对应关系!!!
六、测试配置文件是否生效:
[root@host3 conf]# mkdir -p /usr/local/nginx/web{1,2} [root@host3 conf]# echo "Hello,I'm nginx1" > /usr/local/nginx/web1/index.html [root@host3 conf]# echo "Hello,I'm nginx2" > /usr/local/nginx/web2/index.html [root@host3 conf]# nginx -s reload [root@host3 conf]# echo "192.168.0.241 www.nginx1.com www.nginx2.com" >> /etc/hosts [root@host3 conf]# curl http://www.nginx1.com:8080 Hello,I'm nginx1 [root@host3 conf]# curl http://www.nginx2.com:8080 Hello,I'm nginx2