Apache Virtual Hosts with Multiple wordpress blogs - mysql

I'm running a VPS # slicehost with Linux 9.10. I've got a basic Lamp setup so far. I am hosting a few sites and adding a half dozen more, and I'd like almost all of them to have word press blogs available.
I tried installing wordpress into each directory following the normal instructions thinking that apache virtual hosts, since it finds each domain, would by extension follow each domain's /blog/ directory(I've tried leaving it the default of /wordpress/ with no results either). WordPress also installed and worked fine when I set it up in the var/www directory where I originally set it up before realizing it would only host one site per installation.
I need wordpress to respond with one of many blogs which is matched to the domain it serves via virtual hosts off the same LAMP setup.
If this is a problem of layout, and there is a better way to host multiple, completely separate wordpress blogs on a single server lamp setup with virtual hosts, I'm open to that as well.
Thank you in advance for any assistance.
I'll be keeping an eye on this to be able to answer any questions.
Oh, Also, I've made separate mysql Db's & usernames for each blog to use to keep them compartmentalized.

Simply set up different VirtualHost directories and put a wordpress installation in each.
Example of your virtualhosts
<VirtualHost 12.34.56.78:80>
ServerAdmin admin#site1.com
ServerName site1.com
ServerAlias www.site1.com
DocumentRoot /srv/www/site1.com/public_html/
</VirtualHost>
<VirtualHost 12.34.56.78:80>
ServerAdmin admin#site2.com
ServerName site2.com
ServerAlias www.site2.com
DocumentRoot /srv/www/site2.com/public_html/
</VirtualHost>
<VirtualHost 12.34.56.78:80>
ServerAdmin admin#site3.com
ServerName site3.com
ServerAlias www.site3.com
DocumentRoot /srv/www/site3.com/public_html/
</VirtualHost>
In each of the respective public_html directories put in a wordpress installation.
So for site1.com the wordpress can be put into /srv/www/site1.com/public_html/wp
For site2.com /srv/www/site2.com/public_html/wp
This will completely keep them seperate.

Thanks for the answer. I'm not sure, but I think this is what i've done with a different directory and it isn't finding the WP directory, even though apache finds the index.html for each site in the exampleX.com directories. So to be clear, It loads example1.com/index.html, but not example1.com/wordpress/ even though /wordpress/ is in the same directory as the index.html(each index.html is labeled internally to make sure i'm not pointing to the wrong one somehow).
I'm serving out of home/public_html/example1.com, home/public_html/example2.com, etc.
The virtual hosts file in sites-enabled points to /home/user/public_html/ as document root.
is the /srv/ directory the important part somehow? I thought I could set the pointers to essentially where ever was convenient?
Do I need to change something else other than 000-default in the sites-enabled directory to point?
--- as I started looking at this and thinking about it, I went and checked over my virtual hosts file. It turns out I wrote the document root all the way to /public, which is at the same level as /blog/ So I backed it up to example1.com with a forwarding href in index.html for example1.com pointed to example1.com/blog/index.html(which points to index.php for wp) which brings up the famous 5 minute installation. This works well since this site is only a blog. I'll just forward to each exampleX.com/public/ from an index.html in each exampleX.com for the rest and everything should be working fine.
Thank you very much for your help.

Related

I cannot get my virtual host to work on Ubuntu, Apache2

I moved a client's site to a new host. I have several domains/sub-domains/virtual-hosts already working. However, I cannot get this one to resolve. It keeps going to my dominate site. This is a very simple one page html page. No php, no special anything literally a one page html site.
I have it going though Cloudflare and directed to my ip with "A" & "CNAME" <-- that is it, again, nothing special.
I am on:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.4 LTS
Release: 20.04
Codename: focal
With LAMP Configuration.
<VirtualHost *:80>
ServerAdmin admin#bluebonnet-realty.com
ServerName bluebonnet-realty.com
ServerAlias www.bluebonnet-realty.com
DocumentRoot /var/www/bluebonnet-realty.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<Directory /var/www/bluebonnet-realty.com/>
AllowOverride All
</Directory>
I purged all cache in Cloudflare however, it continues to resolve to the main site.
https://bluebonnet-realty.com goes to https://beardedrazorback.com
I added to the host file as follows
myip bluebonnet-realty.com
I have tried cleared history on PC, tried incognito and 4 different computers including my mobile device.
I can share logs, but I did not see anything jump out. But if you need to see them I can share.

Redirect HTML "/" to subfolder of WWW directory, the VirtualHost DocumentRoot

