MultiformeIngegno

  • Posts: 1,337
Help with apache2
« on June 1st, 2012, 02:08 AM »
I'm setting up a (remote) virtual server with Ubuntu Server 12.04. I installed apache2, php5, mysql, etc..
I need to host several websites on this server, that has just 1 IP. So I need to create virtual hosts. I read Apache documentation and many posts here and there..

The problem is that I get the warning "NameVirtualHost *:80 has no VirtualHosts" every time I restart Apache.

These are the files I have:

etc/apache2/apache2.conf
Code: [Select]
#
# Based upon the NCSA server configuration files originally by Rob McCool.
#
# This is the main Apache server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See http://httpd.apache.org/docs/2.2/ for detailed information about
# the directives.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure
# consult the online docs. You have been warned. 
#
# The configuration directives are grouped into three basic sections:
#  1. Directives that control the operation of the Apache server process as a
#     whole (the 'global environment').
#  2. Directives that define the parameters of the 'main' or 'default' server,
#     which responds to requests that aren't handled by a virtual host.
#     These directives also provide default values for the settings
#     of all virtual hosts.
#  3. Settings for virtual hosts, which allow Web requests to be sent to
#     different IP addresses or hostnames and have them handled by the
#     same Apache server process.
#
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files begin with "/" (or "drive:/" for Win32), the
# server will use that explicit path.  If the filenames do *not* begin
# with "/", the value of ServerRoot is prepended -- so "foo.log"
# with ServerRoot set to "/etc/apache2" will be interpreted by the
# server as "/etc/apache2/foo.log".
#

### Section 1: Global Environment
#
# The directives in this section affect the overall operation of Apache,
# such as the number of concurrent requests it can handle or where it
# can find its configuration files.
#

#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# NOTE!  If you intend to place this on an NFS (or otherwise network)
# mounted filesystem then please read the LockFile documentation (available
# at <URL:http://httpd.apache.org/docs/2.2/mod/mpm_common.html#lockfile>);
# you will save yourself a lot of trouble.
#
# Do NOT add a slash at the end of the directory path.
#
#ServerRoot "/etc/apache2"

#
# The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
#
LockFile ${APACHE_LOCK_DIR}/accept.lock

#
# PidFile: The file in which the server should record its process
# identification number when it starts.
# This needs to be set in /etc/apache2/envvars
#
PidFile ${APACHE_PID_FILE}

#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 300

#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On

#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 5

##
## Server-Pool Size Regulation (MPM specific)
##

# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>

# worker MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadLimit: ThreadsPerChild can be changed to this maximum value during a
#              graceful restart. ThreadLimit can only be changed by stopping
#              and starting Apache.
# ThreadsPerChild: constant number of worker threads in each server process
# MaxClients: maximum number of simultaneous client connections
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_worker_module>
    StartServers          2
    MinSpareThreads      25
    MaxSpareThreads      75
    ThreadLimit          64
    ThreadsPerChild      25
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>

# event MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxClients: maximum number of simultaneous client connections
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_event_module>
    StartServers          2
    MinSpareThreads      25
    MaxSpareThreads      75
    ThreadLimit          64
    ThreadsPerChild      25
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>

# These need to be set in /etc/apache2/envvars
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

#
# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives.  See also the AllowOverride
# directive.
#

AccessFileName .htaccess

#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy all
</Files>

#
# DefaultType is the default MIME type the server will use for a document
# if it cannot otherwise determine one, such as from filename extensions.
# If your server contains mostly text or HTML documents, "text/plain" is
# a good value.  If most of your content is binary, such as applications
# or images, you may want to use "application/octet-stream" instead to
# keep browsers from trying to display binary files as though they are
# text.
#
# It is also possible to omit any default MIME type and let the
# client's browser guess an appropriate action instead. Typically the
# browser will decide based on the file's extension then. In cases
# where no good assumption can be made, letting the default MIME type
# unset is suggested  instead of forcing the browser to accept
# incorrect  metadata.
#
DefaultType None


#
# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., [url=http://www.apache.org]www.apache.org[/url] (on) or 204.62.129.132 (off).
# The default is off because it'd be overall better for the net if people
# had to knowingly turn this feature on, since enabling it means that
# each client request will result in AT LEAST one lookup request to the
# nameserver.
#
HostnameLookups Off

# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog ${APACHE_LOG_DIR}/error.log

#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel warn

