128MB ram – tak mało, a tak wiele.

128MB ram – tak mało, a tak wiele.

Nanana, kupiłem sobie VPS’a za 5$/rok. Przyda się, nie wiem jeszcze do czego, ale warto mieć pod ręką maszynki na różnych kontynentach. Przydaje się w życiu.
Ile można z tego wycisnąć?
Jak zoptymalizować podstawowe usługi do działania na tej maszynie?
Jak wyhodowac VPS’a na sterydach?
Zobaczmy!

No tak, 5$ za rok, to w tej cenie dostałem nic innego jak 128 mb ramu, ale na Xeonie który przy dobrych wiatrach lata do 3.4 GHz.

Dell blade

processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 58
model name : Intel(R) Xeon(R) CPU E3-1240 V2 @ 3.40GHz
stepping : 9
cpu MHz : 3400.023
cache size : 8192 KB
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable 
nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt aes xsave avx hypervisor lahf_lm ida arat epb xsaveopt pln pts dts
bogomips : 6800.04
clflush size : 64
cache_alignment : 64
address sizes : 40 bits physical, 48 bits virtual
power management:

Nie jest źle. 5GB miejsca na dysku, 350GB trafficu/mc. Rurka nie wiem jakiej szerokości, niby 100mbps, speedtest mówi:

admin@smwh ~ $ speedtest-cli
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from ColoCrossing (23.94.38.193)...
Selecting best server based on ping...
Hosted by Makaiwell (Buffalo, NY) [16.56 km]: 13.272 ms
Testing download speed........................................
Download: 119.14 Mbit/s
Testing upload speed..................................................
Upload: 47.10 Mbit/s

Tak czy siak, działa jak powinno i to się liczy. A gdzie takie cudo wyrwałem?
https://123systems.net
128MB ramu. Powalczymy?
Plany były dość ambitne, nie wiedziałem z początku jak to wszystko wyjdzie.
PHP+MySQL+ZSH+HTTPD+rózne bajerki; to wydaje się dość dużo, lecz czy na pewno?
Pierwszy błąd popełniłem już na samym początku instalując system 64bitowy. Nie róbcie tego. 32 bity na takim czymś nie zrobi dużej różnicy, a ram będzie
zaoszczędzony.

No to lecimy.
Pierwsza rzecz: poprawiamy sources.list na USAńskie mirrory żeby złapać troche wiatru w żagle:

#~~~~~~Wheezy~~~~~~#
deb http://ftp.us.debian.org/debian/ wheezy main non-free contrib
deb-src http://ftp.us.debian.org/debian/ wheezy main non-free contrib
deb http://security.debian.org/ wheezy/updates main contrib non-free
deb-src http://security.debian.org/ wheezy/updates main contrib non-free
deb http://ftp.us.debian.org/debian/ wheezy-updates main non-free contrib
deb-src http://ftp.us.debian.org/debian/ wheezy-updates main non-free contrib

#~~~~Debian-multimedia~~~~#
deb http://www.deb-multimedia.org/ stable main non-free
deb-src http://www.deb-multimedia.org/ stable main non-free
Wywalamy niepotrzebne(tj. takie, z których nie będziemy korzystać) rzeczy takie jak MTA, nfsy, samby, bindy, sendmaile, eximy, fetchmaile, inne pierdoły.
I stawiamy co trzeba:

apt-get update && apt-get install aptitude && aptitude install vnstat zsh lighttpd mysql-server php5-cgi php5-mysql mosh

Aptitude jakoś przyjemniejszy dla mnie jest. ZSH żre pamięć, ale jak się do niego przyzwyczaiłem i do tego dopełniania to cięzko mi się odzwyczaić. Bez MOSHa nie potrafię żyć łącząc się po
3g/GSM. Niestety, pożera on troche ramu.
Pierwsza rzecz do zrobienia po instalacji MySQLa? Założenie mu kagańca, żeby się nie rozbestwił zbytnio.
Uruchamiamy:

root@smwh ~ # mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we’ll need the current
password for the root user. If you’ve just installed MySQL, and
you haven’t set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
You already have a root password set, so you can safely answer ‚n’.
Change the root password? [Y/n] n
… skipping.
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n]
… Success!
Normally, root should only be allowed to connect from ‚localhost’. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n]
… Success!
By default, MySQL comes with a database named ‚test’ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n]
- Dropping test database…
ERROR 1008 (HY000) at line 1: Can’t drop database ‚test’; database doesn’t exist
… Failed! Not critical, keep moving…
- Removing privileges on test database…
… Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n]
… Success!
Cleaning up…
All done! If you’ve completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!

