Back

在ubuntu上搭建LNMP服务器

|

LNMP = Linux + Nginx + MySQL + PHP
安装Nginx
执行以下命令即可:
[cc lang="bash"]apt-get install nginx[/cc]
不过源里的版本是0.7.65,不喜欢老旧的玩意,可以尝试编译安装。
编译安装nginx.
1.准备编译环境
[cc lang="bash"]apt-get install libpcre3-dev build-essential libssl-dev[/cc]
在这里 http://nginx.org/en/download.html 能找到nginx的tar球,最新的官方稳定版是
1.2.0.
执行:
[cc lang="bash"]cd /opt/
wget http://nginx.org/download/nginx-1.2.0.tar.gz
tar -zxvf nginx-1.2.0.tar.gz
cd /opt/nginx-1.2.0/[/cc]
configure:
[cc lang="bash"]./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http\_ssl\_module[/cc]
configure完成后会输出nginx的一些相关信息,
nginx path prefix: "/opt/nginx"
nginx binary file: "/opt/nginx/sbin/nginx"
nginx configuration prefix: "/opt/nginx/conf"
nginx configuration file: "/opt/nginx/conf/nginx.conf"
nginx pid file: "/opt/nginx/logs/nginx.pid"
nginx error log file: "/opt/nginx/logs/error.log"
nginx http access log file: "/opt/nginx/logs/access.log"
nginx http client request body temporary files: "client\_body\_temp"
nginx http proxy temporary files: "proxy\_temp"
nginx http fastcgi temporary files: "fastcgi\_temp"
make,安装,执行:
[cc lang="bash"]make
make install[/cc]
给nginx进程添加用户nginx:
[cc lang="bash"]adduser --system --no-create-home --disabled-login --disabled-password --group nginx[/cc]
下载并安装启动脚本:
[cc lang="bash"]wget -O init-deb.sh http://library.linode.com/assets/552-init-deb.sh
mv init-deb.sh /etc/init.d/nginx
chmod +x /etc/init.d/nginx
/usr/sbin/update-rc.d -f nginx defaults[/cc]
启动一下试试:
[cc lang="bash"]/etc/init.d/nginx start[/cc]
如果输出正常,那nginx就安装成功了。
配置虚拟主机:
如果从源里安装的nginx,那nginx配置文件位于/etc/nginx/sites-enabled
下面是一个虚拟主机的配置实例:
[cc]server {
listen 80;
server\_name www.example.com example.com;
access\_log /srv/www/example.com/logs/access.log;
error\_log /srv/www/example.com/logs/error.log;
location / {
root /srv/www/example.com/public\_html;
index index.html index.htm;
}
}[/cc]
注意,配置文件中出现的目录必须存在,nginx并不会自动创建他们,所以,还需要执行:
[cc lang="bash"]mkdir -p /srv/www/example.com/public\_html
mkdir -p /srv/www/example.com/logs[/cc]
如果是编译安装的版本,相关路径见上文configure部分,配置文件位于/opt/nginx/conf,可以直接在nginx.conf添加server段,为了便于管理,还是把虚拟主机独立出来好,修改nginx.conf, 如下在http段添加include部分。
[cc lang="bash"]http {
# [...]
include /opt/etc/nginx/sites-enabled/\*;
# [...]
}[/cc]
添加完虚拟主机后,别忘了重启以下nginx服务。
[cc lang="bash"]/etc/init.d/nginx restart[/cc]
删除虚拟主机只需要删除对应的配置文件并重新启动一下下nginx就好。
安装php with fastcgi
执行:
[cc lang="bash"]apt-get install php5-cli php5-cgi psmisc spawn-fcgi[/cc]
执行下面的命令会下载并安装fastcgi的控制脚本:
[cc lang="bash"]cd /opt/
wget -O php-fastcgi-deb.sh http://library.linode.com/assets/554-php-fastcgi-deb.sh
mv /opt/php-fastcgi-deb.sh /usr/bin/php-fastcgi
chmod +x /usr/bin/php-fastcgi
wget -O init-php-fastcgi-deb.sh http://library.linode.com/assets/553-init-php-fastcgi-deb.sh
mv /opt/init-php-fastcgi-deb.sh /etc/init.d/php-fastcgi
chmod +x /etc/init.d/php-fastcgi
/etc/init.d/php-fastcgi start
update-rc.d php-fastcgi defaults[/cc]
然后在虚拟主机的配置文件里面添加php支持,示例如下:
[cc]server {
server\_name www.example.com example.com;
access\_log /srv/www/example.com/logs/access.log;
error\_log /srv/www/example.com/logs/error.log;
root /srv/www/example.com/public\_html;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
include /etc/nginx/fastcgi\_params;
fastcgi\_pass 127.0.0.1:9000;
fastcgi\_index index.php;
fastcgi\_param SCRIPT\_FILENAME /srv/www/example.com/public\_html$fastcgi\_script\_name;
}
}[/cc]
最后重启一下nginx服务:
[cc lang="bash"]/etc/init.d/nginx restart[/cc]
安装MySQL:
执行:
[cc lang="bash"]apt-get install mysql-server php5-mysql[/cc]
安装过程中会要求输入数据库的初始密码,
还可以执行一下:
[cc lang="bash"]mysql\_secure\_installation[/cc]
禁用root的远程登录即可。
如果需要重新设置MySQL的密码,执行:
[cc lang="bash"]dpkg-reconfigure mysql-server-5.0[/cc]
最后重启一下fastcgi:
[cc lang="bash"]/etc/init.d/php-fastcgi restart[/cc]
至此LNMP安装完成。
本文翻译自Linode的Library文章:http://library.linode.com/lemp-guides/ubuntu-10.04-lucid#sph\_id7
文中的操作系统是Ubuntu 10.04,你可以访问上面的连接获取更多信息。个人更推荐一键傻瓜安装包:
http://www.lnmp.org

No comments yet

Leave a comment

About me

I live in Tasmania, an island a little far from the rest of the world and very close to the sea.
Father of two little girls, have two puppies.

Work

Work in IT industry, AI is my working partner.