Apache - Domain for localhost to access folders as http://folder.local - url

I'm running XAMPP on Ubuntu and I'd like to create a virtual host for my projects, so that I have a tld assigned to my server root directory (for example .local) and folders inside it accessible through URLs as http://foldername.local.
Also, how much more complicated would it be to use .htaccess to have http://someotherdomain.local redirect to the /foldername path in the server root?

I've managed to do it on my own. It is possible to do it, however you'll need to install a DNS server.
Note: I decided to use .dev as my local domain, so in the following
examples, the dev part will refer to my chosen domain. Keep that in
mind.
Install and configure DNS Server
It shouldn't matter which one it is, but you'll need to know how to configure it properly. The configuration depends on which DNS server you chose. I went for dnsmasq. It's lightweight and very handy.
An important note for Ubuntu users is that since Ubuntu 11.10 there is
already a light version called dnsmasq-base installed, which will
cause conflicts during installation. I won't be explaining here how to
get around this, because there are many instructions available elsewhere.
Once you have your DNS server installed, you should configure it to listen for the address equal to your desired domain.
In my case with dnsmasq, that meant opening /etc/dnsmasq.conf and
changing line #62 to this: address=/dev/127.0.1.1
Configure Web server
Assuming that you already have some kind of Server software installed, you need to make a few tweaks.
First, you should edit your hosts file to map your desired domain to your localhost.
in my case of XAMPP for Linux on Ubuntu, this means I opened
/etc/hosts and changed lines
127.0.0.1 localhost
127.0.1.1 tomica-ubuntu
to
127.0.0.1 localhost
127.0.1.1 tomica-ubuntu dev
This will redirect http://dev to my local server.
Next, create a new virtual host with a couple of specific options, like this:
In my case, that means opening
/opt/lampp/etc/extra/httpd-vhosts.conf and adding this at the end of
the file:
<VirtualHost *:80>
DocumentRoot "/opt/lampp/htdocs/dev"
ServerName dev
ServerAlias *.dev
<Directory /opt/lampp/htdocs/dev>
AllowOverride All
</Directory>
</VirtualHost>
For the sake of brevity, I won't explain this piece of code, since
documentation is also available.
After all this is done, start your DNS and Web servers, or restart them if they're already running.
Configure .htaccess
Open root folder of your newly created host. That's the folder devined in your . In my case, that's /opt/lampp/htdocs/dev. In there, create a .htaccess file and put this in it:
# Specify order of index files; if none exist, show files list
DirectoryIndex index.php index.html
# Interpret .html files as .php scripts
AddHandler php5-script .php .html
# THE MAGIC - Redirect subdomains of .dev to their respective folders
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.dev$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.dev(.*)?$ [NC]
RewriteRule !^%2\.dev%3?/$ http://dev/%2%{REQUEST_URI}/ [P]
Again, explaining all this would require too much space and time. Just copy/paste and don't worry :) But don't forget to change my dev to anything you chose for your domain name.
AND THAT'S IT! By now you should be able to browse your project using addresses like http://folder.dev/, http://www.folder.dev, http://folder.dev/file.html, http://folder.dev/subfolder/document.txt etc.
As a bonus, I will add just one more advice. The reason why I did all this is so that I could more easily develop my Laravel and WordPress prjects. However, with Laravel, you should redirect the url http://lvproject.dev/ to the location of /lvproject/public. And here is the .htaccess file that enables just that. Open your /lvproject folder, create a .htaccess file and place this code in it:
RewriteBase /lvproject/
RewriteCond %{REQUEST_URI} lvproject/index\.php [NC]
RewriteRule index\.php(.*)$ public/ [L]
Two drawbacks of this solution are: 1) RewriteBase rule needs to be set anew for every new project (i.e. you need to manually create .htaccess in each new project); 2) Your project will be available from both http://lvproject.dev/ and http://lvproject.dev/public/, which is not cool, but I'm too lazy at the moment to get it fixed :)

Related

how to setup cakephp that runs from url path

So I'm building microservice architecture and I have separate cakePHP installations for Administration and Partners. So https://example.com is going to be served for normal users and that works fine as it's a standard installation.
But I can't figure out how to set up other two that they work as https://example.com/admin and https://example.com/partners. I use nginx to direct traffic to the correct docker container running admin and partners cakephp. Problem is how to let CakePHP know that his root website path is /admin and not just \? Because all the routing gets messed up.
I'm using CakePHP 4.x and PHP 7.2.x
I think you can simply change the App.baseUrl in the cakephp configuration. For more info check the official documentation
I think it's not a good option to create multiple projects for different AUTH (Partner/Administration). Cakephp allow multiple authentication for different roles.
Example: for a school project I had created Admin/School/Teacher/Students using different AUTH and AUTH storage , which never conflict with each other.
In your case there is a solution .
Assuming that you want to use https://example.com/ and Partner and https://example.com/admin as administrator.
1.First host your Partner project in ROOT.
Create a folder , named "admin" in Partner root projects folder.
Copy/Move your admin project code to your "admin" folder.
Change/update your ".htaccess" files in Partner root folder(FOR APACHE).
APACHE CODE
# Uncomment the following to prevent the httpoxy vulnerability
# See: https://httpoxy.org/
#<IfModule mod_headers.c>
# RequestHeader unset Proxy
#</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule admin admin/ [L]
RewriteRule ^(\.well-known/.*)$ $1 [L]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
After "RewriteEngine on " a new line added to bypass the PARTNER cakephp.
NGINX
# nginx configuration by winginx.com
location / {
rewrite admin /admin/ break;
rewrite ^/(\.well-known/.*)$ /$1 break;
rewrite ^/$ /webroot/ break;
rewrite ^(.*)$ /webroot/$1 break;
}

