lnmp环境的实现

1.nginx 简介

     Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的。将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

     nginx可以做为HTTP服务器处理静态文件,索引文件以及自动索引;打开文件描述符缓冲.无缓存的反向代理加速,简单的负载均衡和容错.支持SSL 和 TLSSNI.

     Nginx专为性能优化 而开发,性能是其最重要的考量,实现上非常注重效率 。它支持内核Poll模型,能经受高负载的考验,有报告表明能支持高达 50,000个并发连接数。

     Nginx相较于Apache、lighttpd具有占有内存少,稳定性高等优势,并且依靠并发能力强,丰富的模块库以及友好灵活的配置而闻名。 在Linux操作系统下,nginx使用epoll事件模型,得益于此,nginx在Linux操作系统下效率相当高。同时Nginx在OpenBSD或FreeBSD操作系统上采用类似于epoll的高效事件模型kqueue

2.nginx的案例配置和lnmp环境搭建

1)为了方便的安装软件配置本地yum

[root@localhost ~]# mkdir /mnt/cdrom
[root@localhost ~]# mount /dev/cdrom  /mnt/cdrom //挂载光盘

[root@localhost ~]# vim /etc/yum.repos.d/rhel-debuginfo.repo

文件的配置如下:

[rhel-server]
name=Red Hat Enterprise Linux  server
baseurl=file:///mnt/cdrom/Server
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-redhat-release

2)安装pcre,pcre提供地址重定向功能

[root@localhost ~]# cd /mnt/cdrom/Server

[root@localhost Server]# yum list all |grep pcre //安装pcre
This system is not registered with RHN.
RHN support will be disabled.
pcre.i386                                 6.6-2.el5_1.7              installed 
pcre-devel.i386                        6.6-2.el5_1.7              rhel-server

3)安装pcre-devel

[root@localhost Server]# yum install pcre-devel  

4)解压libevent压缩文件libevent 能够提高网站的性能

[root@localhost ~]# tar -zxvf libevent-2.0.16-stable.tar.gz  -C /usr/local/src   //解压

[root@localhost ~]# cd /usr/local/src
[root@localhost src]# ll
总计 8
drwxr-xr-x 8 user user 4096 2011-11-19 libevent-2.0.16-stable
[root@localhost src]# cd libevent-2.0.16-stable/

5)编译并安装libevent

[root@localhost libevent-2.0.16-stable]# ./configure          //检测预编译环境

[root@localhost libevent-2.0.16-stable]# cd /usr/local/

[root@localhost local]# vim /etc/ld.so.conf.d/libevent.conf        //把软件包库文件的非标准路径写入标

准路径

1

编译

[root@localhost libevent-2.0.16-stable]# make

安装

[root@localhost libevent-2.0.16-stable]# make install

6)查看加载模块

2

7)解压nginx

[root@localhost ~]# tar -zxvf nginx-1.0.11.tar.gz  -C /usr/local/src

[root@localhost ~]# cd /usr/local/src

[root@localhost src]# cd nginx-1.0.11/

8)创建nginx的系统账户

[root@localhost ~]# groupadd -r nginx
[root@localhost ~]# useradd -r -g  nginx -s /bin/false -M nginx

9)检测预编译环境

[root@localhost nginx-1.0.11]# ./configure \ 
  --prefix=/usr   --sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \ 
  --error-log-path=/var/log/nginx/error.log \ 
  --http-log-path=/var/log/nginx/access.log \ 
  --pid-path=/var/run/nginx/nginx.pid \ 
  --lock-path=/var/lock/nginx.lock \ 
  --user=nginx \ 
  --group=nginx \ 
  --with-http_ssl_module \ 
  --with-http_flv_module \ 
  --with-http_stub_status_module \ 
  --with-http_gzip_static_module \ 
  --http-client-body-temp-path=/var/tmp/nginx/client/ \ 
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ 

  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ 
  --with-pcre

编译

[root@localhost nginx-1.0.11]# make

安装

[root@localhost nginx-1.0.11]# make install

[root@localhost ~]# mkdir -pv /var/tmp/nginx

 

 

[root@localhost ~]# nginx

[root@localhost ~]# netstat -tupln |less

1

10)测试nginx结果如下

2

11)基于物理目录的虚拟主机

[root@localhost ~]# cd /usr/html

[root@localhost html]# mkdir xht
[root@localhost html]# cd xht

[root@localhost xht]# echo "hello xht" >index.html

