2013-11-13 08:47:01
来 源
itjs.cn
Nginx安装配置
本文介绍ubuntu系统中Nginx服务器的安装和配置,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。
之所以用nginx没用apache,是因为nginx的效率更高一些,尤其是对一些低配置的服务器

1、安装ubuntu server 10.04或10.10,其中安装语言选的en,时区chongqing,服务只安装ssh,其他全部用默认就行。

提示:以上安装过程完成后,建议用其他计算机登录服务器,windows系统可以用putty,linux系统直接在终端用命令就可以:

ssh 登录名@服务器ip

因为以下过程得输入大量命令和代码,在客户机上直接粘贴即可(在windows下的putty中单击右键即可把剪贴板中的内容粘贴到终端)。

2、添加源:

sudo vi /etc/apt/sources.list

lucid(10.04)的源添加如下:

deb http://archive.ubuntu.com/ubuntu/ lucid main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ lucid-security main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ lucid-updates main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ lucid-backports main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ lucid main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ lucid-security main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ lucid-updates main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ lucid-backports main restricted universe multiverse

maverick(10.10)的源:

deb http://archive.ubuntu.com/ubuntu/ maverick main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ maverick-security main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ maverick-updates main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ maverick-proposed main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ maverick-backports main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ maverick main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ maverick-security main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ maverick-updates main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ maverick-proposed main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu/ maverick-backports main restricted universe multiverse

3、更新

sudo apt-get update

4、安装网站系统

sudo apt-get install mysql-server mysql-client nginx php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-common php5-ming php5-pspell php5-recode php5-snmp php5-tidy php5-xmlrpc php5-sqlite php5-xsl spawn-fcgi phpmyadmin

5、设置开机启动fastcgi进程

sudo vi /etc/rc.local

在exit 0这一行前面添加下一行

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -C 4 -P /var/run/fastcgi-php.pid

6、创建网站、日志目录及phpmyadmin的链接(我的网站个人目录是ubuntu,以下命令用你的个人目录替换其中的ubuntu即可)

mkdir /home/ubuntu/www

mkdir /home/ubuntu/log

ln -s /usr/share/phpmyadmin /home/ubuntu/www/phpmyadmin

注:源安装phpmyadmin后,默认安装在/usr/share/phpmyadmin/目录中,得做一个链接到网站目录。

7、配置nginx

sudo vi /etc/nginx/nginx.conf user www-data;

worker_processes 2;

error_log /dev/null crit;

pid /var/run/nginx.pid;

