Apache: Serving content from two different locations and ports - apache

I have an Apache server running on Ubuntu 14 on a server on which I have sudo.
When users request the server's IP, Apache serves content from /var/www/html.
I'd like to keep this behavior intact, and make it so that users who request IP/cats get some special content that's hosted by a Docker container on port 7777.
What's the best way to achieve this functionality in Apache?

With thanks to #arkascha I did the following to get this going:
Create a file /etc/apache2/sites-available/wow.conf with the following content:
<VirtualHost *:*>
# enable proxies
ProxyPreserveHost On
ProxyPass /cats http://0.0.0.0:7777/
ProxyPassReverse /cats http://0.0.0.0:7777/
ServerAdmin douglas.duhaime#yale.edu
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
This says when requests come in for IP/cats, serve the users the content from port 7777.
I then symlinked this file to the sites-enabled directory:
sudo ln /etc/apache2/sites-available/wow.conf /etc/apache2/sites-enabled/wow.conf
Finally, I needed to delete the default sites enabled file and restart the server:
sudo rm /etc/apache2/sites-enabled/000-default.conf
sudo service apache2 restart
P.S. Apache is amazing.

Related

Apache2 keeps redirecting to default page

I've installed some software for users to be able to change their ldap password on they own named self service password on this path: /usr/share/self-service-password/htdocs.
In sites-enabled my configuration is as follows: (file named 000-default.conf)
<VirtualHost *:80>
ServerName <IP>
DocumentRoot /usr/share/self-service-password/htdocs
DirectoryIndex index.php
AddDefaultCharset UTF-8
LogLevel warn
ErrorLog /var/log/apache2/ssp_error.log
CustomLog /var/log/apache2/ssp_access.log combined
</VirtualHost>
(same thing is "configured" at sites-available").
I've made sure this .conf file (000-default) is enabled by a2ensite, and made sure everything else is disabled, and restarted apache2.
However, When browsing to my IP:81 , it keeps redirecting me to Apache default welcome page under /var/www/html .
I'm trying to find a solution for it for the past 2 hours and couldn't find the reason behind it.
Thanks for anyone willing to assist.
As per your configuration your VirtualHost is accepting connections on port *:80
<VirtualHost *:80>
But then you are testing on IP:81.
Could you please adjust your test to IP:80
or
Adjust the VirtualHost port to *:81
They must match to work.

Host Multiple Domains on One Server/IP

I have phpList already setup and Codeigniter project that I want to add in one server.
The phpList configuration which is located /etc/httpd/conf.d/phplist.conf has:
<VirtualHost *:80>
ServerName phplist.example.com
ServerAdmin admin#example.com
DocumentRoot /var/www/phplist-3.0.5/public_html
LogLevel warn
ErrorLog /var/log/httpd/phplist_error.log
CustomLog /var/log/httpd/phplist_access.log combined
</VirtualHost>
Here is my possible setting for the CI project, but I don't know how to create one.
<VirtualHost *:80>
ServerName listcsv.com
ServerAdmin admin#listcsv.com
DocumentRoot /var/www/citest/index.php
</VirtualHost>
Below the /etc/httpd/conf/httpd.conf file has a IncludeOptional conf.d/*.conf so I think this would include the configuration file on phplist.
I don't know what settings and where configuration file should I go,
should I edit on /etc/httpd/conf/httpd.conf or just add it together with the phpList configuration at /etc/httpd/conf.d/phplist.conf?
I tried accessing phplist at http://phplist.example.com but the site cannot be reached.
I could access the phpList by going to http://server-ip-address/lists/admin on the browser.
Also, I cannot access the CI project.
I would recommend creating the directory that the virtual hosts will be stored in, as well as the directory that tells Apache that a virtual host is ready to serve the website
sudo mkdir /etc/httpd/sites-available # vhost files
sudo mkdir /etc/httpd/sites-enabled # symbolic links for vhost enabled
Tell Apache where to look for virtual hosts
sudo vim /etc/httpd/conf/httpd.conf
Add this line at the end of the file
IncludeOptional sites-enabled/*.conf
Save and exit.
For each website/domain create its own vhost
sudo vim /etc/httpd/sites-available/example1.com.conf
sudo vim /etc/httpd/sites-available/example2.com.conf
sudo vim /etc/httpd/sites-available/example3.com.conf
Enable the virtualhost
sudo ln -s /etc/httpd/sites-available/example1.com.conf /etc/httpd/sites-enabled/example1.com.conf
sudo ln -s /etc/httpd/sites-available/example2.com.conf /etc/httpd/sites-enabled/example2.com.conf
sudo ln -s /etc/httpd/sites-available/example3.com.conf /etc/httpd/sites-enabled/example3.com.conf
We done, restart your Apache server for the changes to take affect
sudo apachectl restart
Don't forget to point your domains to the web server's public IP address.

Why I see the content of my localhost instead of accessing my created virtual host?

I'm working on Ubuntu-14 and I want to access http://localhost:8086/myproject.net using http://myproject.dev
I created a new folder under "/var/www/" named "myproject.net"
I added this line to "/etc/hosts"
127.0.0.1 myproject.dev
I created "myproject.dev.conf" file under "/etc/apache2/sites-available" which contains:
<VirtualHost *:86>
ServerAdmin i-put-my-email-here
ServerName myproject.dev
ServerAlias www.myproject.dev
DocumentRoot /var/www/myproject.net/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I enabled my site using
sudo a2ensite myproject.dev.conf
I restarted Apache
sudo service apache2 restart
I verified that Apache is running using
sudo service apache2 status
Now when I tape http://myproject.dev:8086 I see the content of my localhost (like when I do http://localhost:8086) not the content of myproject!
Am I missing a step or Am I doing something wrong?
You have to take care to define your virtual host on the correct address and port so that it can be matched for an incoming request. Otherwise the default host will be selected to respond to the request.
In your specific example you have to change the definition from
<VirtualHost *:86>
to use the port your http server is obviously listening on and that you make the request to:
<VirtualHost *:8086>
Oh, and... don't forget to restart your http server process after such a change ;-)

apache2 VirtualHost on a new port not working

I just installed LAMP on my Ubuntu machine, and it works fine when I access it. I want to add a virtual host on another port, port 1337, that goes to the directory /var/www/flag-1/. In order to do this, I take the following steps:
cd /etc/apache2/sites-available/
Created a file flag-1.conf
Added contents:
<VirtualHost *:1337>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/flag-1/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
At the top of /etc/apache2/ports.conf, after Listen 80, added Listen 1337
Enabled the vhost site by doing a2ensite flag-1.conf
Reloaded apache service apache2 restart
When I access the site with port 1337, it just loads indefinitely. The default port still works fine, and I don't have UFW enabled. No errors, nothing in access.log or error.log is outstanding.
Any help is appreciated. Thanks!
Figured it out -- I was using Google Cloud Platform and they blocked port 1337, I just had to manually allow TCP through it.
If you're wondering, the command was
gcloud compute firewall-rules create allow-port-1337 --allow tcp:1337 --description="Allow port 1337 to be accessed"

How do you set the default website to serve when your IP address is entered as URL?

I have a server with multiple websites hosted and distinguishable using name-based virtual hosting of apache.
How do I set it so that a specific website is hosted when the ip of my server is entered in the address bar?
What you want to use is the _default_ VirtualHost.
<VirtualHost _default_:80>
DocumentRoot /www/default80
# ...
</VirtualHost>
It's described here. Basically if nothing else match the request the _default_ host will be used.
EDIT
This could also be written as:
<VirtualHost *>
DocumentRoot /www/default
# ...
</VirtualHost>
Is is important that this is the first VirtualHost in the configuration since Apache will start matching them from top to bottom, selecting the one that fit the best based on ServerName and ServerAlias.
This post might also be of interest:
Apache default VirtualHost
just find the Include sites-enabled/ line in your apache2.conf file and add the path to the conf file you want to be site default above it. from:
Include sites-enabled/
to
Include sites-enabled/mydefault.conf
Include sites-enabled/
When you first install apache2, there is a site configuration file named 000-default.conf. This is going to be the default because it is very likely to appear first in the list of files under /etc/apache2/sites-enabled.
To have your own file as the default, you can either replace the file under /etc/apache2/sites-available/000-default.conf with your own, or replace the link like so:
sudo rm /etc/apache2/sites-enabled/000-default.conf
sudo ln -s ../sites-available/my-site-setup.conf /etc/apache2/sites-enabled/000-default.conf
Then restart apache2 (or just reload).
The _default_ as mentioned by the other answer is for defining a virtual host which can be found with the default IP address. It's not the default virtual host.
<VirtualHost _default_:80>
...
is equivalent to
<VirtualHost *:80>
...
The * is a globing pattern which matches any IP addresses.
Note:
Replacing the 000-default.conf file is okay, but in most cases the installation package is going to view that as a modified file and manage it in some weird way which is why I think it's cleaner to only change the soft link.
Keep it clean, don't delete or edit anything in /etc/apache2/sites-available/.
Create all new site configurations in /etc/apache2/sites-available/. Copy whatever site configuration you want enabled to /etc/apache2/sites-enabled/. Only make sure /etc/apache2/sites-enabled/ has only one configuration file.
Sample format for new apache site configurations in Ubuntu 20.04 LTS is
<VirtualHost *:80>
ServerName http://localhost
ServerAdmin admin#mysite.com
DocumentRoot /var/www/html/mysiteroot/public
<Directory /var/www/html/mysiteroot>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Notice that 000-default.conf is by default in both directories mentioned above and should only be replaced by your new configuration in /etc/apache2/sites-enabled/ so that it can be restored anytime you need it.
Restart Apache2 service after you make any configuration changes.