# Include module configuration:
Include mods-enabled/*.load
Include mods-enabled/*.conf

# Include all the user configurations:
Include httpd.conf

# Include ports listing
Include ports.conf

#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
# If you are behind a reverse proxy, you might want to change %h into %{X-Forwarded-For}i
#
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

# Include of directories ignores editors' and dpkg's backup files,
# see README.Debian for details.

# Include generic snippets of statements
Include conf.d/

# Include the virtual host configurations:
Include sites-enabled/

ServerSignature Off
ServerTokens Prod

httpd.conf is blank

ports.conf:
Code: [Select]
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default
# This is also true if you have upgraded from before 2.2.9-3 (i.e. from
# Debian etch). See /usr/share/doc/apache2.2-common/NEWS.Debian.gz and
# README.Debian.gz

NameVirtualHost *:80
Listen 80

<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

/etc/apache2/sites-enabled/000-default
Code: [Select]
<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /home/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
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 ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /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>

/etc/apache2/sites-available/default:
same as /etc/apache2/sites-enabled/000-default

/etc/apache2/sites-available/www.example.com:
Code: [Select]
#
#  Example.com (/etc/apache2/sites-available/www.example.com)
#
<VirtualHost *>
        ServerAdmin webmaster@example.com
        ServerName  [url=http://www.example.com]www.example.com[/url]
        ServerAlias example.com

        # Indexes + Directory Root.
        DirectoryIndex index.html
        DocumentRoot /home/www/www.example.com/htdocs/

        # CGI Directory
        ScriptAlias /cgi-bin/ /home/www/www.example.com/cgi-bin/
        <Location /cgi-bin>
                Options +ExecCGI
        </Location>


        # Logfiles
        ErrorLog  /home/www/www.example.com/logs/error.log
        CustomLog /home/www/www.example.com/logs/access.log combined
</VirtualHost>

/etc/apache2/sites-available/www.example.net:
same as example.com with dirs changed

/etc/apache2/sites-available/www.example.org:
same as example.com with dirs changed

Of course I enabled the sites with the a2ensite command.

Do you know what can cause the problem? :o

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Help with apache2
« Reply #1, on June 1st, 2012, 02:15 AM »
Yup. All the virtual hosts are defined as being * but without any port designation, while you clearly state that Named Virtual Hosts should use *:80 in ports.conf.

(And yes, this is Apache 2, httpd.conf is deprecated in favour of apache2.conf and lots of sub configuration files)

It actually shouldn't matter, Apache is bound to port 80 through the Listen directive so it should just be a case of Apache complaining but still actually serving files correctly.
When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest. | Game Memorial

MultiformeIngegno

  • Posts: 1,337
Re: Help with apache2
« Reply #2, on June 1st, 2012, 02:25 AM »
You're right! ;)
I changed all websites to *:80 and the error disappeared. :D

Now it's time to test if the virtual hosts actually work. So I have 3 available websites (let's say example.com/org/net), I just need to point their A records to the server IP? I think I need to do something with the /etc/host.conf file too.. currently it has only these lines:

Code: [Select]
# The "order" line is only used by old versions of the C library.
order hosts,bind
multi on

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Help with apache2
« Reply #3, on June 1st, 2012, 02:28 AM »
Yup, you just point all the records at the server and let the HTTP request indicate which one it wants.

You don't need to change /etc/host.conf (I think you mean /etc/hosts), you just need to make sure whatever you're using for external DNS points to the right server, and that each of your virtual hosts has the proper ServerName defined like yours does.

What happens is that each request will come in with the Host header set as part of the request, Apache will look through all the name based virtual hosts for any server whose ServerName matches the Host header as indicated, and will work with that one if it's matched.

If no matches are found, I forget exactly what Apache does but I believe it goes to the last one defined as a fallback.


(Interesting: the link still gets processed even inside a code tag >_<)

MultiformeIngegno

  • Posts: 1,337
Re: Help with apache2
« Reply #4, on June 1st, 2012, 02:34 AM »
Seems magic! :D
So it doesn't matter the name of the "available-website", but just the one specified in "ServerName"? I could have the website named sadsadsasad.com but the ServerName set to the right domain?
ServerAlias takes care of the www and non-www version, right? :)

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Help with apache2
« Reply #5, on June 1st, 2012, 02:36 AM »
Correct. The name in available-sites (at least that's the default Apache folder name) is only an internal name for your reference.

ServerName is the directive that tells Apache what to look for and ServerAlias is for declaring alternative variations of the same handler, exactly as you indicate - you can declare ServerName example.com and ServerAlias www.example.com and Apache will match either to find that host. ServerName must be declared once per host, but you can have as many aliases as you want.

PantsManUK

  • [me=PantsManUK]would dearly love to dump SMF 1.X at this juncture...[/me]
  • Posts: 174
Re: Help with apache2
« Reply #6, on June 1st, 2012, 11:47 AM »
A little late to the party, and a little OT. I applaud you using 12.04 (guessing this was a new VPS/VM), but as a warning to existing LTS users I wouldn't recommend upgrading to it (yet). It's very "unstable" at present; *lots* of updates... Needs a few months to settle down then it'll be good to dist-upgrade to.
« What is this thing you hoomans call "Facebook"? »

MultiformeIngegno

  • Posts: 1,337
Re: Help with apache2
« Reply #7, on June 1st, 2012, 11:48 AM »
Great! Everything works fine! My website loads from my virtual server! :D
Now that this new available-site works, what happens if I delete the default one? What would be displayed if I try to reach my IP?

Are there any security tecniques to adopt (apart from permissions) or configurations to make to improve security?
So far I just added these lines to apache.conf (dunno if are of any help..):
Code: [Select]
ServerSignature Off
ServerTokens Prod

Dragooon

  • I can code! Really!
  • polygon.com has to be one of the best sites I've seen recently.
  • Posts: 1,841
Re: Help with apache2
« Reply #8, on June 1st, 2012, 01:01 PM »
There are a couple of things you can do, one is run every site in their own user/group using suPHP/PHPSuExec/PHP-FPM (one of these, fpm being recommended). Apart from that you can install php suhosin and enable open_basedir (even though this shouldn't be required if you got user/group properly configured, but never hurts). There are a few other things that I'm probably forgetting, but search around.
The way it's meant to be

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Help with apache2
« Reply #9, on June 1st, 2012, 01:20 PM »
I'll agree with FPM (especially as it means you can run nginx instead of Apache), but I can't in good conscience recommend suhosin. The number of times I've found software randomly break for no apparent reason just because of suhosin, I find it's more effort than it's worth.

Groups/users is sort of irrelevant if you have software that can update itself through the admin panel since that requires making the software writable by the web server user. (Yeah, I know that suPHP and PHPSuExec are supposed to do this but rarely seem to work properly in that respect)

Dragooon

  • I can code! Really!
  • polygon.com has to be one of the best sites I've seen recently.
  • Posts: 1,841
Re: Help with apache2
« Reply #10, on June 1st, 2012, 01:36 PM »
I haven't really had a problem with suhosin, but to each it's own. Group/user is more for preventing a site from accessing other site (so one site's weak site weak security doesn't result in all of the sites getting hacked). Plus FPM will make the instance of apache run in that user/group so it doesn't need to be writable by nobody, but only the user of the site itself.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Help with apache2
« Reply #11, on June 1st, 2012, 01:43 PM »
Wait... how will FPM make Apache run in that user/group? FPM is what you use to run a pool of PHP processes that nginx can use...

(I've not used FPM with Apache. But the design of FPM is fundamentally stronger than doing it any other way because things aren't just shoved at FPM the way they are with other routes)

Dragooon

  • I can code! Really!
  • polygon.com has to be one of the best sites I've seen recently.
  • Posts: 1,841
Re: Help with apache2
« Reply #12, on June 1st, 2012, 01:58 PM »
Slightly incorrect wording there, FPM doesn't make Apache run in that user/group but it does make apache's user/group irrelevant. From what I understand with FPM Apache/Nginx just routes the request to FPM/FCGI and then FPM processes the request. At that point Apache's user/group doesn't matter anymore since FPM/FCGI is running the PHP process, so the scripts are executed with FPM's user/group. And FPM has the ability to have different user/group for different ports/sites. Hence, once can change the user/group php runs in and prevent those files from accessing files of other user/group, just make sure you don't have everything 777.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Help with apache2
« Reply #13, on June 1st, 2012, 02:00 PM »
Hmm, I suppose that makes sense. Need to check on that, though, because I didn't think FPM did the whole different user thing.

Dragooon

  • I can code! Really!
  • polygon.com has to be one of the best sites I've seen recently.
  • Posts: 1,841
Re: Help with apache2
« Reply #14, on June 1st, 2012, 02:10 PM »
Quote from Arantor on June 1st, 2012, 02:00 PM
Hmm, I suppose that makes sense. Need to check on that, though, because I didn't think FPM did the whole different user thing.
I've been using that setup as an alternative to suExec/PHPSuExec (since both haven't received any updates in years) in my new server. Plus [Unknown] recommended a nice permission setup which IMO is fairly logical and I have been using it for well almost a year. Basically, make all the web server files chown'd to site-user:site-user and make the web process run as site-user-www:site-user (user being different and group being same). Then chmod all files which are primarily read only to 640 and the folders which require write to 750/770 (don't remember properly, should be 750), and make sure that these folders cannot execute PHP scripts. That way, all the other user's are completely locked out, and at the same time you don't sacrifice complete access to the files but still lock PHP out enough to maintain control. Plus if someone gets in, they cannot modify a PHP file or run a script and hence run it securely.