把我架设lnmp网站的过程写出来,希望对想架设网站的朋友有所帮助,如有更好的办法请提出来。
之所以用nginx没用apache,是因为nginx的效率更高一些,尤其是对一些低配置的服务器,比如我在单位256M内存的旧机器上架设的服务器。
1、安装ubuntu server 10.04或10.10,其中安装语言选的en,时区shanghai,服务只安装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
deb http://ppa.launchpad.net/nginx/stable/ubuntu lucid main
deb http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main
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-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-backports main restricted universe multiverse
deb http://ppa.launchpad.net/nginx/stable/ubuntu maverick main
最后一行为nginx的ppa源,需要添加key,在终端运行:
代码:
sudo apt-key adv --keyserver keyserver.Ubuntu.com --recv-keys C300EE8C
3、更新
代码:
sudo apt-get update
4、安装网站系统
代码:
sudo apt-get install nginx php5-common php5-dev php5-cgi php5-fpm php-apc php5-mysql php5-curl php5-gd php5-idn php-pear php5-mcrypt php5-memcache php5-ming php5-recode php5-tidy php5-xmlrpc php5-xsl mysql-server
上面为必选安装,以下php组件为可选安装,一般网站程序可能用不着:
代码:
sudo apt-get install php5-imagick php5-imap php5-recode php5-snmp php5-sqlite php5-xmlrpc php5-suhosin php5-odbc php5-ladp
5、修改nginx配置文件
代码:
sudo vi /etc/nginx/sites-enabled/default
把其中的:
代码:
location / {
root /var/www;
index index.html index.htm;
}
改为:
代码:
location / {
root /var/www/nginx-default;
index index.php index.html index.htm;
}
其中的:
代码:
#location ~ \.php$ {
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# include fastcgi_params;
#}
改为:
代码:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
include fastcgi_params;
}
6、更改网站目录权属:
代码:
sudo chown -R ubuntu nginx-default/
注:其中的ubuntu为系统登录用户名。
7、安装ZendGuardLoader及eaccelerator:
代码:
sudo mkdir /usr/zend
mkdir /tmp/eaccelerator
chmod 0777 /tmp/eaccelerator
wget http://phpcj.googlecode.com/files/ZendGuardLoader.so
sudo mv ZendGuardLoader.so /usr/zend/ZendGuardLoader.so
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
cp control.php /var/www/nginx-default/control.php //复制控制程序到网站目录,通过http://网站名/control.php访问,默认帐号为admin,密码为eAccelertor,可编辑此文件修改。
phpize
sudo ./configure --enable-eaccelerator=shared
sudo make
sudo make install
sudo vi /etc/php5/fpm/php.ini
在配置文件最后加上:
代码:
zend_extension=/usr/zend/ZendGuardLoader.so
zend_loader.enable=1
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3
zend_loader.license_path=
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="/var/www/nginx-default/control.php"
8、(可选步骤)安装phpmyadmin:
代码:
wget http://downloads.sourceforge.net/project/phpmyadmin/phpMyAdmin/3.3.9/phpMyAdmin-3.3.9-all-languages.tar.bz2
tar xvjf phpMyAdmin-3.3.9-all-languages.tar.bz2
mv phpMyAdmin-3.3.9-all-languages /var/www/nginx-default/phpmyadmin
cd /var/www/nginx-default/phpmyadmin
cp config.sample.inc.php config.inc.php
vi config.inc.php
将其中的:
代码:
$cfg['blowfish_secret'] = '';
改为:
代码:
$cfg['blowfish_secret'] = 'web';
下面的:
代码:
// $cfg['Servers'][$i]['controluser'] = 'pma';
// $cfg['Servers'][$i]['controlpass'] = 'pmapass';
// $cfg['Servers'][$i]['pmadb'] = 'phpmyadmin';
// $cfg['Servers'][$i]['bookmarktable'] = 'pma_bookmark';
// $cfg['Servers'][$i]['relation'] = 'pma_relation';
// $cfg['Servers'][$i]['table_info'] = 'pma_table_info';
// $cfg['Servers'][$i]['table_coords'] = 'pma_table_coords';
// $cfg['Servers'][$i]['pdf_pages'] = 'pma_pdf_pages';
// $cfg['Servers'][$i]['column_info'] = 'pma_column_info';
// $cfg['Servers'][$i]['history'] = 'pma_history';
// $cfg['Servers'][$i]['tracking'] = 'pma_tracking';
// $cfg['Servers'][$i]['designer_coords'] = 'pma_designer_coords';
// $cfg['Servers'][$i]['auth_swekey_config'] = '/etc/swekey-pma.conf';
将//全部删除,然后将其中的:
代码:
$cfg['Servers'][$i]['controluser'] = 'pma';
$cfg['Servers'][$i]['controlpass'] = 'pmapass';
pma和pmapass改为你的mysql用户名和密码,最后登录phpmyadmin,将phpmyadmin/scripts目录中的creat_tables.sql文件导入mysql。
9、重启系统、上传文件,网站建立成功!试试吧!
文件上传建议用filezilla(http://filezilla-project.org/),免费的开源ftp软件,windows和linux都可以用,支持ssh的22端口。
附:系统及部分软件管理操作
1、操作系统:
代码:
sudo reboot now //重启系统
sudo halt //关闭系统
2、nginx配置修改及生效:
代码:
sudo vi /etc/nginx/nginx.conf //修改配置
sudo vi /etc/nginx/sites-enabled/default //修改配置
sudo service nginx restart //重启nginx
3、php配置修改及生效:
代码:
sudo vi /etc/php5/fpm/php.ini //修改配置
sudo service php5-fpm restart //重启fastcgi进程
3、网站目录:
代码:
/var/www/nginx-default
4、eaccelerator管理:
代码:
http://你的网站/control.php
5、修复nginx+php出现的重大漏洞、修改上传文件大小(可以看你自己的情况)
代码:
sudo vi /etc/php5/fpm/php.ini
cgi.fix_pathinfo = 0 //修复漏洞
upload_max_filesize = 2M改为5M //修改上传文件大小
6、设定防火墙
代码:
sudo ufw enable
sudo ufw default deny
sudo ufw allow 80
sudo ufw allow 22
7、启动php5-fpm时,出现:
代码:
PHP Deprecated: Comments starting with '#' are deprecated in /etc/php5/fpm/conf.d/ming.ini on line 1 in Unknown on line 0
[WARNING] [pool www] pm.start_servers is not set. It's been set to 20.
的提示,第一行的原因是在配置文件中已用;代替#来进行注释。修改以下文件:
代码:
vi /etc/php5/fpm/conf.d/ming.ini
将#改为;即可。
第二行原因是/etc/php5/fpm/pool.d/www.conf配置文件中的
代码:
;pm.start_servers = 20
去掉前面的;即可。
8、Discuz后台启动 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%3D$3&page=$2 last;
rewrite ^/space-(username|uid)-(.+).html$ /space.php?$1=$2 last;
rewrite ^/tag-(.+).html$ /tag.php?name=$1 last;
break;
以下内容来自:http://www.vpsee.com/2011/04/some-nginx-rewrite-examples-for-subdirectories/,未测试。
Discuz! 7.2 安装在子目录 /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%3D$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;
Discuz! X1.5 安装在子目录 /bbs 下:
代码:
rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/([a-z]+)-(.+)\.html$ $1/$2.php?rewrite=$3 last;
if (!-e $request_filename) {
return 404;
}