📜 ⬆️ ⬇️

Convenient web server on Virtualbox

nginx
When you make simple websites on WordPress, then everything is simple with the web server, set yourself Xampp and work calmly.

But the moment comes when you start to engage in serious projects, and there already the configuration on the server is not quite normal and using Xampp is not very convenient, besides, I am allergic to Apache.

I wanted a good, live web server by the type of the dedicated server, but on my computer, at the same time, it was convenient to use locally.
')
What exactly do you want:


There is another option - just to raise everything in oneself, but there is a snag, if something breaks, the hard disk falls, or something else, then again everything will be too lazy to collect, and you can simply backup the virtual machine file, you also get an independent environment.

Therefore, I picked up debian on a virtual machine (virtualbox) and set up nginx + phpfpm in a couple of hours, but this is not just another way for the nginx + phpfpm combination, we make a ready-made solution of the xampp type.

Training



So, we have a freshly installed Debian 7 (wheezy). The virtual machine is named webserver. Debian wheezy is also installed on the local machine.

First of all, I would like to get rid of the dialog box and use ssh.

Turn off the virtual machine and write:

$ VBoxManage modifyvm "webserver" --natpf1 "guestssh,tcp,127.0.0.1,2222,10.0.2.15,22" 


At the same time we will forward the 80th port for the web server.

Ports less than 1024 can only open root (thanks to or10n ), so I just forwarded 8888 port and redirected from port 80 to 8888.

 $ VBoxManage modifyvm "webserver" --natpf1 "web,tcp,127.0.0.1,8888,10.0.2.15,80" # iptables -t nat -A OUTPUT -d 127.0.0.1 -p tcp --dport 80 -j REDIRECT --to 8888 # nano /etc/rc.local 


Paste before exit 0

 iptables -t nat -A OUTPUT -d 127.0.0.1 -p tcp --dport 80 -j REDIRECT --to 8888 


Here, instead of 10.0.2.15, there may be another ip, see the output of the ifconfig command on eth0.

We start the virtual machine in the background, it is very convenient, please note that we are launching under a normal user who has a webserver virtual machine.

 $ (vboxheadless -s webserver &) 


We press enter

Everything, we have forwarded 2222 port on 22 internal, 80 port will work, we will see later. We try to connect:

 # ssh root@127.0.0.1 -p 2222 


Install the packages:

 # apt-get install nginx php5 php5-fpm php5-mysql php5-gd php5-mcrypt mysql-server mysql-utilities 


Configure php-fpm.



Since here we don’t have to worry about security, so everything will work from one user www-data.
Also we will apply through the socket. I offer a ready config, in fact, I didn’t add anything special there, don’t forget to make backups.

 # mv /etc/php5/fpm/php-fpm.conf /etc/php5/fpm/php-fpm.conf.default # nano /etc/php5/fpm/php-fpm.conf 


