Solved How to Enable CORS in Apache

Hello,
I'm just setting small connection to PostgreSQL database from website and I can't enable CORS in Apache 2.4 on FreeBSD. While ago I have enabled it in Apache 2.4 on RedHat with success, and I dont know what am I doing wrong now.

I added these lines to my httpd.config :
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "*"
Header set Access-Control-Allow-Methods "POST, GET"

I'm still getting an error:
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Some ppl on web tell to use a2enmod to enable headers, but I don't have such command (both unix and linux). How can I enable them??

I can see that there is a lot of LoadModules in httpd.config by default, while there was none of them on RedHat. So I suppouse that one of them could disable cors. Do you know which one??

Code:
LoadModule mpm_prefork_module libexec/apache24/mod_mpm_prefork.so
LoadModule authn_file_module libexec/apache24/mod_authn_file.so
LoadModule authn_core_module libexec/apache24/mod_authn_core.so
LoadModule authz_host_module libexec/apache24/mod_authz_host.so
LoadModule authz_groupfile_module libexec/apache24/mod_authz_groupfile.so
LoadModule authz_user_module libexec/apache24/mod_authz_user.so
LoadModule authz_core_module libexec/apache24/mod_authz_core.so
LoadModule access_compat_module libexec/apache24/mod_access_compat.so
LoadModule auth_basic_module libexec/apache24/mod_auth_basic.so
LoadModule reqtimeout_module libexec/apache24/mod_reqtimeout.so
LoadModule filter_module libexec/apache24/mod_filter.so
LoadModule mime_module libexec/apache24/mod_mime.so
LoadModule log_config_module libexec/apache24/mod_log_config.so
LoadModule env_module libexec/apache24/mod_env.so
LoadModule headers_module libexec/apache24/mod_headers.so
LoadModule setenvif_module libexec/apache24/mod_setenvif.so
LoadModule version_module libexec/apache24/mod_version.so
LoadModule unixd_module libexec/apache24/mod_unixd.so
LoadModule status_module libexec/apache24/mod_status.so
LoadModule autoindex_module libexec/apache24/mod_autoindex.so
LoadModule dir_module libexec/apache24/mod_dir.so
LoadModule alias_module libexec/apache24/mod_alias.so
IncludeOptional etc/apache24/modules.d/[0-9][0-9][0-9]_*.conf

I will be thankfull for any help.

Cheers.
 
I added these lines to my httpd.config
Where exactly?

To add the CORS authorization to the header using Apache, simply add the following line inside either the <Directory>, <Location>, <Files> or <VirtualHost> sections of your server config

Some ppl on web tell to use a2enmod to enable headers, but I don't have such command
As far as I know this is a typical Debian (and derivatives like Ubuntu) command to enable certain sections and websites. No such command exists on FreeBSD (the entire config structure is different too).
 
Thanks for answer.
Where exactly?

I tried in <Directory />, <Directory "/usr/local/www/apache24/data">, between them, after Load modules and in .htaccess. I also spammed all together.

I did instructions from enable-cors.org first. As far as I know a2enmod writes to conf this line (and I have it):
Code:
LoadModule headers_module libexec/apache24/mod_headers.so

I read some from FreeBSD handbook:
Code:
https://www.freebsd.org/doc/en_US.ISO8859-1/articles/linux-users/startup.html3
https://www.freebsd.org/doc/handbook/network-apache.html

So I changed in rc.conf:
Code:
apache24_flags="-DSSL"

##INTO

apache24_flags=""

I also found apache24 file in /usr/local/etc/rc.d, so I changed

Code:
apache24_flags="${apache24_flags} -DNOHTTPACCEPT"

##INTO

apache24_flags=""

I also loaded accf_http to kernel (mistake??) :
Code:
root@jomonger:~ # kldload accf_http
kldload: can't load accf_http: module already loaded or in kernel

##AND CANNOT UNLOAD

root@jomonger:~ # kldunload accf_http
kldunload: can't unload file: Operation not supported
root@jomonger:~ # kldunload -f  accf_http
kldunload: can't unload file: Operation not supported

EDITED:
After these changes and spamming Access-xxx-xxx "*" everywhere, also using "add" instead of "set" in mentioned lines I'm getting an error for POST method in angular :
Code:
 "has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '*, *, *, *', but only one is allowed."
So now it sees something.
Auto OPTIONS method (Where is it from?? Same code requests only POST method from RedHat):
OKnotOK.png

And ERROR 404 on POST:
OKnotOK2.png


If I leave access lines :
Code:
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "*"
Header add Access-Control-Allow-Methods "POST, GET"
Only in .htaccess, I still get no CORS policy error. Also GET method is still rejected, no matter what settings. But now it is ERROR 404, was 500 so ...

I got a plain text when I go to api.py or rkr.py from browser. So I think that changing rc.d helped and now I can connect but python script doesn't run!

Is any of these shebang is good for Python/FreeBSD ?? : - Seems they are wrong, if this is problem now, that means that I had to change rc.conf (and maybe use ADD). What shebang should I use?
Code:
#!/usr/local/bin/python3

#!/bin/sh exec /usr/local/bin/python3

#!/usr/bin/env python3

I'm using FreeBSD 12.1-RELEASE r354233 GENERIC.

(the entire config structure is different too).
Where can I find config structure for FreeBSD?

If I reinstall Apache, can I be sure that all files will be overwrited/deleted ?? I'm lost :D.

Regards.
 
I had access lines in wrong place, maybe also bad priviliges on files(?). As I edited on Windows, I forgot about EOL char, and maybe it caused problems too (?).

Now access lines are before (outside) <Directory />, just after ServerName and work.

I also had to load WSGI module. One can do this by "pip install mod_wsgi" (python package manager) in shell,
or by adding in httpd.conf:
Code:
LoadModule wsgi_module        libexec/apache24/mod_wsgi.so

For python I needed CGI module loaded in apache (httpd.conf):
Code:
LoadModule cgi_module libexec/apache24/mod_cgi.so
and link .py scirpts in proper directory, default:<Directory "/usr/local/www/apache24/data">:
Code:
Options +ExecCGI
AddHandler cgi-script .cgi .pl .py
AllowOverride All
Shebangs were good.

I should have mentioned Python in thread title.

Cheers.
 
As I edited on Windows, I forgot about EOL char, and maybe it caused problems too (?).
The main difference is that Windows uses a CR (Carriage Return) and a LF (Line Feed) at the end of a line, whereas UNIX only uses LF. When you edit such a file you will notice a ^M at the end of a line. This is only an annoyance, it has very little influence. Many (S)FTP clients can actually convert this automatically when you transfer files between systems. Editors like vi(1) can also convert this transparently.
 
Back
Top