Na początek wystarczy. Jeszcze tylko trochę ciaśniej zawiążemy ten kaganiec
/etc/mysql/my.cnf dopisujemy/poprawiamy:

[mysqld]
key_buffer = 16K
max_allowed_packet = 1M
table_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 64K
skip-bdb
skip-innodb

Są to wartości w miarę optymalne, które nie zabiją wydajnościowo mysql’a, a zarobimy mare mb wolnego ramu 😉
Konfiguracja lighttpd to sprawa dość indywiualna, dla tego pozostawię to w spokoju. Jedyne co polecam zrobić to zmiana wysyłanych headerów lighttpd(gdziekolwiek w
configu):

server.modules += ( "mod_setenv" )
setenv.add-response-header = ( "Server" => "Blabla" )
setenv.add-request-header = ( "Server" => "Blabla" )
# /etc/init.d/lighttpd force-reload

Instalacja WordPressa

..to nic nadzwyczajnego. Najpierw utworzymy bazę, z której będzie korzystał WordPress, następnie zrobimy mu osobnego użytkownika oraz przyznamy odpowiednie uprawnienia:

root@smwh ~ # mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 53
Server version: 5.5.35-0+wheezy1 (Debian)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‚help;’ or ‚h’ for help. Type ‚c’ to clear the current input statement.
mysql> CREATE USER ‚wordpress’@’localhost’ IDENTIFIED BY ‚HasłoUseraWordpress’;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE DATABASE wordpress
-> ;
Query OK, 1 row affected (0.00 sec)
mysql> GRANT ALL ON wordpress.* TO ‚wordpress’@’localhost’;
Query OK, 0 rows affected (0.00 sec)
mysql> Bye

Teraz nic innego jak przejśc do folderu roothtml, pobrać i rozpakować paczke wordpressa, i zainstalować. Dane wszystkie mamy 😉
Nie lubię dawać innym możliwości podglądania mojego ruchu idącego czystym tekstem. Lubię SSL, jednak ze względu na pośpiech nie będę tworzył zaufanego certyfikatu, lecz utworzę
tzw self-signed
, podpisany sam przez siebie. Oczywiście 2048 bitowy, szyfrowanie AESem 256.

root@smwh ~ # openssl genrsa -aes256 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
.........................+++
......................+++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
root@smwh ~ # openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‚.’, the field will be left blank.
—–
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:IDK
Locality Name (eg, city) []:WHY
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:
Please enter the following ‚extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
root@smwh ~ # cp server.key server.key.org
root@smwh ~ # openssl rsa -in server.key.org -out server.key
Enter pass phrase for server.key.org:
writing RSA key
root@smwh ~ # openssl x509 -req -days 1000 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=US/ST=IDK/L=WHY/O=Internet Widgits Pty Ltd
Getting Private key

Jeszcze tylko kwestia lekkiej modyfikacji configu lighttpd do akceptowania połączeń ssl:

$SERVER["socket"] == ":443" {
ssl.engine = "enable"
ssl.pemfile = "/etc/lighttpd/crt/server.pem"
ssl.ca-file = "/etc/lighttpd/crt/server.crt"
server.document-root = "/var/www/localhost"
}

Hm… co jeszcze możemy zrobić?
A jest kilka rzeczy …:
Zmienić shella na mniej zasobożernego

root@smwh ~ # aptitude install dash
...
root@smwh ~ # dpkg-reconfigure dash && /etc/init.d/mysql restart

Lżejszy syslog:

root@smwh ~ # aptitude install syslog-ng && dpkg --purge rsyslog

Zwiewny SSHD:

root@smwh ~ # touch /etc/ssh/sshd_not_to_be_run
root@smwh ~ # apt-get install dropbear
...
root@smwh ~ # nano /etc/default/dropbear
root@smwh ~ # /etc/init.d/dropbear start
Starting Dropbear SSH server: dropbear

Uwaga! Nie zapomnij ustawić:

NO_START=0

Efekty? U mnie powyższy setup pożera, łącząc się przez ssh, nie mosha, całe… 60MB ramu! Wydaje mi się, że jest to wręcz genialny wynik biorąc pod uwagę to, co
udało nam się osiągnąć 😉 Mam jeszcze do tego uruchomioną instancję znc…

128MB ram – tak mało, a tak wiele.

128MB ram – tak mało, a tak wiele.

128MB ram – tak mało, a tak wiele.

128MB ram – tak mało, a tak wiele.

128MB ram – tak mało, a tak wiele.

128MB ram – tak mało, a tak wiele.

128MB ram – tak mało, a tak wiele.

128MB ram – tak mało, a tak wiele.

Leave a Reply

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