设为首页 收藏本站
查看: 1157|回复: 0

[经验分享] 搭建nginx网站服务及应用

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-12-16 08:35:18 | 显示全部楼层 |阅读模式
实验环境:
服务器系统:Redhat 6.2             ip地址:192.168.10.1
客户机系统:Win7 64位 旗舰版   ip地址:192.168.10.2
系统环境:已搭建LAMP平台

1、搭建nginx服务并实现访问状态统计
[iyunv@localhost ~]#yum -y install pcre-devel zlib-devel  #首先需要安装这两个工具

[iyunv@localhost ~]#useradd -M -s /sbin/nologin nginx  #在系统中新建一个用户nginx不建立家目录,禁止登陆系统,作为nginx服务的专用账号

[iyunv@localhost ~]# mount.cifs //192.168.10.2/LAMP /opt  #从客户机上将共享的文件夹LAMP挂载到/opt目录下,nginx安装压缩包在这个文件夹中

[iyunv@localhost ~]#tar xzvf /opt/nginx-1.5.10.tar.gz  #解压nginx到/root目录(解压到哪里看个人需求)


[iyunv@localhost ~]#cd nginx-1.5.10/   
[iyunv@localhost nginx-1.5.10]# ./configure      #到nginx解压的源码目录进行./configure配置
--prefix=/usr/local/nginx       #指定安装路径
--user=nginx         #指定服务的使用者
--group=nginx      #指定服务的使用组
--with-http_stub_status_module    #启用状态统计模块

[iyunv@localhost nginx-1.5.10]#make
[iyunv@localhost nginx-1.5.10]#make install

[iyunv@localhost nginx-1.5.10]#ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/   #为程序创建软连接,以便使用nginx就可以直接调用nginx,不要输绝对路径来执行了

nginx -t                  #进行配置检查
nginx                  #启动服务
killall -1 nginx          #重启服务
killall -3 nginx          #停止服务

-------------------------------------------------------------------------------------
一般我们在linux系统中基本上都习惯于用service命令来控制服务的启动与停止等,这里提供一个其他网站看到的别人写的控制脚本,把以下内容添加到vim /etc/init.d/nginx里面保存即可实现
#!/bin/bash  
#  
# nginx      This shell script takes care of starting and stopping  
#            standalone nginx.  

# config: /usr/local/nginx/conf/nginx.conf  

# Source function library.  

. /etc/rc.d/init.d/functions  

RETVAL=0  
prog="nginx"  

start() {  
      #start nginx  
      [ -x /usr/local/nginx/sbin/nginx ] || exit 4  
      [ -z /usr/local/nginx/conf/nginx.conf ] && exit 6  
      echo -n $"Starting $prog: "  
      daemon /usr/local/nginx/sbin/nginx 2>/dev/null  
      RETVAL=$?  
      echo  
      return $RETVAL      
}  

stop () {  
     #stop nginx  
     echo -n $"Shutting down $prog: "  
     daemon /usr/local/nginx/sbin/nginx -s stop 2>/dev/null  
     RETVAL=$?  
     echo  
     return $RETVAL  
}  

reload () {  
     #reload  nginx  
     echo -n $"Reload the config of $prog: "  
     daemon /usr/local/nginx/sbin/nginx -s reload 2>/dev/null  
     RETVAL=$?  
     echo  
     return $RETVAL  
}  



# See how we were called.  
case "$1" in  
  start)  
        start  
        ;;  
  stop)  
        stop  
        ;;  
  restart)  
        stop  
        start  
        RETVAL=$?  
        ;;  
  reload)  
        reload  
        ;;  
  status)  
        status $prog  
        RETVAL=$?  
        ;;  
  *)  
        echo $"Usage: $0 {start|stop|restart|reload|status}"  
        exit 2  
esac  

exit $RETVAL  
------------------------------------------------------------------------------
wKiom1SOqRTSGOwOAADCacQoxjc651.jpg

cd /usr/local/nginx/conf  
mv nginx.conf nginx.conf.back  #将原来的配置文件做个备份以防配置出错可以导回
grep -v "#" nginx.conf.back | grep -v "^$" > nginx.conf  #过滤掉配置文件中的带有#号的行和空行(可根据个人习惯)

[iyunv@ling conf]# vi nginx.conf   #打开配置文件插入红色字的四行内容,注意{}需成对

server {
        listen       80;
        server_name  localhost;
        charset utf-8;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location ~ /status {    #访问位置为ip地址/status
        stub_status   on;       #打开状态统计功能
        access_log off;         #关闭此位置的日志记录
        }        

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       }
    }
在客户机上验证,在浏览器中输入192.168.10.1,看到以下画面的话nginx已经搭建成功

wKiom1SOqwqjW-VPAAKoL0d2rZg127.jpg
验证状态统计功能,在浏览器中输入192.168.10.1/status,将看到如下页面
wKioL1SOrKzC0VPgAAE8utpfcKo048.jpg
2、配置基于域名的虚拟主机
在web服务器当中运用比较广泛的功能还有虚拟主机,nginx当中也是可以配置的,在配置文件中插入以下这两个server块即可。

