前言
這陣子上網溜達,看見很多高手都說nginx(陰精X,嘿嘿)很好、很強大,於是乎,我沒能頂得住誘惑,便裝了個玩玩,為了方便再次安裝,便小記了下。本文安裝的方法僅提供給「玩票」的兄弟姐妹們參考,工業環境請慎重!我這人計算機水平也不高,文中如有不正之處,請指正cmyf DOT name AT gmail DOT com.
零,準備工作
不知誰說過不打無準備之戰來著,咱也要先把需要的東西都準備好。LEMP不外乎就是:Linux、nginx、MySQL、PHP。我使用的版本信息如下:
Linux: Debian etch 2.6.18-5/gcc version 4.1.2/GNU Make 3.81/
Engine X: nginx-0.5.35
MySQL: mysql-5.0.45
PHP: php-5.2.5
由於是源碼安裝,所以在安裝的過程中難免會遇到缺少xxx文件啊、xxx庫之類的信息,所以我在安裝的時候還用到了以下軟體:
freetype-2.3.5 libxml2-2.6.30 gd-2.0.35 lighttpd-1.4.19 jpegsrc.v6b mcrypt-2.6.6 pcre-7.2
libiconv-1.11 memcache-2.2.1 libmcrypt-2.5.8 mhash-0.9.9 libpng-1.2.20 libxml2-2.6.30 ncurses-5.6
當然了,由於我的debian linux系統是全新安裝的基本系統,所以缺的包還比較多,你得根據實際情況添加,大部分的都能apt-get到,實在get不到,就上網搜一個,裝上就歐了。將所有需要的軟體我都全部放到了/home/alpha/lemp目錄中,方便點!
好了,彈夾已經壓好了,喝口茶,上膛,瞄準!
一、安裝MySQL。
#cd /home/alpha/lemp
#tar zxvf mysql-5.0.45.tar.gz
#/sbin/groupadd mysql
#/sbin/useradd msyql
#cd mysql-5.0.45
# ./configure --prefix=/opt/webser/mysql/ --without-debug --with-unix-socket-path=/tmp/mysql.sock --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --with-extra-charsets=utf8,gb2312,gbk --with-pthread –enable-thread-safe-client
詳細的configure信息可以參考:
http://dev.mysql.com/doc/refman/5.0/en/configure-options.html
#make && make install
安裝mysql是個漫長的過程,需要點點時間,我測試用的機器還湊合著。再有,我所有的軟體都是安裝在/opt/webser/目錄下,你可以根據實際情況選擇。
#chmod +w /opt/webser/mysql
#chown -R mysql:mysql /opt/webser/mysql
#cp support-files/my-medium.cnf /opt/webser/mysql/my.cnf
#cd ../
#/opt/webser/mysql/bin/mysql_install_db --defaults-file=/opt/webser/mysql/my.cnf --basedir=/opt/webser/mysql/ --datadir=/opt/webser/mysql/data --user=mysql --pid-file=/opt/webser/mysql.pid --skip-locking --port=3306 –socket=/tmp/mysql.sock
關於mysql_install_db的參數,可以參考下面這篇文章:
http://dev.mysql.com/doc/refman/5.0/en/mysql-install-db.html
#/bin/sh /opt/webser/mysql/bin/mysqld_safe --defaults-file=/opt/webser/mysql/my.cnf &
如果能順利啟動,那就ok了,剩下的關於mysql更多的配置,就參考mysql的手冊吧。
二、PHP安裝(FastCGI)
#tar zxvf php-5.2.5.tar.gz
#cd php-5.2.5/
#./configure --prefix=/opt/webser/php --enable-fastcgi --enable-discard-path --enable-force-cgi-redirect --with-config-file-path=/opt/webser/php/conf --enable-zend-multibyte --with-mysql=/opt/webser/mysql/ --with-libxml-dir=/usr/local/bin/xml2-config --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-sysvsem --enable-inline-optimization --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif –disable-debug
我在configure的時候由於缺少很多文件,所以遇到很多鬱悶問題,在config的時候一定不要忘了—enable-fastcgi,更多的configure參數,請參考:
http://php.net/manual/zh/configure.php
#make && make install
#cd ../
接下來為了讓php更爽,還要添加點有意思的插件:
memcache這玩意兒還是有點用途的,呵呵,反正測試用裝上玩唄!
#tar zxvf memcache-2.2.1.tgz
#cd memcache-2.2.1
#/opt/webser/php/bin/phpize
#./configure –with-php-config=/opt/webser/php/bin/php-config
#make && make install
#cd ../
gd呢是肯定用的上的!
#cd php-5.2.5/ext/gd/
#/usr/local/webserver/php/bin/phpize
#./configure --with-jpeg-dir --with-png-dir --with-zlib-dir --with-ttf --with-freetype-dir –with-php-config=/usr/local/webserver/php/bin/php-config
#make
#cd ../../../
接下來需要稍微修改以下php.ini文件:
查找並替換extension_dir為:
extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"
具體的目錄得根據你實際情況而定,然後增加以下內容:
extension = "memcache.so"
extension = "gd.so"
三,安裝fastcgi
看了下網上兄弟們的文章,lighttpd裡面的spawn-fcgi可用,那就編譯一下 lighttpd-1.4.19,不用安裝,然後按照以下方法設置:
#cp spawn-fcgi /opt/webser/php/bin/
#chmod +x /opt/webser/php/bin/spawn-fcgi
運行fastcgi:
#/opt/webser/php/bin/spawn-fcgi -a 127.0.0.1 -p 8080 -C 64 -u www -f /opt/webser/php/bin/php-cgi
關於spawn-fcgi的運行參數,可以查看:
http://linux.die.net/man/1/spawn-fcgi
四、安裝nginx
#/usr/sbin/groupadd www -g 68
#/usr/sbin/useradd -u 68 -g www www
#mkdir -p /home/data/www
#chmod +w /home/data/www
#chown -R www:www /home/data/www
在測試過程中,服務目錄是/home/data/www,挺別緻的吧,呵呵,
#tar zxvf pcre-7.2.tar.gz
#tar zxvf nginx-0.5.35.tar.gz
#cd nginx-0.5.35
#./configure --user=www --group=www --prefix=/opt/webser/nginx --with-pcre=/home/alpha/nginx-php/pcre-7.2 --with-http_stub_status_module
#make && make install
更多的關於nginx的configure參數可以查看:
http://wiki.codemongers.com/NginxChsInstall
五,nginx配置
創建了個保存日誌的目錄,方便點,呵呵:
#mkdir -p /home/data/logs
#chmod +w /home/data/logs
#chown -R www:www /home/data/logs
接著咱就來配置配置:
#cp /opt/webser/nginx/conf/nginx.conf /opt/webser/nginx/conf/nginx.conf.BACKUP
我的配置文件如下,
#vi /opt/webser/nginx/conf/nginx.conf
user www www;
worker_processes 5;
error_log /home/data/logs/error.log;
events {
worker_connections 2048;
}
http {
include conf/mime.types;
include conf/fcgi.conf;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /home/data/logs/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name example.com;
root /home/data/www;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:8080;
}
}
}
接下來需要在/opt/webser/nginx/conf/中創建一個fcgi.conf文件,
#vi /opt/webser/nginx/conf/fcgi.conf
內容如下:
#fastcgi.conf
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
我這就是簡單配置,更多的關於nginx的配置信息請參考:
http://wiki.codemongers.com/NginxConfiguration
六、啟動並測試nginx.
#/opt/webser/nginx/sbin/nginx -c /opt/webser/nginx/conf/nginx.conf
寫個phpinfo()文件測試下,能正常顯示,那就ok了。
七、參考資料
我已經在文章中註明了參考過的資料了,嘿嘿!
八、後記
LEMP到此就安裝完成了,nginx是很強大的,要想讓它更穩定、高效的為您服務,還是多看看它的官方文檔吧。歡迎指正:cmyf DOT name AT gmail DOT com
[火星人 ] LEMP安裝備忘錄已經有596次圍觀