Link to home
Start Free TrialLog in
Avatar of Marthaj
MarthajFlag for United States of America

asked on

Ubuntu 21.04 and creating virtual host and location of websites

I am migrating from Windows 10 platform running Wampserver with PHP 7.4 to Ubuntu 21.04.
I have been reading and reading, and am now confused.
I have three websites that I created under /var/www - site 1, site 2, site 3 on my Ubuntu computer ( all Ubuntu - no dual environment)
I wanted to create a virtual host for each one i.e. site1.com, site 2.com site3.com
In all the examples I have seen, the examples  create a .com under public_html.
 I find this confusing. Does that mean I need to move my websites to there instead of having them at /var/www ? I want to be able to execute my apps in each website.
In windows, these websites were under wamp/www/site1 etc.
 And I executed the app I wanted by using: http://localhost:8081/site1/some.php.
How does all this tie together ? Can they all three used 80 ??
I understand that I need to create a site1.com,conf file etc.
i do not find a public_html folder. But as I understand it, I can use home/USER/public_html.
I do nit have a public_html  in there either. Is that a folder I need to create ? And then should I place my websites there instead of /var/www ??
Could someone explain it to me ?
Thank you for any help - it is appreciated.


Avatar of Kimputer
Kimputer

Every distro with their default Apache2 packaging, has slightly different defaults, both in base folders as well as other settings.
Usually it's smarter to adjust to that, then figuring out the way you want it. That's because if /var/www IS the default base folder, the rights to that folder has already been set to the correct group/user.
Having sub folders there, and adjusting the .conf file is the fastest way to get going. They're all available on port 80, as the browser calling the server, will let it know which site it wants. And if that particular site is configured correctly in the .conf file, it will be served.
ASKER CERTIFIED SOLUTION
Avatar of Jim Riddles
Jim Riddles
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Several considerations... for your answers...

1) I'd use Focal (20.04) for 5+ years of updates rather than Hippo (21.04) where all repositories will disappear... likely around March of next year, so if you use Hippo... all the work today, you'll have to duplicate again sometime after repositories disappear, because at this point you'll have no further security patches or upgrades of any form available.

2) The "Debian/Ubuntu Way" of managing host/domain configs is elegant, stick with the Ubuntu "way"... which means...

1x config file for host www + bare domain.

1x separate config file for every other host.

4) The following URL discusses how to setup all latest stable PPAs (repositories), to install latest stable code on any Linux Distro.

https://www.experts-exchange.com/questions/29207737/Describe-the-steps-to-install-Apache-FPM-PHP-to-correctly-serve-HTTP2.html

This ensures you have every stable code release installed, a few days after each project releases updates, then the Ubuntu project packages the update.

5) The URL from #4 also covers how to get HTTP2 working.

6) The following URL provides the Apache per host config file I use for site setup.

https://www.experts-exchange.com/questions/29167303/How-To-Re-direct-all-Traffic-to-WWW-Apache.html

Just rename the variables like WEBSITE to your domain.

This URL also covers using the direct LetsEncrypt cert paths for HTTPS implementation.

7) For all front facing code I use the following directory structure...

/sites/$project/$site/{wordpress,htdocs}

Open in new window


The /var/www/html directory structure is owned by the APT process, so there's the occasional problem of an update destroying a site slightly.

This is rare + easily recovered... usually...

I just have some many sites running, I avoid any potential problems, so use a different DocRoot structure.

8) I run LXD so all code - Sites/Apps/APIs - get their own LXD container + separate public IP.

If you're running local only HTTP code, you can adjust your port structure any way.

If you're running public facing code, then easiest approach is using LXD to separate each container, provide public IP (so HTTPS works), then use port 80 + port 443, as trying to run HTTPS on non-443 ports is complex.
1) I have three websites that I created under /var/www - site 1, site 2, site 3 on my Ubuntu computer ( all Ubuntu - no dual environment)