[iyunv@localhost ~]#vi nginx.conf
server {
        server_name  www.benet.com;      #输入域名
        location / {
            root   /var/www/benet;          #指定这个网站的网页根目录
            index  index.html index.php;   #指定默认打开的网页
        }
    }
    server {
       server_name  www.accp.com;
        location / {
            root   /var/www/accp;
            index  index.html index.php;
        }
    }

[iyunv@localhost ~]# mkdir /var/www/benet     #创建网站根目录
[iyunv@localhost ~]# mkdir /var/www/accp
[iyunv@localhost ~]# echo "this is benet" > /var/www/benet/index.html  #制作一个html的网页,内容为this is benet
[iyunv@localhost ~]# echo "this is accp" > /var/www/accp/index.html

[iyunv@localhost ~]# service nginx restart    #重启服务
关闭 nginx:                                               [确定]
正在启动 nginx:                                         [确定]

在验证之前我们配置了两个域名就必须要解析这两个域名,需要搭建DNS,这里为了方便,我们可以在客户机上修改hosts文件,将域名和ip地址对应的写进去保存
wKiom1SOrgvjL2EqAAH8e6AfQZA487.jpg
wKiom1SOr7bhilIVAAKYNYz1kY8200.jpg
注意:用记事本打开hosts文件并修改,如果无法修改可以将hosts拖到桌面修改,之后再拖回原来的文件夹即可。


在客户机上验证,分别打开www.benet.com和[url=http://www.accp.com]www.accp.com[/url]可以看到以下网页
wKioL1SOsI6wilM8AAC5V19rjXY687.jpg
wKiom1SOr_GTONWFAADFR0rl1Hg766.jpg

3、配置nginx支持php网页
因为之前已经搭建过LAMP平台,只需要重新配置php即可,如果之前没有安装过php,需要安装以下gd库和gd库关联程序
[iyunv@localhost ~]# yum -y install
libjpeg-devel
libpng-devel
freetype-devel
zlib-devel
gettext-devel
libXpm-devel
libxml2-devel
fontconfig-devel
openssl-devel
bzip2-devel

这里我们用源码编译php-5.4.5,找到php源码解压的那个目录进行./configure配置
[iyunv@localhost ~]# umount /opt   #之前我们把php解压到/opt目录下了,后来/opt又被用来挂载所以要先卸载才能看到之前的内容

[iyunv@localhost php-5.4.5]#./configure      #进行功能配置
--prefix=/usr/local/php5
--with-gd
--with-mysql=/usr/local/mysql
--with-config-file-path=/etc
--with-zlib-dir
--with-libxml-dir
--with-freetype-dir
--with-jpeg-dir
--with-png-dir
--with-iconv
--with-openssl
--with-gettext
--enable-mbstring
--enable-gd-native-ttf
--enable-gd-jis-conv
--enable-static
--enable-inline-optimization
--enable-sockets
--enable-soap
--enable-ftp
--disable-ipv6
--enable-fpm   #这个模块就是实现让nginx支持php的

[iyunv@localhost php-5.4.5]#make && make install
注意:因为之前装过php,所以make很可能会报错,可以使用make clean再重新make


[iyunv@localhost php-5.4.5]#cp php.ini-development /etc/php.ini  #第一次装php的话需要这一步,这里我们不需要
[iyunv@localhost php-5.4.5]#ln -s /usr/local/php5/bin/* /usr/local/bin/  #建立连接直接调用命令
[iyunv@localhost php-5.4.5]#ln -s /usr/local/php5/sbin/* /usr/local/sbin/

tar xzvf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz   #为了提高php解析效率,安装对应加速器
cd ZendGuardLoader-php-5.3-linux-glibc23-i386/php-5.3.x
cp ZendGuardLoader.so /usr/local/php5/lib/php

[iyunv@localhost ~]#vim /etc/php.ini  #在ini配置文件末尾加入以下三行内容使php能识别加速器
[Zend Guard Loader]
zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so
zend_loader.enable=1

[iyunv@localhost ~]#cd /usr/local/php5/etc/
[iyunv@localhost etc]#cp  php-fpm.conf.default php-fpm.conf
[iyunv@localhost etc]#vim php-fpm.conf   #配置php-fpm,让nginx开启php支持,找到以下几行做相应修改
pid = run/php-fpm.pid
user = nginx
group = nginx
pm.max_children=50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35

[iyunv@localhost etc]#/usr/local/sbin/php-fpm   #执行这个文件

netstat -tnapl | grep 9000   #fpm进程默认端口是9000,查看是否开启

--------以下是让nginx支持PHP功能--------
[iyunv@localhost ~]#vim /usr/local/nginx/conf/nginx.conf

location ~ .php$ {            #配置文件中加入一个location块
            root           /var/www/benet;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }


[iyunv@localhost ~]#vim /var/www/benet/index.php  #在网页根目录下新建一个php网页作为测试,输入以下三行内容
phpinfo();
?>

在客户机上的浏览器中输入http://192.168.5.2/index.php,出现以下页面则实验成功!
wKiom1SOtFTDBdJXAAPzJQw0ydw481.jpg


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-38089-1-1.html 上篇帖子: nginx rewrite php的CI(CodeIgniter)框架 下篇帖子: linux 把nginx加入到系统服务,并开机自己启动的方法 网站
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表