Setup Low End VPS with Nginx, PHP, MySQL

I currently get some low end VPSes from BlazeVPS and BuyVM for fun. It was $10~$15/year, no harm to get few for testing purpose. Since the VPS only have 128MB of RAM, the installation will be difference from normal VPS. Let's share something regarding how I setup my low end VPS.

  1. My favorite OS is Ubuntu but I'm not going to use it this time. I've choosing Debian 5 32bit which is taking less resource. My first step is to rebuild the machine with Debian 5 32bit image. A fresh Debian 5 should take about 15MB of RAM.

  2. Once it's done, setup a hostname for it. I've named this server as "oxygen.on9server.com".
    sudo hostname oxygen.on9server.com
  3. Well, let change the time zone of the server to my local time.
sudo dpkg-reconfigure tzdata
  1. Now it's time to remove some unneccesary applications to reduce the burden of the system.

  2. Some Debian have portmap installed, remove it.
    sudo apt-get remove -y portmap
  3. Remove rsyslog which is taking a lot resources.
    sudo apt-get remove -y rsyslog
  4. Since this is a low memory box, I have to abandon Apache as my web server. Remove it, I will install Nginx for replacement.
    sudo apt-get remove -y apache2*
  5. Remove Samba server as well.
    sudo apt-get remove -y samba*
  6. I will use an external DNS server, so I will remove bind9.
    sudo apt-get remove -y bind9
  7. Remove name service cache daemon.
    sudo apt-get remove -y nscd
  8. Since I'm not sending e-mail using the server, I remove the sendmail too. Stop the service:
    /etc/init.d/sendmail stop

    Remove it:

    sudo apt-get remove -y sendmail*
  9. Horray! The system should be lighter and clean now. Let's do an update to system repositories and start installing some esential applications.
    apt-get -q -y update
    apt-get -q -y upgrade
  10. Install dash as replacement for the existing bash command shell.
    sudo apt-get install -y dash

    Remove the existing bash:

    rm -f /bin/sh

    Create a symbolic link as replacement:

    ln -s dash /bin/sh
  11. Install MySQL server.
    sudo apt-get install -y mysql-server

    This may taking some times. Once it's done, stop MySQL server:

    /etc/init.d/mysql stop

    Since InnoDb engine taking a lot of resources, we are going to stop it. Let's remvoe existing InnoDb database files:

    rm -f /var/lib/mysql/ib*

    Create a new cnf file with InnoDb disabled. Create a new file "/etc/mysql/conf.d/mini.cnf", and insert following lines:

[mysqld]
key_buffer = 8M
query_cache_size = 0
skip-innodb
Save and start MySQL server again:
/etc/init.d/mysql start
  1. And now, setup a Nginx (pronounced "Engine X") web server.
    sudo apt-get install -y nginx

    We need to increase the bucket size, create a new configuration file at "/etc/nginx/conf.d/mini.conf", and insert following line:

server_names_hash_bucket_size 64;
Save, and restart Nginx:
/etc/init.d/nginx restart
  1. For dynamic scripting, I will using PHP fast-cgi. Install all neccesary package:
    sudo apt-get install -y php-cgi php5-cgi php5-cli php5-mysql php-apc
  2. Create a script to control the php-cgi at "/etc/init.d/php-cgi". Insert the following lines:
#!/bin/bash
BIND="127.0.0.1:9000"
USER="www-data"
PHP_FCGI_CHILDREN=1
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI="/usr/bin/php-cgi"
PHP_CGI_NAME="php-cgi"
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
    echo -n "Starting: "
    start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
    RETVAL=$?
    echo "$PHP_CGI_NAME."
}

stop() {
    echo -n "Stopping: "
    killall -q -w -u $USER $PHP_CGI
    RETVAL=$?
    echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
        start
    ;;

    stop)
        stop
    ;;

    restart)
        stop
        start
    ;;

    *)
        echo "Usage: php-cgi {start|stop|restart}"
        exit 1
    ;;
esac
exit $RETVAL
  1. Now, everything I needed are installed. Let's create a website to try.

  2. Make new directories for the website:
    mkdir -p /var/www/default/logs
    mkdir -p /var/www/default/public_html
  3. Create a file "/var/www/default/public_html/index.php" and insert following lines:
<?php
phpinfo();
?>
  1. Create and edit file "/etc/nginx/sites-enabled/default". Insert following lines:
server {
    server_name localhost;
    root /var/www/default/public_html;
    access_log /var/www/default/logs/access.log;

    include /etc/nginx/fastcgi_php;
    location / {
        index index.php;
        if (!-e $request_filename) {
            rewrite ^(.*)$  /index.php last;
        }
    }
}

Restart Nginx:

invoke-rc.d nginx reload
  1. Let's try out by access the website using http://[your_vps_ip_address]<vps_ip_address><your_vps_ip_address> from your browser.

4 Comments

  1. Sei Kan

    优惠已经过去了。
    看看这个吧,http://www.lowendbox.com/blog/nordicvps-15year-128mb-openvz-vps-in-germany-exclusive-offer/
    现在每年15刀,服务器在德国。适合拿来玩玩~

  1. wanjie

    嘿嘿,这2天正研究vps,当然找便宜的开始练手,貌似现在他家只有25刀的了:)

  1. wanjie

    呵呵,谢谢,这家今天下午也看到了,研究好了vps搭键vpn就下手。貌似只能openvpn,而openvpn又只能是2客户,够麻烦的。

  1. Sei Kan

    暂时还没玩过VPN,有空一定也参一脚。

Leave a Comment

Your email address will not be published. Required fields are marked *