Domain and subdomains under one IP - google-cloud-platform

I have a goal to host few websites as subdomains.(Apache2 at Ubuntu 18.04 at Google Cloud Platform)
Directories:
/var/www/domain.com/public_html- main directory for real registered domain
my subdomains
/var/www/test1.domain.com/public_html
/var/www/test2.domain.com/public_html
/var/www/test3.domain.com/public_html
Access to subdomains by address test(1,2,3).domain.com
I'm new with Apache2 so it's possibe I want something unreal or use Apache2 in wrong way. If so could you show my mistakes?
I tried to configure Virtual hosts but unsuccessful.
Maybe it is possible make it via htaccess?
domain.com.conf
<VirtualHost *:80>
ServerAdmin admin#mail.com
DocumentRoot /var/www/domain.com/public_html
ServerName domain.com
ServerAlias www.domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
test1.domain.com.conf
<VirtualHost *:80>
ServerAdmin admin#mail.com
DocumentRoot /var/www/test1.domain.com/public_html
ServerName test1.domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The trouble was not in Apache configuration. It was correct.
If you will have the same goal as mine, first of all you need
create subdomains at your domain registrar
create new A records under main domain in cloud DNS for each subdomain(Google Cloud Platform in my case)
wait (half hour in my case)
And only then configure Apache webserver.

Related

how to host multiple virtual hosts from same IP address in Apache server over AWS?

I want to host two different angular panels over Apache (AWS). So I disabled the default 000-default.conf file in etc/apache2/sites-available using a2dissite 000-default.conf and made two files webAdmin.conf and webUIpanel.conf. Now my webAdmin.conf has the following configuration
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/AngularProjects/getItHomeNow_UI/dist
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
and webUIpanel.conf has following
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/AngularProjects/getItHomeNow_UserPanel/dist
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Then I used a2ensite to enable both config files and at last sudo systemctl restart apache2. But one of them working at a time. I need to disable one to make other work. Kindly suggest me where did I go wrong?
You should add ServerName in your VirtualHost config so Apache can decide which site should get the request. So to apply that to your config it should look like:
webAdmin.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/AngularProjects/getItHomeNow_UI/dist
ServerName www.example1.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
and webUIpanel.conf has following
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/AngularProjects/getItHomeNow_UserPanel/dist
ServerName www.example2.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
NOTE: Both ServerName should be valid domains so they resolve to your server or use your /etc/hosts file to point them to your server IP.

How to redirect google VM external ip address to HTTPS in django with Debial and Apache?

