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 //把軟件包庫文件的非標準路徑寫入標
準路徑
編譯
[root@localhost libevent-2.0.16-stable]# make
安裝
[root@localhost libevent-2.0.16-stable]# make install
6)查看加載模塊
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
10)測試nginx結果如下
11)基於物理目錄的虛擬主機
[root@localhost ~]# cd /usr/html
[root@localhost html]# mkdir xht
[root@localhost html]# cd xht
[root@localhost xht]# echo "hello xht" >index.html
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
重啓服務
[root@localhost xht1]# pkill -1 nginx
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
[root@localhost var]# pkill nginx
[root@localhost var]# nginx
測試
設置測試主機上的host文件
14)基於端口的虛擬主機
[root@localhost var]# vim /etc/nginx/nginx.conf
重啓nginx服務
[root@localhost var]# pkill nginx
[root@localhost var]# nginx
測試
15)基於主機名的虛擬主機
[root@localhost var]# vim /etc/nginx/nginx.conf
[root@localhost var]# pkill nginx
[root@localhost var]# nginx
測試
測試機上的host文件修改如下
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
[root@localhost php-5.3.7]# vim /etc/nginx/nginx.conf
測試nginx調用php
[root@localhost ~]# vim /xht2/index.php
測試結果
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
測試結果
18)lnmp環境搭建成功。