No clue what "dual environment" might mean.

2) I wanted to create a virtual host for each one i.e. site1.com, site 2.com site3.com

See Apache config file EE URL above.

3) In all the examples I have seen, the examples  create a .com under public_html.

Normally public_html is a CPanel-ism. Since CPanel is best avoided + only runs on RedHat derivatives, no consideration here.

Also public_html is just a directory name.

No reason you must use this... directory structure naming approach...

 4) I find this confusing. Does that mean I need to move my websites to there instead of having them at /var/www ? I want to be able to execute my apps in each website.

See conversation above for 1x approach to DocRoot naming.

So for me, I might have the following...

/sites/david-favor/davidfavor.com/htdocs
/sites/david-favor.com/dev.davidfavor.com/wordpress
/sites/david-favor/punchdrunktech.com/htdocs
/sites/david-favor/radicalhealth.com/wordpress

Open in new window


Notice this approach self documents... who owns the sites, actual site name (host/domain), whether site is WordPress or static...

5) In windows, these websites were under wamp/www/site1 etc.

This is just a path structure. You can change this as your like.

Most important consideration to me, is separate all sites, so if one site gets hacked... hack... never... leaks to other sites...

6) And I executed the app I wanted by using: http://localhost:8081/site1/some.php.

This is also just an arbitrary structure.

Per above, if you use HTTP only then you can run any port you like.

Running HTTPS this becomes very difficult, especially when HSTS is used as most browsers... incorrectly... pin to port 443, breaking HTTPS on any other port.

Hence why I use LXD + separate IPs for each project.

7) How does all this tie together ? Can they all three used 80 ??

Yes. Again this is arbitrary.

You can run 1x instance of Apache where all sites share port 80.

Or 3x instances of Apache using 3x different ports.

8) I understand that I need to create a site1.com,conf file etc.
i do not find a public_html folder. But as I understand it, I can use home/USER/public_html.

This is all arbitrary + usually a CPanel-ism.

9 I do not have a public_html  in there either. Is that a folder I need to create ? And then should I place my websites there instead of /var/www ??

Correct.

Debian/Ubuntu don't use the public_html form by default.

10) Could someone explain it to me ?

Hopefully the above provides some clarity for you.
Avatar of Marthaj

ASKER

Thank you all for responding. I have a lot of information to digest and consider. I am not sure where
I am going with this but it has cleared up some confusion in my mind.
Jim - I have a question and I know this sounds stupid, but I want to be sure about this:
/var/virtualhosts/site1.mysite.com/www
/var/virtualhosts/site2.mysite.com/www
/var/virtualhosts/site3.mysite.com/www

Open in new window

Does the foregoing coding mean you have 3 /var/virtualhosts folders or 1 /var/virtualhosts with 3 different sites underneath it ?
And does the webpages then be placed under the www ?
What about being able to copy files etc.  to them ??
That always seems to be a problem for me - access privileges.
Thank you for your help/
I will be posting again to this question in a day or two after I digest the suggestions given here.
I appreciate you all for your patience in my responding.
Getting started is always the toughest !


The above references three distinct virtual hosts.  The folder hierarchy can be whatever you want it to be.  It could just as easily be the following:
/home/firstuser/public_html/
/var/www/
/var/virtualhosts/site3/www

Open in new window

The layout in my original post is just my personal preference.  The key is that the configuration files reference the correct folder.  Additionally the user that your web server runs as needs to have read permissions for each folder.  If it is an app that is also going to need to write files to that path it will need write privileges.

In my original layout all files related to your website should be within the www folders.  Note that if you have configuration files that contain sensitive information I would store them in the parent folder of your site root (ex. /var/virtualhosts/site1.mysite.com) so no web users will be able to directly access those files.

