ubuntu 10.04 LTS下安装nginx-0.8.54+php-5.3.2+php-fmp+mysql-5.1+phpmyadmin
安装前准备:
选在最快的源并更新源
sudo apt-get update
更新系统补丁
sudo apt-get dist-upgrade
安装LNMP用的程序
//需要用到的
apt-get install htop binutils cpp flex gcc libarchive-zip-perl libc6-dev libcompress-zlib-perl m4 libpcre3 libpopt-dev lynx make perl perl-modules openssl unzip zip autoconf2.13 gnu-standards automake libtool bison zlib1g-dev ntp ntpdate autotools-dev g++ bc subversion psmisc
//php用到的库
apt-get install libmysqlclient-dev libcurl4-openssl-dev libjpeg62-dev libpng3-dev libxpm-dev libfreetype6-dev libt1-dev libmcrypt-dev libxslt1-dev libbz2-dev libxml2-dev libevent-dev libltdl-dev libmagickwand-dev imagemagick
//nginx用到的库
apt-get install build-essential libpcre3-dev libssl-dev libgd2-xpm-dev libgeoip-dev
1.安装nginx
下载 0.8.54稳定版
tar xvzf nginx-0.8.
下载 upstream fair 模块。upstream fair 是比内建的负载均衡更加智能的负载均衡模块。它采用的不是内建负载均衡使用的轮换的均衡算法,而是可以根据页面大小、加载时间长短智能的进行负载均衡。
wget www.linuxidc/system/systembak/nginx/
解压
tar xvzf
进入nginx解压目录配置nginx
cd /nginx-0.8.54
配置
./configure --conf-path=/etc/f \--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
-
-with-debug \
--with-http_stub_status_module \
--with-http_flv_module \
--with-http_ssl_module \
--with-http_dav_module \
--with-http_gzip_static_module \
--with-mail \
--with-mail_ssl_module \
--with-ipv6 \
--with-http_realip_module \
--with-http_geoip_module \
-
-with-http_xslt_module \
--with-http_image_filter_module \
--with-sha1=/usr/include/openssl \
--with-md5=/usr/include/openssl \
--add-module=/gnosek-nginx-upstream-fair-2131c73
注意:--add-module路径为gnosek-nginx-upstream-fair-2131c73的解压路径以便在安装的时候将此模块编译进去
编译并安装
make && make install
注意:/var/lib/nginx/目录 需要手工建立,否则启动 nginx 会报错:[emerg]: mkdir() "/var/lib/nginx/body" failed (2: No such file or directory)
建立shell 脚本 /etc/init.d/nginx
------------------------------------------------------------------------------------
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start
-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
--------------------------------------------------------
然后执行命令更新RC
sudo update-rc.d -f nginx defaults
最后启动:
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart
注意:网站的目录在/usr/local/nginx/html下
要使用upstream fair,只要在 nginx 配置文件的 upstream 段加入 fair 开关即可:
upstream backend {
server server1;
server server2;
fair;
}
现在可以在浏览器以IP测试下nginx。
2.安装php和php-fmp用ubuntu自带的源免去经常因配置而失败。
网上搜索 大部分都是讲怎么编译安装的, 自己也搜索过ubuntu的源, 没有单独的php5-fpm的安装包, 不过按照一个老外的安装方法, 还是可以不用编译的。
aptitude install python-software-properties
add-apt-repository ppa:brianmercer/php
apt-get update
apt-get install php5-fpm php5-cli php5-cgi php5-dev //目前就安装这几个
安装好后, 配置一下 /etc/php5/f
pm.max_children 设置大一点, 默认是10, 这是php5-fpm的进程数。
然后就是 service php5-fpm start 启动。
php5-fpm是可以平滑重启的,修改了php.ini文件后, 用service php5-fpm reload 就能重新加载配置文件。
然后配置f让它支持php,以下是我的配置文件
----------------------------------------------------------------------------------------
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#err
or_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include pes;
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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/nginx/html;
index index.php index.html index.htm;
}
## Images and static content is treated different
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
access_log off;
expires 30d;
root /usr/local/nginx/html;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
mysql下载starting the servererror_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass 127.0.0.1;
#}
# 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 /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
## Parse all .php file in the /var/www/nginx-default directory
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
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;
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_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
----------------------------------------------------------------------------------------
3.安装mysql,也用自带的源
aptitude install mysql-server mysql-client
安装的过程中根据提示输入密码
测试下 mysql -uroot -proot
然后安装php的mysql模块下面是我从网上拷贝来的一并装了。
apt-get install php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
安装完以上模块我们需要重启php5-fpm
/etc/init.d/php5-fpm restart
可能会提示Starting php5-fpm PHP Deprecated: Comments starting with '#' are deprecated in /etc/php5/fpm/conf.d/idn.ini on line 1 in Unknown on line 0错误
我们将#号替换成;就可以了。
然后再重启。
忘了一点我们需要将php的cgi.fix_pathinfo=1打开,去掉前面的;号。
nano /etc/php5/cgi/php.ini
这时我们建个php文件 输入phpinfo();测试以下。
4.安装phpmyadmin
sudo apt-get install phpmyadmin
phpmyadmin默认安装在/usr/share/phpmyadmin/下,我们需要做个软链接或者直接拷贝到网站的目录下
sudo ln -s /usr/share/phpmyadmin/ /usr/local/nginx/html/
然后根据自己的ip就可以访问了,由于我的VM虚拟机没有做桥接192.168.52.129/phpmyadmin/
5.其他技巧
要是我在nginx下用zend framework或者ThinkPHP的话就需要去掉inde.php需要url重写配置,以及obey漏洞修补。
这里我以ThinkPHP为例
首先上传项目,将url_model设置为2,然后修改f配置文件
location /xuanke {
index index.php;
if (!-e $request_filename) {
rewrite ^/xuanke/(.*)$ /xuanke/index.php/$1 last;
break;
}
}
## Parse all .php file in the /var/www/nginx-default directory
location ~ \.php($|/) {
root /usr/local/nginx/html;
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+?\.php)(/.+)$") {
set $script $1;
set $path_info $2;
}
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$script;
fastcgi_param SCRIPT_NAME $script;
f
astcgi_param PATH_INFO $path_info;
}
之前的我直接注释掉了,这个是我从另一个虚拟机拷贝过来的,相应的路径做下修改。
接下来修补漏洞,用张晏的方法。
在fastcgi_params配置文件下加入以下判断
if ($request_filename ~* (.*)\.php) {
set $php_url $1;
}
if (!-e $php_url.php) {
return 403;
}
--------------------------------------------------------------------------------------
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_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 SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
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;
if ($request_filename ~* (.*)\.php) {
set $php_url $1;
}
if (!-e $php_url.php) {
return 403;
}
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
----------------------------------------------------------------------------------------------------
6.接着顺便说一下nginx的升级
我之前的是nginx版本是0.7.6,升级需要用到pcre库。
wget /download/nginx-0.8.
tar -zxvf nginx-0.8.
pcre下载
sourceforge/projects/pcre/files/
个最新的下载下来
tar -zxvf pcre-8.
cd nginx-0.8.53
./configure --sbin-path=/usr/sbin --with-http_ssl_module --with-pcre=/pcre-8.11
注:nginx bin 升級前的位置在 /usr/sbin 下
make
/etc/init.d/nginx stop #停止nginx安裝到新的版本
make install #安裝nginx
复制nginx编辑档到新的位置
cp /etc/f /usr/local/nginx/f
cd ../
nginx -v
若出现讯息:nginx version: nginx/0.8.53
表示OK
最后重启nginx
# /etc/init.d/nginx start
有错了....80端口占用了
ipv6也是[::]:80之前可以现在不可以
直接注释掉
# /etc/init.d/nginx start
ok~
以上仅是个人经验,不代表其他机器适用。
总之我是经过多时失败成功了,祝你好运!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论