Ruby On Rails on eHost

Is it possible to just upload my RoR files to my server (eHost) and have my website up and running?
If not, what should I do in order to have it working on any Host with support for RoR? I know of heroku but it is quite expansive for me right now. eHost charges only $3.27 per month and they claim to have support for RoR. I even called Tech Support and asked them to update the vertion to 2.3.0(Ruby) and 4.2.4(Rails).
I am new on RoR and have no idea of how to host my application on the ordinary hosts out there. So any help will be very welcome.
I've not tried this, I only hope this gives you an idea,
They say they support Ruby and RoR, So taking a look https://support.ehost.com/articles/ruby-on-rails-and-gems they say it's on Apache CGI server.
So - this is my suggestion - you upload your project, and either you contact support or you might want to try the suggestion here:
http://blog.hulihanapplications.com/browse/view/52-how-to-install-a-ruby-on-rails-app-part-2 I'll quote the section (in case it ever becomes unavailable). It's under the section FastCGI/CGI:
Configure .htaccess
Apache usually looks in any web directory for an Override file(which lets you set your own Apache configuration settings for the directory) called .htaccess. For ruby on rails, we need a special .htaccess file that tells the server to set up the directory as a ruby on rails application. This file should be created in the public directory if it's not already there. Here's what the contents of the file should look like:
#AddHandler fastcgi-script .fcgi
#AddHandler fcgid-script .fcgi
AddHandler cgi-script .cgi
Options +FollowSymLinks +ExecCGI
RewriteEngine On
RewriteRule ^$ index.html [QSA]<br>RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
Linking to public
The public directory inside your rails application contains all your public resources(images, css, javascript, etc.), and well as the .htaccess file needed to run your application. Until now, your public directory hasn't been visible to the outside world. Now we'll link your application to a directory inside your web root.
For example, if I was planning on running my rails application in a subdirectory of my website called www.mywebsite.com/myrailsapp, you'll need to create a symbolic link(it's kind of like a shortcut) in your web root directory(remember, this is where your regular website stuff goes, usually named public_html or www). Let's also pretend that on the server, the path to your web root is /home/myaccount/public_html and the path to your rails application is /home/myaccount/myrailsapp. Therefore, the command to create a symbolic link will be:
ln -s /home/myaccount/myrailsapp/public /home/myaccount/public_html/myrailsapp

Zend Framework 2 .htaccess mamp pro

I want start to work with Zend Framework 2.0 . I use os x lion and mamp pro. So to start I used this http://framework.zend.com/manual/2.0/en/user-guide/skeleton-application.html all good, I see zend project index page, but
To test that your .htaccess file is working, navigate to http://zf2-tutorial.localhost/1234 and you should see this:
If you see a standard Apache 404 error, then you need to fix .htaccess usage before continuing. If you’re are using IIS with the URL Rewrite Module, import the following:
RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^.*$ index.php [NC,L]
So I do this, import this lines to .htaccess file in public folder, but nothing change, I have 404 error,
I try to create action named "edit" in indexController, for example, but if I go to
http://zf2-tutorial.localhost/index/edit/ I have 404 error.
What I must do to make it work ?????????????????????
Validate that your httpd.conf has the rewrite module enabled. Usually in /etc/apache2, make sure you have the line LoadModule rewrite_module libexec/apache2/mod_rewrite.so in the module section.
If you have default MAMP installation you can go to phpinfo from MAMP start page by clicking phpinfo tab on top. In phpinfo page you can find if mod_rewrite have been loaded. Just click Cmd + f (on Mac) and type mod_rewrite. It should be in "Loaded Modules" section.
Now you should setup your VirtualHost. Open /Applications/MAMP/conf/apache/httpd.conf and add this to end of this file:
<VirtualHost *:8888>
ServerName zf2-tutorial.localhost
DocumentRoot /absolute/path/to/your/projects/zf2-tutorial/public
SetEnv APPLICATION_ENV "development"
<Directory /absolute/path/to/your/projects/zf2-tutorial/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Now add this line
127.0.0.1 zf2-tutorial.localhost localhost
to your /private/etc/hosts file.
Restart MAMP.
After going to http://zf2-tutorial.localhost:8888/ you should see "Welcome to Zend Framework 2" start page. After going to http://zf2-tutorial.localhost:8888/1234 you should see 404 ZF2 page the content of this page should be something like that:
If you see something like that everything is perfectly normal. It means that your mod_rewrite is working and you have been redirected to 404 ZF2 page insted of default apache "Not Found" page like that

