Chinaunix首页 | 论坛 | 博客
  • 博客访问: 477391
  • 博文数量: 58
  • 博客积分: 6012
  • 博客等级: 准将
  • 技术积分: 1211
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-09 22:50








2010-11-18 08:51:10

To build a perfect LNMP with those packages: Ubuntu 10.04 LTS, Nginx, MySQL, Php5

The nginx web server is a fast, lightweight server designed to efficiently handle the needs of both low and high traffic websites. Although commonly used to serve static content, it's quite capable of handling dynamic pages as well. This guide will help you get nginx up and running with PHP and FastCGI on your Ubuntu 10.04 LTS (Lucid) Linux VPS.

We assume you've already followed the steps outlined in our . These steps should be performed via a root login to your Linode VPS over SSH.

These instructions work with the Linode platform. If you don't have a Linode yet, sign up for a  and get started today.


Install Required Packages Link

Issue the following commands to update your system and install the nginx web server, PHP, and MySQL Server packages:

# apt-get update
# apt-get upgrade
# apt-get install nginx php5-cli php5-cgi spawn-fcgi php5-mysql mysql-server
# echo "console output" >> /etc/init.d/nginx
# update-rc.d nginx defaults
# /etc/init.d/nginx start

Various additional dependency packages will be installed along with the ones we requested, and will ask you to input password for the root user of Mysql. Once the installation process finishes, you may wish to make sure nginx is running by browsing to your IP address, take this for example: You should get the default ngnix page.

Configure Your Site Link

In this guide, we'll be using the domain "" as our example site. You should substitute your own domain name in the configuration steps that follow. First, we'll need to create directories to hold our content and log files:

# mkdir -p /var/www/dajiamaicom 
# cat > /var/www/dajiamaicom/index.php <

Welcome to nginx!

Welcome to nginx!

EOF # chown -R www-data:www-data /var/www/dajiamaicom

Next, we'll need to define our site's virtual host file:

File: /etc/nginx/sites-available/

# cat > /etc/nginx/sites-availabe/ <
server {
    listen   80;
error_log /var/log/nginx/dajiamaicom.error.log;
location / { root /var/www/dajiamaicom; index index.html index.htm index.php; } location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass; fastcgi_index index.php index.html; fastcgi_param SCRIPT_FILENAME /var/www/dajiamaicom$fastcgi_script_name; } } EOF

Issue the following commands to enable the site:

# cd /etc/nginx/sites-enabled/
# ln -sv ../sites-available/ ./
# /etc/init.d/nginx restart

You may wish to create a test HTML page under /var/www/dajiamaicom/ and view it in your browser to verify that nginx is properly serving your site (PHP will not work yet). Please note that this will require an  pointing your domain name to your IP address.At windows, you could edit the file c:\windows\system32\drivers\etc\hosts, append below line to serve as DNS temporarily:

NOTICE: you should replace with your Ubuntu 10.04 host's real IP address,and then you can browse to test the new website.

Configure spawn-fcgi Link

Create a wrapper script for spawn-fcgi:

File: /usr/sbin/php-fastcgi

# cat > /usr/sbin/php-fastcgi << EOF
/usr/bin/spawn-fcgi -a -p 9000 -C 6 -u www-data -f /usr/bin/php5-cgi

Make the script executable with the following command:

chmod a+x /usr/sbin/php-fastcgi

Create an init script for fast-cgi:
File: /etc/init.d/php-fastcgi

# cat > /etc/init.d/php-fastcgi << EOF
case "$1" in
      killall -9 php5-cgi
      killall -9 php5-cgi
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
exit $RETVAL
console output

Issue the following commands to make the init script run at startup and launch it for the first time:

# chmod 755 /etc/init.d/php-fastcgi
# update-rc.d php-fastcgi defaults
# /etc/init.d/php-fastcgi start

change the default character_set of MySQL, just to add below line in [client] and [mysqld] sections :


reboot mysql server :

# /etc/init.d/mysql restart

Issue the following commands to make sure the needed ports to be listen:

# netstat -tnlp
tcp        0      0*               LISTEN      463/mysqld
tcp        0      0    *               LISTEN      476/nginx
tcp        0      0*               LISTEN      488/php5-cgi

More Information Link

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

License Link

This guide is licensed under a External Link. Please feel free to redistribute unmodified copies of it as long as attribution is provided, preferably via a link to this page.

阅读(1693) | 评论(0) | 转发(0) |