Insert the following:

 [global] pid = /var/run/php5-fpm.pid error_log = /var/log/php5-fpm.log include = /etc/php5/fpm/pool.d/*.conf 


Here, again, the default, you can not do this at all, because This is the same config only without comments, but it's easier for me to orient in the config.

Now we need to prepare a pool that will handle our php requests. We give a config to the following form.

 # nano /etc/php5/fpm/pool.d/www.conf 


We insert the following

 [www] user = www-data group = www-data listen = /tmp/php5-fpm.sock pm = dynamic pm.max_children = 10 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 chdir = / 


The pm parameters are well described on other resources, so here I will not explain anything, just keep it ready.

It remains to correct php.ini. In fact, someone can arrange and default, it is not bad. But I still bring my own.

 # nano /etc/php5/fpm/php.ini 


The config is large and optional, so I paste pastebin.com/AAudu4sh here

Reboot php-fpm

 # service php5-fpm restart 


We configure nginx.



In general, there remains a problem with configs, too lazy to crawl to the server each time, add a config, reboot nginx. Therefore, assembled a universal config.
Then you can simply create a folder, upload files and work. No sooner said than done. Here is a ready nginx config:

 user www-data; worker_processes 4; pid /var/run/nginx.pid; events { worker_connections 768; # multi_accept on; } http { ## #   ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; #       nginx server_tokens off; include /etc/nginx/mime.types; default_type application/octet-stream; ## #   ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## #   ## gzip on; gzip_disable "msie6"; ## #    ## include /etc/nginx/conf.d/*.conf; server { server_name phpmyadmin.l; listen 80; root /web/utils/phpmyadmin.l; index index.php index.html index.htm; access_log /web/access.log; error_log /web/error.log; location / { try_files $uri $uri/ /index.php?q=$uri&$args; } location ~* ^.+.(js|css|png|jpg|jpeg|gif|ico)$ { access_log off; expires max; } location ~ \.php$ { # fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_index index.php; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_intercept_errors off; fastcgi_ignore_client_abort off; fastcgi_connect_timeout 60; fastcgi_send_timeout 180; fastcgi_read_timeout 180; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; } } server { server_name ~^(.*)$; listen 80; set $p $host; if ($host ~ www\.(.*)) { set $p $1; } root /web/sites/$p; index index.php index.html index.htm; access_log /web/access.log; error_log /web/error.log; location / { try_files $uri $uri/ /index.php?q=$uri&$args; } location ~* ^.+.(js|css|png|jpg|jpeg|gif|ico)$ { access_log off; expires max; } location ~ \.php$ { # fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_index index.php; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_param QUERY_STRING $query_string; fastcgi_param SERVER_NAME $p; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_intercept_errors off; fastcgi_ignore_client_abort off; fastcgi_connect_timeout 60; fastcgi_send_timeout 180; fastcgi_read_timeout 180; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; } location = /favicon.ico { log_not_found off; access_log off; } } } 


 # mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.default # nano /etc/nginx/nginx.conf 


We insert the above config

I do not pretend that the config is good and perfect, but it works for me and there are no problems yet. I would be very happy if there are people who have something to add here.

All right, it remains only to set up a directory. I will explain a little how everything will happen here.



There will also be logs, access and error.

Create folders

 # mkdir /web && mkdir /web/sites && mkdir /web/utils 


We set the rights

 # chmod -R a-rwx,u+rwX,g+rX /web && chown www-data:www-data -R /web 


Reboot nginx.

 # service nginx restart 


It remains to put phpmyadmin.

On the server we execute

 # su www-data # cd /web/utils/phpmyadmin.l && wget http://jaist.dl.sourceforge.net/project/phpmyadmin/phpMyAdmin/4.0.4.2/phpMyAdmin-4.0.4.2-all-languages.zip && unzip phpMyAdmin-4.0.4.2-all-languages.zip && mv phpMyAdmin-4.0.4.2-all-languages/* ./ && rm -r phpMyAdmin-4.0.4.2-all-languages/ && rm phpMyAdmin-4.0.4.2-all-languages.zip # exit 


That's it, phpmyadmin is ready for work and defense. You can now find it at phpmyadmin.l

So, the web server is ready for us, it remains to make it start when it is loaded, it is saved when it is turned off and on restart, and there is no status window.

I will describe the strategy a bit, after the virtual machine boots up, we use sshfs to keep the directory of sites, and I made it an automatic machine.

I'm also too lazy to edit the / etc / hosts file each time another site is added, so we automate all of this.

Automation



We do authorization by key

On the server you need:

Stop the nginx and php5-fpm daemons, and install the home directory for the www-data user.

 # service nginx stop && service php5-fpm stop # mkdir /home/www-data && usermod -d /home/www-data www-data # service nginx start && service php5-fpm start 


Generate the root key, and then place the public key in /home/www-data/.ssh/authorized_keys

On the client, we check:

 # ssh www-data@127.0.0.1 -p 2222 


If you log in without a password, then everything is fine, if not, then you did something wrong with the keys.

Now it remains to create 2 scripts.

1) The script checks the availability of the virtual machine and automatic mount, as well as the generation of the file / etc / hosts.

 # nano /usr/bin/webserver 


Insert the following code

 #!/bin/bash sleep=60 #  when_mount='/mnt/webserver' #       directories=`ls -p $when_mount | grep "/" | sed 's/\///g'` while true; do if ! $(mount | grep "$when_mount" > /dev/null); then if [ "$(nmap -p 2222 -sT 127.0.0.1 | awk '{print $2}' | grep open)" = "open" ]; then sshfs -o allow_other -o port=2222 www-data@127.0.0.1:/web/sites "$when_mount" fi; elif ! [ "$directories" = $(ls -p $when_mount | grep "/" | sed 's/\///g') ]; then hosts=$(grep -v "127.0.0.1" /etc/hosts) directories=$(ls -p $when_mount | grep "/" | sed 's/\///g') if ! [ "$directories" = "" ] then echo "$hosts" > /etc/hosts echo "127.0.0.1 localhost phpmyadmin.l "$directories >> /etc/hosts fi; fi sleep "$sleep" done 


I do not pretend to the correctness of the code, and I will be glad to anyone who finds bugs there. But it works for me regularly.
The convenience of the script is that even if you restart the virtual machine, nothing will break and everything will be fine.

Now make it executable

 # chmod -x /usr/bin/webserver 


2) A small script that will start and stop the web server.

 # nano /etc/init.d/webserverd 


Insert the following code

 #!/bin/bash case "$1" in start) echo -n "Starting webserver: " (su - darkrain -c 'vboxheadless -s webserver' &) (webserver &) ;; stop) echo -n "Stopping webserver: " (su - darkrain -c 'VBoxManage controlvm webserver savestate' &) killall webserver ;; esac exit 0 


darkrain - replace with your user, since The created machine is subordinate to him, not to the root.
Again, I'm not a pro here, I would appreciate if they correct me, but it works.

We set the rights, and add to the list of services

 # chmod -x /etc/init.d/webserverd && update-rc.d webserverd defaults 


It remains to create a directory where our sites will be visible.

 # mkdir /mnt/webserver && chmod 777 /mnt/webserver 


Restart our server

 # service webserverd stop # service webserverd start 


Everything, now everyone is happy and satisfied, we create for the test:

 $ mkdir /mnt/webserver/testsite.ru.l && echo "<?php phpinfo();" > /mnt/webserver/testsite.ru.l/index.php 

We wait 60 seconds, and go to the browser at testsite.ru.l

All right, we are done! You can forget about the fact that we have a virtual machine in general, unless of course you need to install some kind of lib or something else you may need.

Now, in order to work with the new site, I will simply create a folder with the domain name in / mnt / webserver, and will simply work without being distracted.

PS

Here it is described more or less clearly about php-fpm, about pools and other manualpages.pro/node/31

UPD.
Whoever doesn’t like my bike can use Vagrant (thanks to zvirusz ). References:


The rest of the information was taken bit by bit from Google.

Thank you very much to someone who gave me an invite! Yes, unfortunately I did not understand who it is.
Thank you UFO for invite!

Source: https://habr.com/ru/post/188702/


All Articles