I constantly forget how to do this. I decided to ask a question here so that I could find it later.

How do i do:

sudo gedit / etc / hosts

I add a line in the opened file

127.0.0.1 test.loc www.test.loc

we save, further:

sudo gedit /etc/apache2/conf.d/custom/vhosts

add the following

<VirtualHost *:80>ServerName test.locServerAlias test.locDocumentRoot "/home/www-data/www/test/www"ScriptAlias /cgi/ "/home/www-data/www/test/cgi-bin/"ErrorLog /home/www-data/www/test/error.logCustomLog /home/www-data/www/test/access.log common</VirtualHost> 

I create in "/ home / www-data / www" "/ home / www-data / www / test / www" with 777 rights as a result of typing in the browser http: //www.test.loc/ I have to go to the page

/home/www-data/www/test/www/index.php

but apparently I'm missing something, because another page opens.

Here is what I had in vhosts:

 <VirtualHost *:80>ServerName test.locServerAlias test.locDocumentRoot "/home/www-data/www/test/www"ScriptAlias /cgi/ "/home/www-data/www/test/cgi-bin/"ErrorLog /home/www-data/www/test/error.logCustomLog /home/www-data/www/test/access.log common</VirtualHost> 

and hosts:

 127.0.0.1 localhost127.0.1.1 alexander-A17127.0.0.1 test.loc www.test.loc 

Everything that does not work is redirected to skaz-gorod.loc (the address does not change in the address bar), what am I doing wrong?

of the www-data user added a user to the group (which is not safe but I'm not afraid :)) I finished the file with:

That's what I got in vhosts:

 <VirtualHost 127.0.0.5>ServerName test.locServerAlias test.locDocumentRoot "/home/www-data/www/test/www"ScriptAlias /cgi/ "/home/www-data/www/test/cgi-bin/"ErrorLog /home/www-data/www/test/error.logCustomLog /home/www-data/www/test/access.log common</VirtualHost> 

and hosts:

 127.0.0.1 localhost127.0.1.1 alexander-A17127.0.0.5 test.loc www.test.loc 

after the site opened with an error:

 Internal Server ErrorThe server encountered an internal error or misconfiguration and was 

unable to complete your request.

 Please contact the server administrator, [no address given] and 

You couldn’t have caused any error.

 More information about this error may be available in the server error 

log.

 Apache/2.2.22 (Ubuntu) Server at 127.0.0.5 Port 80 

and in the logs wrote:

 [alert] [client 127.0.0.1] /home/www-data/www/test/www/.htaccess: 

Invalid command 'RewriteEngine', perhaps misspelled or not defined in the serverconfiguration

The error says that the Apache rewrite module is not enabled.

It is solved simply:

sudo a2enmod rewrite

sudo /etc/init.d/apache2 restart

URA EARNED!

1 answer 1

For EVERY virt.host

 1. sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site_name2. sudo gedit (nano, vi, etc. - нужное подчеркнуть) /etc/apache2/sites-available/site_name<VirtualHost *:80>ServerAdmin admin@mail.local #почта админа#то, что увидят посетителиDocumentRoot /home/user/sites/site_name/www<Directory /> Options FollowSymLinks AllowOverride None</Directory>#то, что НЕ увидят посетители (например логи сайта) и права на папку<Directory /home/user/sites/site_name/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all</Directory>ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/<Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all</Directory>#лог ошибок сайтаErrorLog /home/user/sites/site_name/logs/error.log# Possible values include: debug, info, notice, warn, error, crit,# alert, emerg.LogLevel warn#лог доступа сайтаCustomLog /home/user/sites/site_name/access.log combinedAlias /doc/ "/usr/share/doc/"<Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128</Directory> 

</ Virtualhost>

 4. sudo a2dissite default #отключить дефолтный сайт5. sudo a2ensite site_name #подключить свой сайт6. sudo /etc/init.d/apache2 restart #рестарт апача для полноты ощущений;)7. прописАть IP сайта на DNS-сервере (если локально - то файл /etc/hots привести к виду типа "127.0.0.1 localhost site_name www.site_name") 

Taken HERE HERE HERE

Oh, yes .... I almost forgot the most interesting ... the user www-data should have full rights to the folder / home / user / sites / site_name / ( for this particular example of a virtual host). In general, the folder with the site can be put anywhere, the main thing is that at the time Apace2 was launched it was available at the registered address.

UPD

For the sake of completeness, the name-based virtual host can (or should) be checked in ports.conf for presence and uncommenting

 NameVirtualHost *:80Listen 80 

and in the same place to prescribe something like

 <VirtualHost *:80> DocumentRoot /home/user/sites/site_name/www ServerName site_name</VirtualHost><VirtualHost *:80> DocumentRoot /home/user/sites/site_name-1/www ServerName site_name-1</VirtualHost> 

by block for each site (here - site_name and site_name-1 ). And you can host as many sites on a single IP as the server and channel pull;)

  • A small addition: I would still leave the address 127.0.0.1 alone, and place my websites at the addresses following it (127.0.0.2, 127.0.0.3). - Justicet