apache config in apache server ,where to edit apache config in cpanel - apache

I read a post which says Do this in the Apache config:
<VirtualHost *:80>
ServerName www.domain2.com
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) ws://localhost:3001/$1 [P,L]
ProxyPass / http://localhost:3001/
ProxyPassReverse / http://localhost:3001/
</VirtualHost>
The problem is that i am new to these things(server configuration). Can you please tell me where to find apache config and how to input these lines into it.. i have linux vps cpanel + centos6 running an apache server . because when i go to apache configuration in whm cpanel, i see global configuration,
DirectoryIndex Priority, and so on. .

Best I recommend is to ssh your Centos Server go to this default path (/etc/apache2/httpd.conf) and here you can make all possible changes .
So just ssh to server change to sudo nano /etc/apache2/httpd.conf you may use vi or gedit whatever you preferred and do the needful changes to the configuration .
For Cpanel you can do it in two ways either via WHM or with CLI
I recommend here to do with CLI :
To modify the Apache configuration's include files via the command line interface (CLI), edit the appropriate configuration files in the /usr/local/apache/conf/includes/ directory.
Afterward run service httpd/apache2 restart
You can do it by editing .htaccess file as well but again it's not the best practices to be follow.
Please let me know if you need more clear picture on this.

Related

Configure Apache for Angular and Tomcat on the same host