Sorry for asking this question again. Even though this has been asked and discussed quite a bit I cannot seem to find the right solution for a local dev environment using VirtualHost. I am using XAMPP Portable for Windows for dev work but assume this is the same for any other local server with regards to the .htaccess file.
DocumentRoot of VirtualHost is D:\dev\www\ for example.
ServerName is devwork.webdev for example.
HOSTS entry is 127.0.0.1 devwork.webdev.
VirtualHost file does have a default DocumentRoot being DocumentRoot "D:/xampp/htdocs". It works just fine.
The projects are each in a folder under D:\dev\www\ for example D:\dev\www\project01\ or D:\dev\www\project02\ and so on and show nicely in the browser when going to devwork.webdev with Options Indexes FollowSymLinks enabled. Apache is not showing any error and the access log file is also OK, things are working.
Now in my HTML when I use Project 01 a click on the link does link me to D:\dev\www\ showing all the projects I have in that folder.
Instead I would like to be linked to the root of the project, being D:\dev\www\project01\ or rather http://devwork.webdev/project01/.
How can I get that to work?
I am looking for a solution to this so that I can do dev work locally and without changing the HTML later FTP the data to the live host's root and it will work.
I have read and tried the following:
http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
https://perishablepress.com/redirect-subdirectory-to-root-via-htaccess/ .htaccess How to redirect root URL to subdirectory files, rewrite to clean URL AND not affect subdomains?
http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
https://stackoverflow.com/a/990405/1010918
How to redirect /directory/index.html and /directory/index.php to /directory/
Redirecting /directory/index.html to /directory/
with
How to remove .html from URL
and
http://forums.modx.com/thread/77211/endless-friendly-url-redirect-from-subdomain-folder-location
being closest to what I think I need but I must be doing something wrong since I always arrive at the DocumentRoot instead of the folder where the project is kept under the DocumentRoot.
Thank you for any help.
This seems closest:
Add VirtualHost definition for each project you want to access. (I'm
not sure how to do it on XAMPP for Windows).
For example, project01.devwork.webdev...
Set DocumentRoot for this VirtualHost to D:\dev\www\project01...
Add the hostname to your /etc/hosts/ file.
Open http://project01.devwork.webdev/ in your browser.
You should see the application in D:\dev\www\project01, while all URLs
will be based on "/".
What happens in the background:
When you open the URL http://project01.devwork.webdev/ in your browser, it will (as usual) translate it to IP address, but along with the request, it will also send Host header with the entered hostname:
GET / HTTP/1.1
Host: project01.devwork.webdev
Based on the Host field, Apache will decide which VirtualHost it needs to "pretend" to be, and serve files from the respective direcory.
However, if you need to have index of the projects, you will have to create it manually with full URLs.
An alternative with the given logic would be to simply add a VirtualHost for each project in D:dev\www\projectname by giving it its own domain instead of a subdomain on the host.
There is little difference there now when already editing httpd-vhosts.conf (using XAMP this file is in InstallDir and then in apache\conf\extra )but ideally it makes copying the local HTML to the live server without changing the HTML possible.
So this is something devs doing local work should keep in mind. I certainly will!! Thank you for your help and information.
<VirtualHost *:80>
DocumentRoot "D:/dev/www/newprojectname"
ServerName newdomainname.webdev
ServerAlias www.newdomainname.webdev
ErrorLog "D:/dev/www/log/dev-apache.error.log"
CustomLog "D:/dev/www/log/dev-apache.access.log" common
<Directory "D:/dev/www/newprojectname">
AllowOverride All
Options Indexes FollowSymLinks
Require local
# more detailed local
# Require ip 192.168.188
# or the IP from your local network
</Directory>
</VirtualHost>

Photos are not showing up when using No-IP ddns and linux apache