How to change the directory public assets are served from in rails?

My setup:
thin running on port 1234 with --prefix /foobar
apache running on port 80
apache reverse proxies /foobar to thin on port 1234
I would like the static assets to not be served via the proxy to thin, but instead be served at /assets directly via apache instead.
I have to use a relative path because I don't know the host name/ip of the rails application before startup (it's app-in-a-box that should be able to be moved around).
I found config.action_controller.asset_host in production.rb, but I can't set that to a relative path. When I do it gets confused and creates really bogus URLs.
How can I make this work?
You don't need to call it through the config block in the environment, you can call it from the application controller, which gives you access to the request object. So you could do:
class ApplicationController < ActionController::Base
before_filter :set_asset_url
def set_asset_url
ActionController::Base.asset_host = "http://#{request.host}"
end
end
It feels a little hackish but I know of no better way.
and if you need to worry about ssl and different ports, you could go crazy:
ActionController::Base.asset_host = "http#{request.ssl? ? 's' : ''}://#{request.host_with_port}"
This depends in your server environment somewhat, but basically you want something along the lines of what is described here: http://blog.codahale.com/2006/06/19/time-for-a-grown-up-server-rails-mongrel-apache-capistrano-and-you/
First, I want to thank Geoff and darkliquid. I took what was in darkliquid's link and worked on it to make it work for my case. The big challenge was that I wasn't serving the rails application from the root of the webserver.
Notes:
thin is run with --prefix '/railsapp' on port 9999.
This works for windows and linux. W00T!
I have to use the LA-U (look-ahead) to get the final filename apache would use.
The IS_SUBREQ check is to prevent the look-ahead (a sub request) from ever returning the proxy.
The /railsapp/index.html rewrite is required because otherwise another rule in my apache conf would rewrite it to /index.html, which is a default 'here's what's here' page; 404s are served elsewhere, though.
Here's the relevant part of the apache configuration:
# Only proxy the thin stuff.
<Proxy /railsapp/*>
Order deny,allow
Allow from all
</Proxy>
## Add an alias for filename mapping.
Alias /railsapp "/website/root/railsapp/public"
## We need the Rewrite Engine for this.
RewriteEngine on
<IfDefine debug>
## If debugging, turn on logging.
RewriteLogLevel 9
RewriteLog "/website/logs/http.rewrite.log"
</IfDefine>
## Check for a static root page.
RewriteRule ^/railsapp/?$ /railsapp/index.html [QSA]
## Check for Rails caching.
RewriteRule ^(/railsapp/[^.]+)$ $1.html [QSA]
## Redirect all non-static requests to Rails.
# Don't proxy on sub-requests (needed to make the LA-U work).
RewriteCond %{IS_SUBREQ} false
# Use look-ahead to see if the filename exists after all the rewrites.
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
# Proxy it to Rails.
RewriteRule ^/railsapp(.*)$ http://127.0.0.1:9999%{REQUEST_URI} [P,QSA,L]
## Make sure Rails requests are reversed correctly.
ProxyPassReverse /railsapp http://127.0.0.1:9999/railsapp
## Disable keepalive; railsappd doesn't support them.
SetEnv proxy-nokeepalive 1

what's the best way to force a domain with a rails app? ex: example.com instead of www.example.com

My rails app seems to break when it answers on www.example.com, it previously was working fine with just example.com...however I've moved servers recently and would like to know the best way to redirect all www.example.com requests to go to http://example.com/.../
thanks.
This should do the trick, assuming that you have mod_rewrite enabled
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.com
RewriteRule ^(.*)$ http://example.com$1 [R=301,L]
It depends on your server setup and there are different ways. You can just cname www in DNS to the root domain, for one way. If you are running Rails behind Apache then you can do it in Apache with mod_rewrite. If you are taking network requests straight into Rails with eg, mongrel (or webbrick) then you may have to configure those servers, or might have to use Rails routes ?
Hope that helps,
adricnet
According to the Apache's name-based virtual host documentation the first virtual host is the default host. I use this to make the first entry a catch-all that redirects every "undefined" request to the main site:
# Default catch-all
<VirtualHost *:80>
# Note the lack of a ServerName
RewriteRule ^(.*)$ http://www.example.com$1 [redirect=permanent]
</VirtualHost>
# Site 1 - www.example.com
<VirtualHost *:80>
ServerName www.example.com
[ the rest of the site config ]
</VirtualHost>
For extra credit you can set up a wildcard DNS entry so that every undefined host (e.g. asdfasdfasdfas.example.com) gets redirected to www.example.com.
You might not want to hear it but I suggest fixing what breaks instead. You've hard coded your domain somewhere in your app, probably in your routing but it is not possible to tell without the specifics of the error(s), and you need to remove that so you, or some other maintainer, won't have to deal with it again in the future.

Resources