I'm not certain what your statement means "What about being able to copy files etc. to them ??".  If you just mean adding your HTML, JavaScript, images, etc...then of course whatever user you use to upload files to those sites needs to have write privileges.  If you're the only developer that will be managing files this is a simple task.  If you are going to have multiple users that will be managing specific sites then you will naturally have to come up with a permission scheme to deal with that.  Which situation describes your needs?
Avatar of Marthaj

ASKER

I think I need to clarify something here. I am only trying to get set-up to test my applications.
As it is right now, I keep running into permissions problem when trying to copy files to the structure of:

/var/virtualhosts/somesite.com/www

Open in new window


When I want to execute a program in one of my sites, using the structure above,  what would the syntax
be in my browser window So far, I have struck out on getting it correct.
I am also trying your suggestion David. that seems to be the easiest with copying files.
I see value in both responses.
Avatar of Marthaj

ASKER

Jim- thank you for responding.  I thought as much, as to placement of  directory structure and coordinating with the conf files. I just want to be sure I understand correctly. I am so new to Ubuntu and after years with Windows, quite a change. But I like Ubuntu much better. The biggest problem I am having is being able to copy the files to folders and receiving 'permission denied' errors. Currently I am using your virtual host structure to see how that flies for me.The concern down the road is editing my app in the site - this seems to be a problem. Again permissions.
No one will be accessing my sites as they are for development and local to only my computer.
In other words, I am using localhost. Or that is what I am trying to achieve.
Avatar of Marthaj

ASKER

Okay, I have been experimenting with the information you folks have provided and am stuck on a permissions issue.
After I created my directories, I did this:
sudo su
chmod g+s /var/virtualhosts/site1.com/www
chmod o-rwx /var/virtualhosts/site1.com/www
sudo service apache2 restart

Open in new window

And when I try to copy my project into the www folder, it says:
the folder worklogs cannot be copied because you do not have permissions to create it in the destination

Open in new window

All of my projects have sub-folders. So then I did this:
chmod -R 0777 /var/virtualhosts/site1.com/www

Open in new window

I know I could have used 777 but it is a octal number and therefore should be 0777.
I was able to copy all my files and folders into it. The smart thing to do would now be changing those permissions.
But since I am the only one accessing it, it probably doesn't matter all that much.
I will be posting more later.
Great, since you are the only one, you can use a similar structure that I do.  First an explanation of how permissions work in Linux in general.  If you execute the command "ls -a" in a terminal window in the folder "/var/virtualhosts" you will see a list of all the folders in that folder.  The first column displays the permissions for each folder.  The next column displays the user who owns the folder, followed by the group.

Use the following commands to replicate my setup:
sudo chown -r username:root site1.mysite.com/
sudo chmod 0755 site1.mysite.com/
cd site1.mysite.com/
chmod 0755 www/

Open in new window

IMPORTANT: Be sure to change "username" to your username you use to access your system.
The first command changes the owner of the root web folder to your user and the root group.
The second command changes the permission to give your user full permissions and everybody else read and execute permissions recursively to every folder under the root web folder.  Note that execute is required on the folder to allow the web user to access the folder.
The third command simply moves you into the site1.mysite.com foler.
The fourth command changes the permission for the www folder to be the same as the root web folder.  The reason that I did not have you use the recursive switch is because we don't want those permissions on individual files...only folder.

For any file that you create within the www folder you should use permissions of 0644.  That will give your user read and write privileges and everybody else only read permissions.  Additionally, as I stated before, if you have any config files that contain sensitive information place them directly in the site1.mysite.com folder.  When you reference them from within your pages you can use "../" to backtrack to higher level folders to reach those files.

Once you have those permissions in place you should be able to move files into the directories with no issues.
Avatar of Marthaj

ASKER

Thank you for responding. I have been able to copy my files into the appropriate folder.
Is there any reason why the information for the sites con.conf can not be placed in the default.com.conf file ?
Can it have multiple <virtualhost *:80> entries ??
The only difference I made from you structure for sites was I did this:
var/virtualhosts/site1.com/www
var/virtualhosts/site2.com/www
etc.