I don't have a static IP address so a person recommended that I use no-ip.com
I did and now my website is accessible from external internet yay
The problem now is that my photos aren't showing, I thought this would have been a simple issue
I'm not sure if I have to do something with Virtual Host or...
My domain name is greenace92.ddns.net
I can access my index.html file, that shows up fine but the photos aren't showing
On my httpd.conf file I have
ServerRoot "/usr/local/apache2"
The html files are in htdocs under apache2
So, on virtual host that httpd-vhosts.conf file I have the following
ServerAdmin "jacobcun#ricekidengineer.com"
DocumentRoot "/usr/local/apache2/"
ServerName "www.greenace92.ddns.net"
*ricekidengineer.com is my other domain with email configured
I didn't change the error logs from stuck "dummy" stuff
So on my html file for image href's I have
http://www.greenace92.ddns.net/htdocs/images/normalbus-icon-on.png
I assume that www.greenace92.ddns.net is /usr/local/apache2
So I'm just continuing the directory location
Is that correct? What is wrong?
Yeah I don't understand, I even tried three different directories for a few photos, trying directly in apache2, then htdocs, then the images folder
What am I not seeing?
You will have to decide what your want as the domain for your site:
www.greenace92.ddns.net is not setup for your web site
greenace92.ddns.net is the domain setup for your web site.
You either have to
configure no-ip.com to point to the domain www.greenace92.ddns.net
(or)
change your apache ServerName directive to
ServerName "greenace92.ddns.net"
and change the image URLs in your HTML pages from something like
http://www.greenace92.ddns.net/htdocs/images/normalbus-icon-on.png
to
http://greenace92.ddns.net/images/normalbus-icon-on.png
Note: With your current configuration, the 2nd URL mentioned above should already work (It worked when I tested!).

Two XAMPP WordPress Installations, pointing one to a different URL, both not working correctly

I am running Windows 7 with an XAMPP local server with an installation of WordPress. I know this one works correctly, I've used it many times. I have a MySQL database for it and it sits in htdocs/wordpress.
Now, I have a contract in which I'm working for someone on another WordPress theme and would like to have this as a separate database and install. So, I make another folder in htdocs, let's call it folder2. I then create another database in MySQL, import the db I was given, and create a user to match the wp-config.php file of this install. No problem, except that it points to "localhost/folder2" and I need it to point to "local.folder2.com" to match the contractor.
So I hunt around the web and stumble on VirtualHosts for XAMPP. I go to /System32/drivers/etc/hosts and add the domain.
127.0.0.1 localhost ## Already here for XAMPP purposes
127.0.0.1 local.folder2.com ## URL to point localhost/folder2 towards
I then go to /xampp/apache/conf/extra/httpd-vhosts.conf and add the necessary Virtual Host.
NameVirtualHost *:80
<VirtualHost *:80>
DirectoryRoot "C:/xampp/htdocs/folder2"
ServerName local.folder2.com
</VirtualHost>
I restart Apache and MySQL. Problem is, now local.folder2.com works, but "localhost/wordpress/" does not. Using that URL directs me to the theme and db of folder2, baffling me entirely. Any ideas? I realize I can just work on one and not the other, but if I ever have more than one client at a time, switching it all out isn't going to work.
In this case, localhost stopped working because when another vhost was declared, no longer did the inherent connection between http://localhost.com and the directory "/htdocs" work. The solution was to add a vhost for the original location and then add a second one for the new location. Simple as:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/xampp/htdocs/wordpress" # Where I had localhost pointing
</VirtualHost>
<VirtualHost *:80>
ServerName local.folder2.com
DocumentRoot "C:/xampp/htdocs/folder2"
<Directory "C:/xampp/htdocs/folder2">
Allow Override All
</Directory>
</VirtualHost>
Of course, you then must add them to your hosts file:
127.0.0.1 localhost
127.0.0.1 local.folder2.com
Restart Apache server, and you're good to go.

Wildcard Subdomains