events {

  worker_connections 3000; http {

  autoindex on;

  autoindex_exact_size off;

  autoindex_localtime on;

  include /etc/nginx/mime.types;

  default_type application/octet-stream;

  sendfile on;

  tcp_nopush on;

  tcp_nodelay on;

  keepalive_timeout 10;

  gzip on;

  gzip_min_length 1k;

  gzip_buffers 4 8k;

  gzip_http_version 1.1;

  gzip_comp_level 3;

  gzip_types text/css text/xml text/plain application/x-javascript application/xml application/pdf application/x-perl application/x-tcl application/msword application/rtf application/vnd.ms-excel application/vnd.ms-powerpoint application/vnd.wap.xhtml+xml image/x-ms-bmp;

  gzip_disable "MSIE [1-6] .";

  gzip_vary on;

  output_buffers 4 32k;

  client_max_body_size 20m;

  server {

    listen 80;

    server_name localhost;

    charset utf-8,gb2312;

    access_log /home/ubuntu/log/access.log;

    server_name_in_redirect off;

    location / {

      root /home/ubuntu/www;

      index index.html index.htm index.php;     location ~ .php$ {

    fastcgi_pass 127.0.0.1:9000;

    fastcgi_index index.php;

    set $path_info "/";

    set $real_script_name $fastcgi_script_name;

    if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {

        set $real_script_name $1;

        set $path_info $2;     }

    location ~ .*.(gif|jpg|jpeg|png|bmp|swf|js|css|ico)$ {

      root /home/ubuntu/www;

      access_log off;

      expires 30d;     error_page 500 502 503 504 /50x.html;

    location = /50x.html {

      root /home/ubuntu/www;     fastcgi_param SCRIPT_FILENAME /home/ubuntu/www/$real_script_name;

    fastcgi_param path_info $path_info;

    include /etc/nginx/fastcgi_params; }

8、下载并安装eaccelerator(),最新版:Release-0.9.6.1 - 2010/05/31 。

sudo apt-get install php5-dev

wget http://bart.eaccelerator.net/source/0.9.6.1/eaccelerator-0.9.6.1.tar.bz2 //下载

tar xvjf eaccelerator-0.9.6.1.tar.bz2 //解压

cd eaccelerator-0.9.6.1

mkdir /tmp/eaccelerator //创建缓存目录

chmod 0777 /tmp/eaccelerator //设置为可读写

cp control.php /home/ubuntu/www/control.php //复制控制程序到网站目录,通过http://网站名/control.php访问,默认帐号为admin,密码为eAccelertor,可编辑此文件修改。

phpize 

sudo ./configure --enable-eaccelerator=shared

sudo make 

sudo make install

sudo vi /etc/php5/cgi/php.ini

zend_extension="/usr/lib/php5/20090626+lfs/eaccelerator.so" 

eaccelerator.shm_size="16" 

eaccelerator.cache_dir="/tmp/eaccelerator" 

eaccelerator.enable="1" 

eaccelerator.optimizer="1" 

eaccelerator.check_mtime="1" 

eaccelerator.debug="0" 

eaccelerator.filter="" 

eaccelerator.shm_max="0" 

eaccelerator.shm_ttl="0" 

eaccelerator.shm_prune_period="0" 

eaccelerator.shm_only="0" 

eaccelerator.compress="1" 

eaccelerator.compress_level="9" 

eaccelerator.allowed_admin_path="/home/ubuntu/www/control.php"

9、修复nginx+php出现的重大漏洞、修改上传文件大小(可以看你自己的情况)

sudo vi /etc/php5/cgi/php.ini

cgi.fix_pathinfo = 0 //修复漏洞

upload_max_filesize = 2M改为5M //修改上传文件大小

10、设定防火墙

sudo ufw enable

sudo ufw default deny

sudo ufw allow 80

sudo ufw allow 22

11、重启系统、上传文件、导入mysql数据

文件上传建议用filezilla(),免费的开源ftp软件,windows和linux都可以用,支持ssh的22端口。

13、网站建立成功!试试吧!附:系统及部分软件管理操作

1、操作系统:

sudo shutdown -r now //重启系统

sudo halt //关闭系统

2、nginx配置修改及生效:

sudo vi /etc/nginx/nginx.conf //修改配置

sudo /etc/init.d/nginx restart //重启nginx

3、php配置修改及生效:

sudo vi /etc/php5/cgi/php.ini //修改配置

sudo pkill php5-cgi //杀死进程

sudo /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -C 4 -P /var/run/fastcgi-php.pid //启动fastcgi进程

3、phpmyadmin管理mysql数据:

http://你的网站/phpmyadmin/

4、eaccelerator管理:

http://你的网站/control.php

5、Discuz7.2后台启动 URL静态化,会提示 404 Not Found的解决办法:

在niginx中开启Rewrite,在服务器配置文件nignx.conf中写入以下内容,然后重启nginx。

rewrite ^/archiver/((fid|tid)-[w-]+.html)$ /archiver/index.php?$1 last;

rewrite ^/forum-([0-9]+)-([0-9]+).html$ /forumdisplay.php?fid=$1&page=$2 last;

rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ /viewthread.php?tid=$1&extra=page=$3&page=$2 last;

rewrite ^/space-(username|uid)-(.+).html$ /space.php?$1=$2 last;

rewrite ^/tag-(.+).html$ /tag.php?name=$1 last;

break;

注意你的论坛地址,如论坛目录为/bbs/,则上面的代码改为:

rewrite ^/bbs/archiver/((fid|tid)-[w-]+.html)$ /bbs/archiver/index.php?$1 last;

rewrite ^/bbs/forum-([0-9]+)-([0-9]+).html$ /bbs/forumdisplay.php?fid=$1&page=$2 last;

rewrite ^/bbs/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ /bbs/viewthread.php?tid=$1&extra=page=$3&page=$2 last;

rewrite ^/bbs/space-(username|uid)-(.+).html$ /bbs/space.php?$1=$2 last;

rewrite ^/bbs/tag-(.+).html$ /bbs/tag.php?name=$1 last;

break;

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。