Open in new window

Is that okay? And of course I made sure to use the right path in conf file.
When I try to display a page from my site in my browser window - I tried this:
localhost/site1.com/www/index.html

Open in new window

And it said I don't have permissions to access the resources. Is this because I used 0777 ??
When I issued sudo whoami - it gave me root.
 It always ask me for the password before I do anything unless I issue  a sudo su.
When I issued ls -a - it returned this: To make sure I didn't mess up, I exited the terminal mode and then issued whoami again.
And it asked me for the password and returned root.
. .. site1.com

Open in new window

So I did echo $USER and it gave me marthaj - which returned marthaj - which is what I need to use for the username in the permissions.

 sudo chown -r marthaj:root site1.mysite.com/

Open in new window

And it gave me an error - both times I tried - it returned chown --'r' invalid option.
When I did chown --help, it display valid options but -r was not listed.
So I did this:


sudo chmod -r a+rwx /var/virtualhosts/site1.com

Open in new window

That seemed to work.


Avatar of Marthaj

ASKER

Finally have one site working - but deviated from some things. I have certainly acquired  a lot of practice using sudo and nano !
this is what I did:
1. I changed the location of my projects to be under the /var/www folder 
   i.e. /var/www/mysite.com. And added the following to the /etc/host file:

127.0.0.1 mysite.com

2. I did this:
     sudo chown -R www-data: /var/www/mysite.com

3. In the etc/apache2/sites-available folder, I created a mysite.com.conf
 containing this:

<VirtualHost *:80>
    ServerName mysite.com
    ServerAlias www.mysite.com
    ServerAdmin webmaster@mysite.com
    DocumentRoot /var/www/mysite.com
    <Directory /var/www/mysite.com>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/mysite.com-error.log
    CustomLog ${APACHE_LOG_DIR}/mysite.com-access.log combined
</VirtualHost>

4. I then executed:
       sudo a2ensite mysite.com

5.And executed:
       sudo apachectl configtest

6. And received this:
apache2 could not reliably determine the server's fully qualified domain name, 
using 127.0.1.1. Set the 'ServerName' directive globally to supress this message.
Syntax OK

7.And executed:
sudo systemctl restart apache2

And when I type in this my browser window:
http://mysite.com/index.com

 It displayed the Apache 2 Ubuntu Default page
And to double check - I created a phpinfo.php in mysite.com and that displayed correctly.

I have learned a great deal, and I thank you all.
I feel both David and Jim deserve a lot of credit because both of 
them really helped me to finally get here. 
I am sure I may still have some issues, I need to really make sure
 that my work folders  within the mysite. allows read. write etc 
as the application reads, write and deletes files. 
And I want to be sure to be able to copy new pages into the folder.
Since this Ubuntu server is for development only, I did not create a
public folder. (I followed the hierarchy of the existing production server.)
If anyone is unhappy about how I selected the solution, please let me know.
I wish EE would go back to being able to split points. 

Again, I thank you all. :)

Open in new window


1) Is there any reason why the information for the sites con.conf can not be placed in the default.com.conf file ?

You can do this + this file is owned by the update process, so can be overwritten at any moment, based on many factors.

Best avoid this, unless you're a Thrillseeker.

2) I think I need to clarify something here. I am only trying to get set-up to test my applications.

Simple to fix...

chmod 777 /var/virtualhosts

Open in new window


Which is also a massive security hole, so only do this locally.

3) Messages like, "the folder worklogs cannot be copied because you do not have permissions to create it in the destination" are easily fixed by prepending your commands with "sudo" to raise your command's priviledge to root.

4) Using - 127.0.0.1 mysite.com - means you can only access this site, when you're on the actual machine/container/VM where site is running.

5) To suppress the ServerName warning, add this to your apache2.conf file... or just ignore the warning...

ServerName mysite.com
You're welcome!

Hang in there!