I know there have been a few threads on this before, but I have tried absolutely everything suggested (that I could find) and nothing has worked for me thus far...
With that in mind, here is what I'm trying to do:
First, I want to allow users to publish pages and give them each a subdomain of their choice (ex: user.example.com). From what I can gather, the best way to do this is to map user.example.com to example.com/user with mod_rewrite and .htaccess - is that correct?
If that is correct, can somebody give me explicit instructions on how to do this?
Also, I am doing all of my development locally, using MAMP, so if somebody could tell me how to set up my local environment to work in the same manner (I've read this is more difficult), I would greatly appreciate it. Honestly, I have been trying a everything to no avail, and since this is my first time doing something like this, I am completely lost.
Some of these answers have been REALLY helpful, but for the system I have in mind, manually adding a subdomain for each user is not an option. What I'm really asking is how to do this on the fly, and redirect wildcard.example.com to example.com/wildcard -- the way Tumblr is set up is a perfect example of what I'd like to do.
As far as how to set up the DNS subdomain wildcard, that would be a function of your DNS hosting provider. This would be different steps depending on which hosting provider you have and would be a better question for them.
Once you've set that up with the DNS host, from your web app you really are just URL rewriting, which can be done with some sort of module for the web server itself, such as isapi rewrite if you're on IIS (this would be the preferred route if possible). You could also handle rewriting at the application level as well (like using routing if on ASP.NET).
You'd rewrite the URL so http://myname.example.com would become http://example.com/something.aspx?name=myname or something. From there on out, you just handle it as if the myname value was in the query string as normal. Does that make sense? Hope I didn't misunderstand what you're after.
I am not suggesting that you create a subdomain for each user, but instead create a wildcard subdomain for the domain itself, so anything.example.com (basically *.example.com) goes to your site. I have several domains setup with MyDomain. Their instructions for setting this up is like this:
Yes, you can configure a wild card but
it will only work if you set it up as
an A Record. Wildcards do not work
with a C Name. To use a wildcard, you
use the asterisks character ''. For
example, if you create and A Record
using a wild card, *.example.com,
anything that is entered in the place
where the '' is located, will resolve
to the specified IP address. So if you
enter 'www', 'ftp', 'site', or
anything else before the domain name,
it will always resolve to the IP
address
I have some that are setup in just this way, having *.example.com go to my site. I then can read the base URL in my web app to see that ryan.example.com is what was currently accessed, or that bill.example.com is what was used. I can then either:
Use URL rewriting so that the subdomain becomes a part of the query string OR
Simply read the host value from the accessed URL and perform some logic based on that value.
Does that make sense? I have several sites set up in just this exact way: create the wildcard for the domain with the DNS host and then simply read the host, or base domain from the URL to decide what to display based on the subdomain (which was actually a username)
Edit 2:
There is no way to do this without a DNS entry. The "online world" needs to know that name1.example.com, name2.example.com,..., nameN.example.com all go to the IP address for your server. The only way to do this is with the appropriate DNS entry. You have to add the wildcard DNS entry for your domain with your DNS host. Then it's just a matter of you reading the subdomain from the URL and taking the appropriate action in your code.
The best thing to do if you are running *AMP is to do what Thomas suggests and do virtual hosts in Apache. You can do this either with or without the redirect you describe.
Virtual hosts
Most likely you will want to do name-based virtual hosts, as it's easiest to set up and only requires one IP address (so will also be easy to set up and test on your local MAMP machine). IP-based virtual hosts is better in some other respects, but you have to have an IP address for each domain.
This Wikipedia page discusses the differences and links to a good basic walk-thru of how to do name-based vhosts at the bottom.
On your local machine for testing, you'll also have to set up fake DNS names in /etc/hosts for your fake test domain names. i.e. if you have Apache listening on localhost and set up vhost1.test.domain and vhost2.test.domain in your Apache configs, you'd just add these domains to the 127.0.0.1 line in /etc/hosts, after localhost:
127.0.0.1 localhost vhost1.test.domain vhost2.test.domain
Once you've done the /etc/hosts edit and added the name-based virtual host configs to your Apache configuration file(s), that's it, restart Apache and your test domains should work.
Redirect with mod_rewrite
If you want to do redirects with mod_rewrite (so that user.example.com isn't directly hosted and instead redirects to example.com/user), then you will also need to do a RewriteCond to match the subdomain and redirect it:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com
RewriteRule ^(.*)$ http://example.com/subdomain$1 [R]
You can put this in a .htaccess or in your main Apache config.
You will need to add a pair of rules like the last two for each subdomain you want to redirect. Or, you may be able to capture the subdomain in a RewriteCond to be able to use one wildcard rule to redirect *.example.com to example.com/
-- but that smells really bad to me from a security standpoint.
All together, vhosts and redirect
It's better to be more explicit and set up a virtual host configuration section for each hostname you want to listen for, and put the rewrite rules for each of these hostnames inside its virtual host config. (It is always more secure and faster to put this kind of stuff inside your Apache config and not .htaccess, if you can help it -- .htaccess slows performance because Apache is constantly scouring the filesystem for .htaccess files and reparsing them, and it's less secure because these can be screwed up by users.)
All together like that, the vhost config inside your Apache configs would be:
NameVirtualHost 127.0.0.1:80
# Your "default" configuration must go first
<VirtualHost 127.0.0.1:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /www/siteroot
# etc.
</VirtualHost>
# First subdomain you want to redirect
<VirtualHost 127.0.0.1:80>
ServerName vhost1.example.com
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/vhost1$1 [R]
</VirtualHost>
# Second subdomain you want to redirect
<VirtualHost 127.0.0.1:80>
ServerName vhost2.example.com
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/vhost2$1 [R]
</VirtualHost>
I realize that I'm pretty late responding to this question, but I had the same problem in regards to a local development solution. In another SO thread I found better solutions and thought I would share them for anyone with the same question in the future:
VMware owned wild card domain that resolves any subdomain to 127.0.0.1:
vcap.me resolves to 127.0.0.1
www.vcap.me resolves to 127.0.0.1
or for more versatility 37 Signals owns a domain to map any subdomain to any given IP using a specific format:
127.0.0.1.xip.io resolves to 127.0.0.1
www.127.0.0.1.xip.io resolves to 127.0.0.1
db.192.168.0.1.xip.io resolves to 192.168.0.1
see xip.io for more info
I am on Ubuntu 16.04 and since 14.04 I've using solution provided by Dave Evans here and it works fine for me.
Install dnsmasq
sudo apt-get install dnsmasq
Create new file localhost.conf under /etc/dnsmasq.d dir with the following line
#file /etc/dnsmasq.d/localhost.conf
address=/localhost/127.0.0.1
Edit /etc/dhcp/dhclient.conf and add the following line
prepend domain-name-servers 127.0.0.1;
(You’ll probably find that this line is already there and you just need to uncomment it.)
Last one is restart the service
sudo systemctl restart dnsmasq
sudo dhclient
Finally, you should check if it's working.
dig whatever.localhost
note:
If you want to use it on your web server, you need to simply change the 127.0.0.0 to your actual IP address.
I had to do exactly the same for one of my sites. You can follow the following steps
If you've cPanel on your server, create a subdomain *, if not, you'd have to set-up an A record in your DNS (for BIND see http://ma.tt/2003/10/wildcard-dns-and-sub-domains/). On your dev. server you'd be far better off faking subdomains by adding each to your hosts file.
(If you used cPanel you won't have to do this). You'll have to add soemthing like the following to your apache vhosts file. It largely depends on what type of server (shared or not) you're running. THE FOLLOWING CODE IS NOT COMPLETE. IT'S JUST TO GIVE DIRECTION. NOTE: ServerAlias example.com *.example.com is important.
<VirtualHost 127.0.0.1:80>
DocumentRoot /var/www/
ServerName example.com
ServerAlias example.com *.example.com
</VirtualHost>
Next, since you can use the PHP script to check the "Host" header and find out the subdomain and serve content accordingly.
First, I want to allow users to
publish pages and give them each a
subdomain of their choice (ex:
user.mysite.com). From what I can
gather, the best way to do this is to
map user.mysite.com to mysite.com/user
with mod_rewrite and .htaccess - is
that correct?
You may be better off using virtual hosts. That way, each user can have a webserver configuration pretty much independent of others.
The syntax goes something like this:
<VirtualHost *:80>
DocumentRoot /var/www/user
ServerName user.mysite.com
...
</VirtualHost>
From what I have seen on many webhosts, they setup a virtual host on apache.
So if your www.mysite.com is served from /var/www, you could create a folder for each user. Then map the virtual host to that folder.
With that, both mysite.com/user and user.mysite.com works.
As for your test enviroment, if you are on windows, I would suggest editing your HOSTS file to map mysite.com to your local PC (127.0.0.1), as well as any subdomains you set up for testing.
The solution I found for Ubuntu 18.04 is similar to this one but involves NetworkManager config:
Edit the file /etc/NetworkManager/NetworkManager.conf, and add the line dns=dnsmasq to the [main] section
sudo editor /etc/NetworkManager/NetworkManager.conf
should look like this:
[main]
plugins=ifupdown,keyfile
dns=dnsmasq
...
Start using NetworkManager's resolv.conf
sudo rm /etc/resolv.conf
sudo ln -s /var/run/NetworkManager/resolv.conf /etc/resolv.conf
Create a file with your wildcard configuration
echo 'address=/.localhost/127.0.0.1' | sudo tee /etc/NetworkManager/dnsmasq.d/localhost-wildcard.conf
Reload NetworkManager configuration
sudo systemctl reload NetworkManager
Test it
dig localdomain.localhost
You can also add any other domain, quite useful for some types of authentication when using a local development setup.
echo 'address=/.local-dev.workdomain.com/127.0.0.1' | sudo tee /etc/NetworkManager/dnsmasq.d/workdomain-wildcard.conf
Then this works:
dig petproject.local-dev.workdomain.com
;; ANSWER SECTION:
petproject.local-dev.workdomain.com. 0 IN A 127.0.0.1