I have successfully installed SSL certificate with certbot and lets encrypt on my debian and apache linux virtual machine on google cloud.
the domain is successfully secure with HTTPS.
Although on directly accessing the external ip address i am still getting an unsecure version of the website.
How to redirect the ip directly to the HTTPS version set up with APACHE and just the "domain.com" towards -->> HTTPs:www.domain.com .
I have tried to re-route to port 80 and 443 towards the HTTPS version as in PHP without any luck as shown here :
How to redirect from www to https www with htacces?
in my 000-default.conf:
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster#localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.bracketline.com [OR]
RewriteCond %{SERVER_NAME} =localhost
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI}
[END,NE,R=permanent]
</VirtualHost>
and on my 000-default-le-ssl.conf i have:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName localhost
ServerAdmin webmaster#localhost
Alias /static /var/www/static-root
<Directory /var/www/static-root>
Require all granted
</Directory>
Alias /media /var/www/media-root
<Directory /var/www/media-root>
Require all granted
</Directory>
<Directory /var/www/venv/src/cfehome>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess cfehome python-
path=/var/www/venv/src/:/var/www/venv/lib/python3.5/site-packages
WSGIProcessGroup cfehome
WSGIScriptAlias / /var/www/venv/src/cfehome/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ServerAlias www.bracketline.com
SSLCertificateFile
/etc/letsencrypt/live/www.bracketline.com/fullchain.pem
SSLCertificateKeyFile
/etc/letsencrypt/live/www.bracketline.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
I am not sure how this thing works if a detailed blog or turorial could be given it would be of great help. thanx in advance!
First lacate which .conf file youre actually using by typing apachectl -S (this works on Debian based OS'es).
Next up edit the file, it should look simillar:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
Protocols h2 http/1.1
# SSL Configuration
# Other Apache Configuration
</VirtualHost>
In general there are several tutorials how to configure HTTP to HTTPS redirect:
Redirect HTTP to HTTPS in Apache
How to Redirect HTTP to HTTPS on Apache
Apache Redirect to HTTPS
And some interesting discusson Why is my Apache VirtualHost directing to the wrong VirtualHost?
Lastly - here's another SO discussion on that topic that has an accepted answer.
Those are just recent (up to 2 years old) examples that will help you and there are dozens more if they won't answer your questions.

Two domains in a single AWS ec2 instance

I have two domains domain1.com & domain2.com & want to run both domains on a single ec2 instance.
I have created 2 hosted zones for both domains and added nameserver in my domain provider.
domain1.com's code resides in - /var/www/html
domain2.com's code resides in - /var/www/domain2
Added virtual host for both respective domains in
/etc/httpd/conf/httpd.conf
virtual host looks like below:
<VirtualHost *:80>
ServerAdmin webmaster#yourdomain.com
DocumentRoot "/var/www/html"
ServerName domain1.com
</VirtualHost>
So when I run domain1.com that works perfectly fine, but when run domain2.com it show code from /var/www/html but it should show code from /var/www/domain2
So please help me to identify what I am doing wrong?
Give this a try:
# Ensure that Apache listens on port 80
Listen 80
<VirtualHost *:80>
DocumentRoot "/var/www/html"
ServerName www.domain1.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/domain2"
ServerName www.domain2.com
# Other directives here
</VirtualHost>
This will set up two VirtualHosts that respond to domain1.com and domain2.com, respectively. Note that the first VirtualHost for domain1.com will be seen as the primary host and will be the default responder for all requests that don't match any ServerNames.
Other examples can be found here.

How to configure subdomain to specific port

I have two projects 1: is wordpress running on apache (main website thespatio.com/45.33.10.149)
2: A Django Application running on Nginx using same IP with 81 port. (45.33.10.149:81). I want to configure above two apps so that when some one hit http://thespatio.com it should show main website and if some hit http://or.thespatio.com it should show my django application. I have seen many fix but none work for me. I tried virtual host like proxypass and proxy_reverse but apache stopped working. below are the two virtual hosts conf file
Main Website settings (conf)
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName thespatio.com
ServerAlias www.thespatio.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Django App (conf)
<VirtualHost *:80>
ServerAdmin admin#test.com
ServerName or.thespatio.com
ServerAlias www.thespatio.com
ProxyPass / http://or.thespatio.com:81/
ProxyPassReverse / http://or.thespatio.com:81/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Any help would be appreciated. Thanks in advance
mod_proxy was not installed. I follow Install mod_proxy tutorial and my both apps working fine.

HTTP to HTTPS redirect Vagrant

So I've been having an issue with Vagrant and Port Forwarding...
Currently, Vagrant is already forwarding ports 80 -> 8080 and 443 -> 8443. Issue is, I don't want a client to have to type ports 8443 and 8080 to access my web page. So now, the client has to type in 127.0.0.1:8080 or 127.0.0.1:8443 to access the webpages.
How would I go about changing this? My current stack is VM, Vagrant, Apache, Django.
I have 2 VHosts setup. Heres my sites-available/mysite.com file
<VirtualHost *:80>
ServerAdmin webmaster#example.com
#ServerName spritebots.com
#ServerAlias www.spritebots.com
ServerName 127.0.0.1
ServerAlias 127.0.0.1
ProxyRequests off
ProxyPreserveHost On
ProxyPass / https://127.0.0.1:8443
ProxyPassReverse / https://127.0.0.1:8443
#Redirect permanent / https://127.0.0.1:8443
</VirtualHost>
<VirtualHost *:443>
WSGIDaemonProcess spritebots
WSGIProcessGroup spritebots
WSGIScriptAlias / /var/www/spritebots/apps/wsgi.py
ServerAdmin webmaster#example.com
#ServerName spritebots.com
#ServerAlias www.spritebots.com
ServerName 127.0.0.1
ServerAlias 127.0.0.1
DocumentRoot /var/www/spritebots/
Alias /media/ /var/www/spritebots/static/media/
Alias /static/ /var/www/spritebots/static/
<Directory /var/www/spritebots/static>
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/spritebots/static/media>
Order deny,allow
Allow from all
</Directory>
SSLEngine on
SSLCertificateFile /etc/ssl/certs/spritebots.crt
SSLCertificateKeyFile /etc/ssl/certs/spritebots.key
</VirtualHost>
So I bought a domain name, spritebots.com and I can't even get spritebots.com to point to 127.0.0.1:8443.
But for now, I would like for anyone visiting my site at 127.0.0.1, to be redirected 127.0.0.1:8443, then mask the port number. Basically like how every production web page is with HTTPS enabled.
Does a majority of developers/companies use vagrant or VMs for production web sites? Because I feel that Vagrant's port forwarding is messing this up. Or it's more possibly me just being ignorant of web development. :(
Any help would be greatly appreciated!
In your Vagrantfile, you can assign your VM a private network IP, by dropping in:
config.vm.network "private_network", ip: "192.168.50.5"
Then you don't have to mess with ports.