Nginx是一個小巧而高效的Linux下的Web服務器軟件,是由 Igor Sysoev 為俄羅斯訪問量第二的 Rambler 站點開發的, 已經在一些俄羅斯的大型網站上運行多年,相當的穩定。 Nginx性能穩定、功能豐富、運維簡單、處理靜態文件速度快且消耗系統資源極少。
各種有點不說了!!!
閒言:
最近一直時間不太多,一直在學習路由技術,誰知道一個朋友喊我幫忙做下ras,做完了就想起了 nginx,於是這篇文章誕生了,在這裡我采用的是CentOS 6.0。安裝時是默認的minimal安裝,不帶有任何非必要軟件包的! 裝完不到1G!
至於操作系統的安裝,圖形化肯定挑戰不了大家的智商,至於文本安裝,辰逸的博客裡有一篇redhat5 文本安裝全過程~這裡就省略掉了!
安裝前准備:
1:配置好IP dns 網關,確保xshell等工具可以連上去~咳 咳!xshell是個好東西,使用linux就應該要會用xshell(個人見解,如需下載,可在本博客搜索!)
2:關閉 SElinux
vi /etc/selinux/config 把SELINUX=enforcing 注釋掉 添加一行為 SELINUX=disabled
3:關閉 Iptables(不建議)
可為iptables 添加策略 允許80端口和3306端口! 相關知識可以在俺博客裡參考一番!
安裝所需軟件包!!!有點小多。呵呵呵!
pcre-8.20.tar.gz (支持nginx偽靜態)
nginx- 1.0.6.tar.gz (目前的穩定版本)
mysql-5.1.59.tar.gz
php-5.2.17.tar.gz
php-5.2.17-fpm- 0.5.14.diff.gz (支持PHP FastCGI模式)
ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
libmcrypt- 2.5.7.tar.gz
下面的有好多好多需要安裝的軟件包(前面有提過,我用的mini安裝,所以很多包需要手動打,個人 感覺除了麻煩了一點,其實挺不錯的)
kernel-headers-2.6.32-71.el6.i686.rpm
compat-glibc-headers- 2.5-46.2.i686.rpm
compat-glibc-2.5-46.2.i686.rpm
compat-libstdc++-33-3.2.3- 69.el6.i686.rpm
compat-libf2c-34-3.4.6-19.el6.i686.rpm
compat-libgcc-296-2.96- 144.el6.i686.rpm
mpfr-2.4.1-6.el6.i686.rpm
cpp-4.4.4-13.el6.i686.rpm
glibc-headers-2.12- 1.7.el6.i686.rpm
glibc-devel-2.12-1.7.el6.i686.rpm
libgomp-4.4.4- 13.el6.i686.rpm
libstdc++-devel-4.4.4-13.el6.i686.rpm
ppl-0.10.2-11.el6.i686.rpm
cloog- ppl-0.15.7-1.2.el6.i686
gcc-4.4.4-13.el6.i686.rpm
compat-gcc-34-3.4.6- 19.el6.i686.rpm
compat-gcc-34-c++-3.4.6-19.el6.i686.rpm
make-3.81- 19.el6.i686.rpm
keyutils-libs-devel-1.4-1.el6.i686.rpm
libcom_err-devel-1.41.12- 3.el6.i686.rpm
libsepol-devel-2.0.41-3.el6.i686.rpm
libselinux-devel-2.0.94- 2.el6.i686.rpm
krb5-devel-1.8.2-3.el6.i686.rpm
zlib-devel-1.2.3-25.el6.i686.rpm
openssl- devel-1.0.0-4.el6.i686.rpm
libXpm-3.5.8-2.el6.i686.rpm
gd-2.0.35-10.el6.i686.rpm
freetype -2.3.11-5.el6.i686.rpm
libxml2-2.7.6-1.el6.i686.rpm
libxml2-devel-2.7.6- 1.el6.i686.rpm
curl-7.19.7-16.el6.i686.rpm
libjpeg-devel-6b-46.el6.i686.rpm
libjpeg-6b- 46.el6.i686.rpm
libjpeg-static-6b-46.el6.i686.rpm
libpng-1.2.44-1.el6.i686.rpm
libpng- devel-1.2.44-1.el6.i686.rpm
libpng-static-1.2.44-1.el6.i686.rpm
php-common-5.3.2- 6.el6.i686.rpm
php-gd-5.3.2-6.el6.i686.rpm
gcc-c++-4.4.4-13.el6.i686.rpm
ncurses-devel- 5.7-3.20090208.el6.i686.rpm
ncurses-term-5.7-3.20090208.el6.i686.rpm
ncurses-static-5.7- 3.20090208.el6.i686.rpm
perl-Pod-Escapes-1.04-115.el6.i686.rpm
perl-libs-5.10.1- 115.el6.i686.rpm
perl-version-0.77-115.el6.i686.rpm
perl-Module-Pluggable-3.90- 115.el6.i686.rpm
perl-Pod-Simple-3.13-115.el6.i686.rpm
perl-5.10.1- 115.el6.i686.rpm
autoconf213-2.13-20.1.el6.noarch.rpm
autoconf-2.63- 5.1.el6.noarch.rpm
automake-1.11.1-1.2.el6.noarch.rpm
automake14-1.4p6- 19.2.el6.noarch.rpm
automake16-1.6.3-18.el6.1.noarch.rpm
libtool-ltdl-2.2.6- 15.5.el6.i686.rpm
libtool-ltdl-devel-2.2.6-15.5.el6.i686.rpm
libtool-2.2.6- 15.5.el6.i686.rpm
patch-2.6-6.el6.i686.rpm
把這些軟件包啊用xshell自帶的文件傳輸功能傳輸 或者 flashfxp 的sftp傳輸過去!再或者你可以掛載鏡像~~~
建議裝上vim 如果你和我環境一樣的話,呵呵
——— —————————————————邪惡的分割線————————————————————
正式開始安裝咯 ~~
下面均是采用xshell操作~
按照順序依次安裝,如果你嫌麻煩可直接復制以下命令(如果你不嫌麻煩.... 呵呵呵)
rpm -ivh kernel-headers-2.6.32-71.el6.i686.rpm rpm -ivh compat-glibc-headers-2.5-46.2.i686.rpm rpm -ivh compat-glibc-2.5-46.2.i686.rpm rpm -ivh compat-libstdc++-33-3.2.3-69.el6.i686.rpm rpm -ivh compat-libf2c-34-3.4.6-19.el6.i686.rpm rpm -ivh compat-libgcc-296-2.96-144.el6.i686.rpm rpm -ivh mpfr-2.4.1-6.el6.i686.rpm rpm -ivh cpp-4.4.4-13.el6.i686.rpm rpm -ivh glibc-headers-2.12-1.7.el6.i686.rpm rpm -ivh glibc-devel-2.12-1.7.el6.i686.rpm rpm -ivh libgomp-4.4.4-13.el6.i686.rpm rpm -ivh libstdc++-devel-4.4.4-13.el6.i686.rpm rpm -ivh ppl-0.10.2-11.el6.i686.rpm rpm -ivh cloog-ppl-0.15.7-1.2.el6.i686.rpm rpm -ivh gcc-4.4.4-13.el6.i686.rpm rpm -ivh compat-gcc-34-3.4.6-19.el6.i686.rpm rpm -ivh compat-gcc-34-c++-3.4.6-19.el6.i686.rpm rpm -ivh make-3.81-19.el6.i686.rpm rpm -ivh keyutils-libs-devel-1.4-1.el6.i686.rpm rpm -ivh libcom_err-devel-1.41.12-3.el6.i686.rpm rpm -ivh libsepol-devel-2.0.41-3.el6.i686.rpm rpm -ivh libselinux-devel-2.0.94-2.el6.i686.rpm rpm -ivh krb5-devel-1.8.2-3.el6.i686.rpm rpm -ivh zlib-devel-1.2.3-25.el6.i686.rpm rpm -ivh openssl-devel-1.0.0-4.el6.i686.rpm rpm -ivh libXpm-3.5.8-2.el6.i686.rpm rpm -ivh gd-2.0.35-10.el6.i686.rpm rpm -ivh freetype-2.3.11-5.el6.i686.rpm rpm -ivh libxml2-2.7.6-1.el6.i686.rpm rpm -ivh libxml2-devel-2.7.6-1.el6.i686.rpm rpm -ivh curl-7.19.7-16.el6.i686.rpm rpm -ivh libjpeg-devel-6b-46.el6.i686.rpm rpm -ivh libjpeg-6b-46.el6.i686.rpm rpm -ivh libjpeg-static-6b-46.el6.i686.rpm rpm -ivh libpng-1.2.44-1.el6.i686.rpm rpm -ivh libpng-devel-1.2.44-1.el6.i686.rpm rpm -ivh libpng-static-1.2.44-1.el6.i686.rpm rpm -ivh php-common-5.3.2-6.el6.i686.rpm rpm -ivh php-gd-5.3.2-6.el6.i686.rpm rpm -ivh gcc-c++-4.4.4-13.el6.i686.rpm rpm -ivh ncurses-devel-5.7-3.20090208.el6.i686.rpm rpm -ivh ncurses-term-5.7-3.20090208.el6.i686.rpm rpm -ivh ncurses-static-5.7-3.20090208.el6.i686.rpm rpm -ivh --nodeps perl-Pod-Escapes-1.04-115.el6.i686.rpm rpm -ivh --nodeps perl-libs-5.10.1-115.el6.i686.rpm rpm -ivh --nodeps perl-version-0.77-115.el6.i686.rpm rpm -ivh --nodeps perl-Module-Pluggable-3.90-115.el6.i686.rpm rpm -ivh --nodeps perl-Pod-Simple-3.13-115.el6.i686.rpm rpm -ivh --nodeps perl-5.10.1-115.el6.i686.rpm rpm -ivh autoconf213-2.13-20.1.el6.noarch.rpm rpm -ivh autoconf-2.63-5.1.el6.noarch.rpm rpm -ivh automake-1.11.1-1.2.el6.noarch.rpm rpm -ivh automake14-1.4p6-19.2.el6.noarch.rpm rpm -ivh automake16-1.6.3-18.el6.1.noarch.rpm rpm -ivh libtool-ltdl-2.2.6-15.5.el6.i686.rpm rpm -ivh libtool-ltdl-devel-2.2.6-15.5.el6.i686.rpm rpm -ivh libtool-2.2.6-15.5.el6.i686.rpm rpm -ivh patch-2.6-6.el6.i686.rpm
————————————————————邪惡的分割線——————— —————————————————
安裝Mysql
解包mysql 並進行預配置
[root@itchenyi chenyi]# tar -zxf mysql-5.1.59.tar.gz [root@itchenyi chenyi]# cd mysql-5.1.59 [root@itchenyi mysql-5.1.59]# ./configure --prefix=/usr/local/mysql --localstatedir=/usr/local/mysql/data/ --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static --with-unix-socket-path=/tmp/mysql.sock --with-shared --without-debug --with-extra-charsets=all --without-embedded-server --enable-profiling -- enable-assembler --enable-thread-safe-client --with-server-suffix=-community --with-extra- charset=utf8,gbk
編譯 並 安裝
[root@itchenyi mysql-5.1.59]# make && make install
更多精彩:轉到www.itchenyi.com
特別注意:在執行make install編譯的過程中,如果你 看到屏幕突然不動了,卡在那裡,很久都過不去,你一定認為編譯錯誤了,或者服務器鬧情緒了,但是往往需要堅持,耐心 的等待幾分鐘,就會編譯成功!Mysql編譯安裝告訴我們,任何事情,只要堅持下去,終會成功!
對文件夾設置相應 權限|建立配置文件|添加為系統服務
[root@itchenyi mysql-5.1.59]# chown -R root:mysql /usr/local/mysql/ [root@itchenyi mysql-5.1.59]# cd /usr/local/mysql/ [root@itchenyi mysql]# cp share/mysql/my-huge.cnf /etc/my.cnf [root@itchenyi mysql]# cp share/mysql/mysql.server /etc/init.d/mysqld
編輯 /etc/profile 在末行 加入一下命令~
export PATH=$PATH:/usr/local/mysql/bin
下面這兩行把myslq的庫文件鏈接到系統默認 的位置,這樣你在編譯類似PHP等軟件時可以不用指定mysql的庫文件地址。
[root@itchenyi mysql]# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql [root@itchenyi mysql]# ln -s /usr/local/mysql/include/mysql /usr/include/mysql
生成mysql系統數據庫 ~~
[root@itchenyi mysql]# /usr/local/mysql/bin/mysql_install_db --user=mysql &
啟動 mysql | 設置開機自啟並給它來個密碼~~~~
[root@itchenyi mysql]# chkconfig mysqld on [root@itchenyi mysql]# /etc/rc.d/init.d/mysqld start [root@itchenyi mysql]# mysqladmin -u root -p password 'chenyi'
mysql安裝完畢~你可以mysql -u root -p 回車輸入密碼測試一番了~
安裝pcre
解包|預配置
[root@itchenyi chenyi]# tar zxf pcre- 8.20.tar.gz [root@itchenyi chenyi]# mkdir /usr/local/pcre [root@itchenyi chenyi]# cd pcre-8.20 [root@itchenyi pcre-8.20]# ./configure --prefix=/usr/local/pcre/
編譯|安裝
[root@itchenyi pcre-8.20]# make && make install
安裝nginx
解包|預配置
[root@itchenyi chenyi]# tar zxf nginx-1.0.6.tar.gz [root@itchenyi chenyi]# cd nginx-1.0.6 [root@itchenyi nginx-1.0.6]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with- openssl=/usr/ --with-pcre=/usr/local/src/pcre-8.20
注:--with-openssl=/usr/ --with- pcre=/usr/local/src/pcre-8.20 指向的路徑是源碼包解包的路徑,並非其安裝路徑~~~否則會報錯的哦!
編譯|安 裝
[root@itchenyi nginx-1.0.6]# make && make install
啟動,控制權限~
[root@itchenyi nginx-1.0.6]# /usr/local/nginx/sbin/nginx [root@itchenyi nginx-1.0.6]# chown nobody:nobody -R /usr/local/nginx/html/ [root@itchenyi nginx-1.0.6]# chmod 777 -R /usr/local/nginx/html/
設置nginx開機啟動~~
vi /etc/rc.d/init.d/nginx 然後一字不漏的將以下代碼復制進去~~
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/usr/local/nginx/logs/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid } reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
賦權文件~~~|~~~加入自啟
[root@itchenyi nginx-1.0.6]# chmod 755 /etc/rc.d/init.d/nginx [root@itchenyi nginx-1.0.6]# chkconfig nginx on [root@itchenyi /etc/rc.d/init.d/nginx restart 停止 nginx: [失敗] 正在啟動 nginx: [確定]
安裝 libmcrypt| 一條命令完成安裝, 你要是不放心就拆開用~~~~
[root@itchenyi chenyi]tar zxf libmcrypt-2.5.7.tar.gz [root@itchenyi chenyi]cd libmcrypt-2.5.7 [root@itchenyi libmcrypt-2.5.7]# ./configure && make && make install
安裝php 做到這一步 就可以去喝壺茶了~~~~~無奈啊。。。
解包|解包|打補丁|預配置|
[root@itchenyi chenyi]# tar -zxf php-5.2.17.tar.gz [root@itchenyi chenyi]# gunzip php-5.2.17-fpm-0.5.14.diff.gz [root@itchenyi chenyi]# patch -d php-5.2.17 -p1 <php-5.2.17-fpm-0.5.14.diff [root@itchenyi chenyi]# cd php-5.2.17 [root@itchenyi php-5.2.17]# ./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql- sock=/tmp/mysql.sock --with-gd --with-iconv --with-freetype --with-jpeg --with-png --with-zlib --with- libxml --enable-xml --enable-discard-path --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable -shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --enable-ftp --enable-gd-native-ttf --with- openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with- gettext --with-mime-magic --enable-suhosin --enable-session --with-mcrypt
編譯|安裝
[root@itchenyi php-5.2.17]# make && make install
建立配置文件
[root@itchenyi php5]# cd /usr/local/php5/ [root@itchenyi php5]# cp /chenyi/php-5.2.17/php.ini-dist /usr/local/php5/etc/php.ini [root@itchenyi php5]# rm -rf /etc/php.ini [root@itchenyi php5]# ln -s /usr/local/php5/etc/php.ini /etc/php.ini
編輯php-fpm.conf
vi /usr/local/php5/etc/php-fpm.conf
取消下面的注釋 如圖~
啟動php-fpm 設置為開機自啟
[root@itchenyi php5]# /usr/local/php5/sbin/php-fpm start [root@itchenyi php5]# cp /usr/local/php5/sbin/php-fpm /etc/rc.d/init.d/php-fpm 在#!/bin/bash下添加兩行 #chkconfig:345 62 62 #description:php-fpm [root@itchenyi php5]# chmod 755 /etc/rc.d/init.d/php-fpm [root@itchenyi php5]# chkconfig php-fpm on
安裝 Zend optimizer
[root@itchenyi chenyi]# tar zxf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz [root@itchenyi chenyi]# cp /chenyi/ZendOptimizer-3.3.9-linux-glibc23-i386/data/5_2_x_comp/ZendOptimizer.so /usr/local/zend [root@itchenyi chenyi]# rm -rf /chenyi/ZendOptimizer-3.3.9-linux-glibc23-i386
配置Zend optimizer
在末行加入一下內容
[Zend Optimizer] zend_optimizer.optimization_level=15 zend_extension="/usr/local/zend/ZendOptimizer.so"
重啟web服務器
[root@itchenyi chenyi]# service nginx restart
停止 nginx: [確定]
正在啟動 nginx: [確定]
[root@itchenyi chenyi]# service php-fpm restart
Shutting down php_fpm . done
Starting php_fpm done
配置nginx支持 php
代碼如下
location / { root html; index index.php index.html index.htm; #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ .php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; } }
完成配置後重新啟動~~~
[root@itchenyi chenyi]# service nginx restart
停止 nginx: [確定]
正在啟動 nginx: [確定]
[root@itchenyi chenyi]# service php-fpm restart
Shutting down php_fpm . done
Starting php_fpm done
現在測試網站~~~~傳個php探針上去一試便知~~~
[root@itchenyi html]# mv /root/tz.php ./ [root@itchenyi html]# chown nobody:nobody -R /usr/local/nginx/html/ [root@itchenyi html]# chmod 777 -R /usr/local/nginx/html/
各種支持~~~
如果你的php無法跟nginx相愛那麼請檢查路徑的設置~~~~