Enable Website access from both www and non-www - python

I recently hosted my site on Cpanel, however, I am still not able to find the right solution for my problem.
I intend to make my site accessible from www.site.com too.
Currently accessing the site using the above returns the following on the browser
This site can’t be reached
www.site.com’s server IP address could not be found.
Try:
Checking the connection
Checking the proxy, firewall, and DNS configuration
ERR_NAME_NOT_RESOLVED
How can I make the site accessible using www.site.com as well? Probably using .htaccess file
Adding the following on my .htaccess file still does not work
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
It's a Python app

I can see that you are getting the error "ERR_NAME_NOT_RESOLVED". This means that the DNS record for www.site.com is missing. You need to add a CNAME or A record for www.site.com to point to the IP address of your server.
You can add the records from cPanel >> Zone editor.
https://docs.cpanel.net/cpanel/domains/zone-editor/
This works only if your nameservers are the same as your hosting server. If you are using any other nameserver you will need to add the records on it.

Related

How to develop in production and development mode of Flask website

This is not a pure technical issue but more methodologic question.
I've seen Q&A regarding configuration for DEBUG and PRODUCTION ENVs but my question is concerning other issue.
When I started working on the project on my local machine I edited the hosts file to redirect www.example.com (I used the same URL for my live website) to 127.0.0.1 as I used to and it's working great.
Now when www.example.com is live, I wanted to know what is the right configuration for keep developing the website?
The only idea I came up with is to use www.example.org (So I won't lose actual access to www.example.com) in my hosts file and on the code to use IF DEBUG to redirect traffic to example.org instead of example.com but I feel there are better options.
I also would love some tips about the right way of working with git to post local updates to the live server.
When I want to access the website I'm running locally I just use http://127.0.0.1:5000 in my browser to access it.
If you've hardwired the domain "www.example.com" into your flask logic somewhere, i.e. when passing a redirect link to an OAuth service I would consider removing that hardcoded logic. Instead use an environment variable which you set differently on production/dev or else access to the current domain of a request with request.url_root or request.headers['Host'].
Make use of Flask's SERVER_NAME and PREFERRED_URL_SCHEME builtin configuration values.
class Config(object):
# blah blah
class DevelopmentConfig(Config):
SERVER_NAME = "example.local"
PREFERRED_URL_SCHEME = 'http'
class ProductionConfig(Config):
SERVER_NAME = "example.com"
PREFERRED_URL_SCHEME = 'https'
On your development machine map example.local to 127.0.0.1 .

deploying my django site to windows server without using IIS

I finished writing a Django application and now I want to deploy it,
I have a Windows server and have successfully installed Python and Django on it,
Now my app runs on localhost on my windows server,
Now I want to make the site public, meaning that anyone who goes to the IP address of my windows server can browse my site,
Is there a simple way to do this without using IIS?
thank you
Step One
Set a static ip for your server (It's possible without this, but easier)
Once set, log into your router as admin, and forward port 80 to your servers ip address.
There is a tutorial for this at https://portforward.com
Step Two
If you already have a domain name, ignore this bit
Purchase a domain name from an domain name from a domain name registrar such as
1and1 / Ionos (https://ionos.com)
(I would personally advise against https://GoDaddy.com, the prices tend to be odd there)
Step 2.1
Go into your domains settings, and forward traffic to the external ip address of your router.
Hope that this helped!
I've never done what your planing but if you plan to host just one domain then localhost(127.0.0.1) will work just fine but if you plane to host multiple domain you will need to find a way to resolve the right domain to the right site.
Ports to open on both router and Win Server
80 for HTTP
465 for HTTPS
Make sure that you have a Static IP provided by your ISP, you will also need to make sure that your ISP does not Block port 80 if they do ask them to unblock.
If you don't have a static IP or ISP don't allow you to open port 80 then you can use DynDNS to forward traffic to your server, but this option is not the best.
Your Server will also need a static IP to the server as mentiond by Legorooj

WLST edit mode issue for managed instance

While I had executed command edit() connecting to managed instance I was ended-up with the following error. How & What I have to do in order to come out of this problem.
wls:/offline> connect('Admin60000','sun1rise','t3://my-comm-app-serv:60001')
Connecting to t3://my-comm-app-serv:60001 with userid Admin60000 ...
Successfully connected to managed Server "MiCommApp" that belongs to domain "MiBeaDir".
Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.
wls:/MiBeaDir/serverConfig>cd('/Servers/MiCommApp/SSL/MiCommApp')
wls:/MiBeaDir/serverConfig/Servers/MiCommApp/SSL/MiCommApp> edit()
Edit MBeanServer is not enabled on a Managed Server.
60001 is managed instance port which is one among the managed instance that runs in admin server. Admin server runs in 60000 port
That is because for managed servers, WLST functionality is limited to browsing the configuration bean hierarchy. Read below excerpt from WL official documentation.
To edit configuration beans, you must be connected to an
Administration Server, and you must navigate to the edit tree and
start an edit session, as described in edit and startEdit,
respectively.
If you connect to a Managed Server, WLST
functionality is limited to browsing the configuration bean hierarchy.
While you cannot use WLST to change the values of MBeans on Managed
Servers, it is possible to use the Management APIs to do so. BEA
Systems recommends that you change only the values of configuration
MBeans on the Administration Server. Changing the values of MBeans on
Managed Servers can lead to an inconsistent domain configuration.
So, basically you need to connect with your Admin server (current you are getting connected with your managed server, as per logs you have provided - Successfully connected to managed Server "MiCommApp" that belongs to domain "MiBeaDir".) and then issue edit configurations using edit() and startEdit() WLST commands.
BTW, I connect to my server using following command:
If HTTPS - connect(url='t3s://abc.xyz.com:37001',adminServerName='AdminServer')
If HTTP - connect(url='t3://abc.xyz.com:37001',adminServerName='AdminServer')

HTTP_AUTHORIZATION header in Django

I wrote a simple check in django which requires an access token to be passed along in some requests (I'm using a decorator).
authorization = request.META.get('HTTP_AUTHORIZATION', None)
# Forbid access when no access token, or an invalid one, is provided
form = AccessTokenForm({ 'access_token': authorization })
if not form.is_valid():
raise exceptions.HttpDeniedException()
This has been working fine with django's development server, but recently I deployed using Apache and now it doesn't work anymore, as the authorisation variable is None. Has anyone faced something similiar before? The client is passing the header as Authorization; again, this is working fine in localhost, but not on the remote server.
Edit: I found this, and it seems that it should work, but it doesn't. Actually, I didn't even have an .htaccess file, and adding one doesn't seem to have taken any effect at all. Here are the contents:
RewriteEngine On
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(static/.*)$ - [L]
RewriteCond %{REQUEST_URL} !(dispatch.fcgi)
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L,E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
The thing is, I'm not even sure Apache is using this at all..
It seems there's a ticket for this issue here. Just add the following to apache2.conf:
WSGIPassAuthorization on

Appropriate cookie domain for Django dev server running multiple sites

I have multiple Django dev sites running locally like http://localhost:8000, http://localhost:8001, http://localhost:8002, etc.
Originally, I had SESSION_COOKIE_DOMAIN and CSRF_COOKIE_DOMAIN set to '' or 127.0.0.1 but this causes each site to overwrite the other's cookies, causing me to have to login every time I switch between sites. I tried using 127.0.0.1:<port> but that had no effect.
How do I get these sites to use separate cookies?
One solution to this problem is to use local domain name resolution to reach each of your different development servers. If you leave SESSION_COOKIE_DOMAIN as None, then the returned cookie is a standard domain cookie and will have the same domain as the request.
Have a look at http://en.wikipedia.org/wiki/Hosts_(file) which describes how to add local host file entries.
With a hosts file like this:
127.0.0.1 www.testserver1.com www.testserver2.com
You could then access each of your different test servers at:
http://www.testserver1.com:8000
http://www.testserver2.com:8001
I haven't tried this, but I believe it should work.
Alternatively, as per Mikhail's answer, use a different session cookie name for each instance.
Cookies are shared across ports for the same domain according to various RFCs (see e.g. https://www.rfc-editor.org/rfc/rfc6265#section-8.5). So this is not django-specific.
I think you could use different SESSION_COOKIE_NAME to have at least sessions separated.

Categories

Resources