I am trying to serve my application on a Centos 7 host, where I have deployed an Angular app for the frontend and Java Spring Boot app for the backend.
My versions are Apache 2.4.6, Tomcat 9 and Angular 7.
Let's say my domain is example.com. I want the user to write the following on the browser to access the Angular app routes, i.e. the frontend:
example.com/**
Also I would like to be able to write the following on the browser to access the Java app routes, i.e. the backend:
example.com/api/**
I tried to do it using Apache's mod_proxy, after reading this post: https://stackoverflow.com/a/33095758/350061. However, I only managed to serve Angular through example.com, while my API is only served on its original endpoint, that is example.com:8080/my-api and not through example.com/api.
These are my Apache settings:
/etc/httpd/conf.d/example.conf
<VirtualHost *:80>
ServerName example.com
ProxyPreserveHost On
DocumentRoot "/var/www/my-ng-app/dist/my-ng-app"
RewriteRule ^/api/(.*) http://localhost:8080/$1 [P,L,QSA]
ProxyPassReverse /api/ http://localhost:8080/
</VirtualHost>
/var/www/my-ng-app/.htaccess
RewriteEngine On
RewriteRule ^api/(.*) http://localhost:8080/my-api/$1 [P,L,QSA]
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
I have configured .htaccess to be read from that folder and verified that it is actually read. Also even if I remove the last part of .htaccess that redirects non-existing resources to index.html, the api does not get served from example.com/api, as I wish. It only gets served from example.com:8080/my-api
Any ideas on how to configure it?
After a long time of googling and trial and error, my solution consists of these changes:
I edited Tomcat's server.xml connector to serve requests through localhost. This made possible for rewrite settings to access my Java app through localhost:8080, but made impossible for me to access remotely my Java app through example.com:8080, which is fine.
I configured SELinux to allow Apache (httpd) to initiate outbound connections, in my case connections to localhost
I applied some fixes on my original VirtualHost and .htaccess configuration
More specifically:
sudo nano /opt/tomcat/latest/conf/server.xml
Find the connector for port 8080 and add address attribute as such:
<Connector port="8080" protocol="HTTP/1.1"
address="127.0.0.1"
connectionTimeout="20000"
redirectPort="8443" />
/usr/sbin/setsebool -P httpd_can_network_connect 1
The -P persist the setting for future reboots
/etc/httpd/conf.d/example.conf
<VirtualHost *:80>
ServerName example.com
ProxyPreserveHost On
DocumentRoot "/var/www/my-ng-app/dist/my-ng-app"
RewriteRule ^/api/(.*) http://localhost:8080/my-api/$1 [P,L,QSA]
ProxyPassReverse /api/ http://localhost:8080/my-api/
</VirtualHost>
/var/www/my-ng-app/.htaccess
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
After these changes just restart Tomcat and Apache and you will be able to access your API through example.com/api

Apache/2.4.6 (CentOS) Virtual Host error 404 with directory and files outside root

I'm trying to setup a Virtual Host (let's say "subdomain1.domain.com") on Apache/2.4.6 (CentOS). The web server does not have a hostname, IP address let's say is 55.55.55.55, Apache is listening at port 80. I have a redirect service active and the requests for "subdomain1.domain.com:80" are redirected to http://55.55.55.55/.
DocumentRoot is "/var/www/html" as per httpd.conf.
The website public content is in a directory named "subdomain1" which I'm placing inside the /var/www/html directory. The website code makes use of some JS/CSS which should be in common with the other virtual hosts I would like to create later, so I place the JS/CSS in a directory "libraries" located outside the WebSite Document Root (means, inside "/var/www/html"), and I'm tryng to give them access from the html code by a "../libraries/".
Virtual Host configuration file is a "subdomain1.conf" placed in /etc/httpd/conf.d/" directory, this is the file:
<VirtualHost *:80>
ServerName subdomain1.domain.com
DocumentRoot /var/www/html/subdomain1
</VirtualHost>
Now, with this configuration I can reach the website of Subdomain1 at http://subdomain1.domain.com. The public content of Subdomain1 is reachable, but.. the problem is: all the JS/CSS are not found by Apache and they all return a 404 error. While debugging with the web browser I see the path where Apache is searching the JS/CSS library is "http://55.55.55.55/libraries/" and this returns 404.
Any idea where I'm doing wrong ?
Many thanks !
Mic
Here are some suggestions from me.
Setting FQDN of your server FQDN
$ nano /etc/hosts
55.55.55.55 subdomain1.domain.com subdomain1
Setting your server hostname
$ nano /etc/hostname
subdomain1.domain.com
Enable mod_rewrite on the apache webserver
Create an .htaccess file below and save it into the folder /var/www/html/subdomain1/
< IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] < /IfModule>
Thank you,
Afrizal

Configure Passenger with Apache - On Canvas LMS

Canvas LMS Installation
https://github.com/instructure/canvas-lms/wiki/Production-Start
Configure Passenger with Apache
First, make sure Passenger is enabled for your Apache configuration. In Debian/Ubuntu, the libapache2-mod-passenger package should have put symlinks inside of /etc/apache2/mods-enabled/ called passenger.conf and passenger.load. If it didn't or they are disabled somehow, you can enable passenger by running:
sysadmin#appserver:/var/canvas$ sudo a2enmod passenger
In other setups, you just need to make sure you add the following lines to your Apache configuration, changing paths to appropriate values if necessary:
LoadModule passenger_module /usr/lib/apache2/modules/mod_passenger.so
PassengerRoot /usr
PassengerRuby /usr/bin/ruby
If you have trouble starting the application because of permissions problems, you might need to add this line to your passenger.conf, site configuration file, or httpd.conf (where canvasuser is the user that Canvas runs as, www-data on Debian/Ubuntu systems for example):
PassengerDefaultUser canvasuser
I don't find the httpd.conf file (Using ubuntu 16.04)
and don't understand where to put (Path of the file, Which file)
Can anyone please help me with this, What i have to do in this section?
You don't need add those lines to apache configs if you have installed libapache2-mod-passenger (Ubuntu, Debian) and executing 'sudo a2enmod passenger' was OK (no reply that module don't exist)
You need create VirtualHost with something like
<VirtualHost IP:80>
ServerName canvas.yourdomain.net
DocumentRoot /home/canvas/public
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteCond %{REQUEST_URI} !^/health_check
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
ErrorLog /var/log/virtualmin/canvas_error_log
CustomLog /var/log/virtualmin/canvas_access_log combined
SetEnv RAILS_ENV production
<Directory /home/canvas/public>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>

apache2 rewrite rule without changing URL

This below rewrite redirects localhost to http://www.example.com/?id=211&test=1 but I want that localhost on browser should not be changed but the page will come form the above link.
I am using this rewrite rule on my Apache conf:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$ [OR]
RewriteRule ^/?$ http://www.example.com/?id=211&test=1 [L]
This below rewrite redirects localhost to
http://www.example.com/?id=211&test=1 but I want that localhost on
browser should not be changed but the page will come form the above
link.
If you want to load a page from elsewhere without chaining the URL, mod_rewrite is the wrong tool for the job. Use mod_proxy instead. First enable it in Apache like this; example assumes you are on Ubuntu 12.04 but should work on most any Linux Apache install
sudo a2enmod proxy proxy_http
Then set this to enable a reverse proxy from your root path of / to http://www.example.com/?id=211&test=1:
<IfModule mod_proxy.c>
# Proxy specific settings
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://www.example.com/?id=211&test=1
ProxyPassReverse / http://www.example.com/?id=211&test=1
</IfModule>
EDIT: Seems like mod_proxy and query strings for the destination do not mix; emphasis mine:
This directive allows remote servers to be mapped into the space of
the local server; the local server does not act as a proxy in the
conventional sense, but appears to be a mirror of the remote server.
The local server is often called a reverse proxy or gateway. The path
is the name of a local virtual path; url is a partial URL for the
remote server and cannot include a query string.
So if there is anyway you could set another page—maybe on localhost—that would bounce it behind the scenes. Meaning this happens on localhost:
ProxyPass / bounce.php
And then the file, bounce.php could have this line in it:
<?php
header('Location: http://www.example.com/?id=211&test=1');
?>
Which would allow mod_proxy to have a valid destination. And then the PHP file does the redirect? Hard to say, but the query string on your destination server is the issue.

Apache Virtualhost Directory conditional redirect

I just created a website with two environments as virtualservers - testing and production. As production server is open to everyone but I allowed only my IP to access testing environment:
<VirtualHost *:80>
DocumentRoot /home/xxx/www
ServerName testing.xxx.com
<Directory /home/xxx/www>
Order deny, allow
Deny from all
Allow from xxx.xxx.xxx.xxx
</Directory>
</VirtualHost>
The problem is that google has already indexed some of my testing environment pages and they are available in google results. I would like any IP but mine to be redirected to production server (xxx.com) while accessing testing.xxx.com. I would rather do it with apache.conf than .htaccess(because of git repositories conflicts). Is it possible to add a conditional redirect to apache config?
You can use mod_rewrite features in your httpd.conf Apache config file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REMOTE_HOST} !^123\.456\.788 [OR] # exclude your first IP
RewriteCond %{REMOTE_HOST} !^123\.456\.789 # exclude your second IP
RewriteRule ^(.*)$ http://production-env.com/$1 [R=301,L] # redirection to production site
</IfModule>
Or you can put these declarations into <Directory> section of your vhosts config file.
Generally you can take advantage of mod_rewrite module to manage URL routing policies for your web server. Before using it make sure that this module is installed and activated in your Apache.