1

12)基于虚拟目录虚拟主机

[root@localhost ~]# mkdir /xht1
[root@localhost ~]# cd /xht1
[root@localhost xht1]# echo "hello xht2" >index.html

[root@localhost xht1]# vim /etc/nginx/nginx.conf

2

重启服务

[root@localhost xht1]# pkill -1  nginx

3

13)基于ip的虚拟主机

[root@localhost ~]# ifconfig eth0:1 192.168.1.11 netmask 255.255.255.0

[root@localhost ~]# mkdir /xht2
[root@localhost ~]# mkdir /xht3

[root@localhost ~]# echo "technology" >/xht2/index.html
[root@localhost ~]# echo "maket" >/xht3/index.html

[root@localhost xht1]# vim /etc/nginx/nginx.conf

4 

[root@localhost var]# pkill nginx

[root@localhost var]# nginx

测试

设置测试主机上的host文件

5

6

7

14)基于端口的虚拟主机

[root@localhost var]# vim /etc/nginx/nginx.conf

3

重启nginx服务
[root@localhost var]# pkill nginx
[root@localhost var]# nginx

测试

4

5

15)基于主机名的虚拟主机

[root@localhost var]# vim /etc/nginx/nginx.conf

2
[root@localhost var]# pkill nginx
[root@localhost var]# nginx

测试

测试机上的host文件修改如下

1

3

4

16)源码安装php

解压源文件

[root@localhost ~]# tar -jxvf php-5.3.7.tar.bz2   -C /usr/local/src

[root@localhost ~]# cd /usr/local/src

[root@localhost src]# cd php-5.3.7/

检测预编译环境

[root@localhost php-5.3.7]# ./configure  --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml  --with-mhash --with-config-file-path=/etc/php --with-config-file-scan-dir=/etc/php --with-bz2 --with-curl

编译

[root@localhost php-5.3.7]#make

安装

[root@localhost php-5.3.7]# make install

为php提供配置文件

[root@lyt php-5.3.7]# cp php.ini-production        /usr/local/php/etc/php.ini

为php-fpm提供配置文件

[root@localhost php-5.3.7]# cp /usr/local/php/etc/php-fpm.conf.default
/usr/local/php/etc/php-fpm.conf 

[root@localhost php-5.3.7]# cd /usr/local/php/etc/ 

[root@localhost etc]# vim php-fpm.conf  // 编辑该文件

pm.max_children = 50

pm.start_servers = 10

pm.min_spare_servers = 3

pm.max_spare_servers = 8

[root@localhost etc]# /usr/local/php/sbin/php-fpm &  //启动fastcgi 

整合nginx和php

[root@localhost php-5.3.7]# vim /etc/nginx/fastcgi_params

 

4

[root@localhost php-5.3.7]# vim /etc/nginx/nginx.conf

5 

测试nginx调用php

[root@localhost ~]# vim /xht2/index.php

4

测试结果

1

17)安装mysql

解压软件包

[root@localhost ~]# tar -zxvf mysql-5.5.15-linux2.6-i686.tar.gz  -C /usr/local

创建mysql组
[root@localhost ~]# groupadd mysql

创建mysql用户并属于mysql组
[root@localhost ~]# useradd -r -g mysql mysql
[root@localhost ~]# cd /usr/local

创建软连接
[root@localhost local]# ln -s mysql-5.5.15-linux2.6-i686/ mysql
[root@localhost local]# chown -R mysql .
[root@localhost local]# chgrp -R mysql .
[root@localhost local]# cd mysql

[root@localhost mysql]# scripts/mysql_install_db --user=mysql
[root@localhost mysql]# chown -R root .
[root@localhost mysql]# chown -R mysql data

初始化mysql
[root@localhost mysql]# bin/mysqld_safe --user=mysql &

用service管理mysql
[root@localhost mysql]# cp support-files/mysql.server  /etc/init.d/mysqld

把mysql的库加入开机启动
[root@localhost mysql]# cd /etc/ld.so.conf.d
[root@localhost ld.so.conf.d]# vim mysql.conf
/usr/locallmysql/lib
[root@localhost include]# ln -s /usr/local/mysql/include mysql

启动mysql
[root@localhost include]# service  mysqld start
Starting MySQL                                             [确定]

测试php调用mysql

[root@localhost xht2]# vim /xht2/index.php

3

测试结果

2

18)lnmp环境搭建成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章