How to config VPS / VDS server for Debian & Ubuntu

I decided to write this article in order to gather all the information on this subject in one place. Many questions arise to optimize server. Tell you how I achieved the speed of page loading for 0.013 seconds. So. We have a clean Debian or Ubuntu.
We will install and configure:
Apache 2
PHP 5
MySQL 5
nginx
memcached
eAcellerator
Sphinx
Will write for debian, just 6 for Debian, almost all the same.
Hope that we have a network already configured.
Install LAMP
apache2 php5 php5-mysql mysql-server5.0.
Install nginx.
All put out off repository
apt-get install nginx libapache2-mod-rpaf
Hang Apache on a different port, for example 81 (can the 8080, doesn’t matter):
$ nano/etc/apache2/ports.conf
In the assign file:
Listen *: 81
Next, configure the virtual host apache.
Copy the default config:
$ cd/etc/apache2/sites-available
$ sudo cp/default domain.ru/etc/apache2/sites-enabled/.
Edit config virtualhosta Apache:
$ nano/etc/apache2/sites-enabled/domain.ru
<VirtualHost *:81>
ServerName www.example.com
ServerAlias example.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory〉
ErrorLog /var/log/apache2/error.log
LogLevel warn
ServerSignature On
</VirtualHost>

 

Configure gzip compression for nginx. Open the config:
$ nano/usr/local/etc/nginx/nginx.conf
My config looks like this:
user www-data;
worker_processes  1;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;
events {
worker_connections  1024;
}
http {
include       /etc/nginx/mime.types;
default_type  application/octet-stream;
server_names_hash_bucket_size 64;
access_log  /var/log/nginx/access.log;
sendfile        on;
#tcp_nopush     on;
#keepalive_timeout  0;
keepalive_timeout  65;
tcp_nodelay        on;
gzip                on;
gzip_proxied        any;
gzip_min_length     1100;
gzip_http_version   1.0;
gzip_buffers        4 8k;
gzip_comp_level     9;
gzip_types          text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
# Enable compression for all proxied requests
gzip_proxied any;
# MIME types that you want to press
gzip_types text/plain text/html, text/xml, application/xml, application/x-javascript text/javascript, text/css, text/json;
# Gzip compression Level (I have 9, i.e. the maximum compression, as resources allow)
gzip_comp_level 8;

 

To create a new virtualhosta config:

 

$ nano/etc/nginx/sites-enabled/domain.ru

 

server {
listen 80;
server_name domain.ru;
access_log /var/log/nginx/domain.ru-nginx.access.log;
location / {
proxy_pass http://127.0.0.1:81/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 40m;
client_body_buffer_size 256k;
proxy_connect_timeout 120;
proxy_send_timeout 120;
proxy_read_timeout 120;
proxy_buffer_size 64k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#Static files location
location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|html|flv|mp3)$ {
root /var/www/domain.ru; }
}

 

Check config nginx:
$ nginxt
Install memcached:
$ apt-get install memcached
Memcached configuration is located at/etc/php5/conf.d/memcache.ini
Getting change anything is not worth it.
Install eAccelerator.
Just pull php5-dev, because we need the utility phpize.
$ sudo apt-get install php5-dev
$ sudo apt-get install make
$ sudo cd /tmp/
$ sudo wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
$ sudo tar xvjf eaccelerator-0.9.5.3.tar.bz2
$ sudo cd eaccelerator-0.9.5.3
$ sudo phpize
$ sudo ./configure --enable-eaccelerator=shared
$ sudo make
$ sudo make install

 

Create a cache folder:
$ sudo mkdir -p /var/cache/eaccelerator
$ sudo chmod 0777 /var/cache/eaccelerator

 

Edit config PHP:

$ nano /etc/php5/apache2/php.ini

 

Add a line at the top:
[PHP]
; eAccelerator configuration
; Note that eAccelerator may also be installed as a PHP extension or as a zend_extension
; If you are using a thread safe build of PHP you must use
; zend_extension_ts instead of zend_extension
extension                       = "eaccelerator.so"
eaccelerator.shm_size           = "16"
eaccelerator.cache_dir          = "/var/cache/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/eaccelerator"

 

Attention!
If you have enough RAM, then recommend a bytecode cache mode solely in RAM:

eaccelerator.shm_only           = “1”

 

As well as increase the available memory for cache:

eaccelerator.shm_size           = “128”

 

Also in the package there is a file aksselleratora control.php, which you can manage the cache and analyze errors, which is very convenient. Put this file it is necessary on the way

eaccelerator.allowed_admin_path = “/var/www/eaccelerator”

 

Otherwise it will throw errors.
Next install Sphinx.
Why, I repeat, because this has already been written a couple of posts (by the way online manuals Poti not)? Answer: I am faced with such problems, which do not obhvačeny, so am writing because these moments.
Pull the source code (no package repositories), unpack and install:
$ wget http://www.sphinxsearch.com/downloads/sphinx-0.9.8.1.tar.gz
$ tar –xzvf sphinx-0.9.8.tar.gz
$ cd sphinx-0.9.8
$ ./configure
$ make
$ make install
$ mkdir -p /var/db/sphinx/log
$ chmod -R 777 /var/db/

 

Next, edit the configuration file, kindly compiled by kruft’om (for which he, or not to him, but going thanks), but I will give a fully working immediately and ready version (version of the author’s article is not relevant and requires dopilivaniâ):

$ nano /usr/local/etc/sphinx.conf

 

Sphinx configuration file for indexing Live Street

 

#######################
#

#
#######################

source lsParentSource
{
type            = mysql
sql_host        = your_database_host
sql_user        = your_database_login
sql_pass        = your_database_password
sql_db          = your_database_name
sql_port        = 3306

sql_sock        = /var/run/mysqld/mysqld.sock

mysql_connect_flags     = 32 # 32-
sql_query_pre                   = SET NAMES utf8
sql_query_pre                   = SET SESSION query_cache_type=OFF
}

source topicsSource : lsParentSource
{

sql_query               = \
SELECT t_fast.topic_id, t_fast.topic_title, UNIX_TIMESTAMP(t_fast.topic_date_add) as topic_date_add, \
tc.topic_text, t_fast.topic_publish \
FROM prefix_topic as t_fast, prefix_topic_content AS tc \
WHERE t_fast.topic_id=tc.topic_id AND t_fast.topic_id>=$start AND t_fast.topic_id<=$end

sql_query_range         = SELECT MIN(topic_id),MAX(topic_id) FROM prefix_topic

sql_range_step          = 1000

sql_attr_bool           = topic_publish

sql_attr_timestamp      = topic_date_add

sql_attr_multi  = uint tag from query; SELECT topic_id, topic_tag_id FROM prefix_topic_tag

sql_ranged_throttle     = 0
}

source commentsSource : lsParentSource
{
sql_query               = \
SELECT comment_id, comment_text, UNIX_TIMESTAMP(comment_date) as comment_date, comment_delete \
FROM prefix_topic_comment \
WHERE comment_id>=$start AND comment_id<=$end

sql_query_range         = SELECT MIN(comment_id),MAX(comment_id) FROM prefix_topic_comment
sql_range_step          = 5000

sql_attr_bool           = comment_delete
sql_attr_timestamp      = comment_date
}

#######################
#

#
#######################

index topicsIndex
{

source                  = topicsSource
path                    = /usr/local/SphinxIndex/topicsSource

docinfo                 = extern

mlock                   = 0

morphology              = stem_enru, soundex, metaphone

# Kodirovaka data from a data source


charset_type            = utf-8

html_strip                              = 1
}

index commentsIndex
{
source                  = commentsSource
path                    = /usr/local/SphinxIndex/commentsSource

docinfo                 = extern

mlock                   = 0

morphology              = stem_enru, soundex, metaphone

charset_type            = utf-8
}

#######################
#

#
#######################

indexer
{

mem_limit                       = 32M
}

#######################

#######################

searchd
{

address                         = 127.0.0.1

port                            = 3312

log                                     = /var/log/sphinx/searchd.log

query_log                       = /var/log/sphinx/query.log

read_timeout            = 5

# Maximum number of simultaneously processed requests. 0 means a ton, or rather without restrictions


max_children            = 30

# File in which to store the PID of the process at startup


pid_file                        = /var/log/sphinx/searchd.pid
}

 

Do not forget to write the MySQL settings.
Next, create a folder for the database index and set write permissions:
$ mkdir -p /usr/local/SphinxIndex
$ chmod -R 777 /usr/local/SphinxIndex/

 

Run the indexing:

$ /usr/local/bin/indexer –all

 

Start the daemon:

$ /usr/local/bin/searchd

 

This means that we will start indexing every 3:0.

$ nano /etc/crontab

Добавляем:

0 */3 * * * /usr/local/bin/indexer –all –rotate
Это значит, что мы будем запускать индексацию каждые 3 часа.

Перезапускаем сервисы:

$ service apache2 restart
$ service nginx restart
$ service memcached restart

 

The SIM everything should work.

Проверяем в phpinfo()

In headere eAccelerator:

This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.2.0, Copyright © 1998-2008 Zend Technologies
with eAccelerator v0.9.5.3, Copyright © 2004-2006 eAccelerator, by eAccelerator

And below

memcache support enabled

Check the workability of the ports (PID and DEVICE of course may be different):

lsof -i

 

We should see the following line:
apache2     948 www-data    3u  IPv4  47935       TCP *:81 (LISTEN)
memcached  3096   nobody    3u  IPv4 165608       TCP localhost:11211 (LISTEN)
nginx     19605 www-data    6u  IPv4  62263       TCP *:www (LISTEN)