Creating Virtual Host on apache not loading styles from root directory - php

I am new to VPS hosting.... and giving a try to digital ocean Linux VPS. I need to point 2 domains to my PHP app.
This is how my Virtual Host looks like inside /etc/apache2/sites-available
<VirtualHost *:80>
ServerAdmin root#domain1.com
ServerName domain1.com
ServerAlias www.domain1.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
However... I am not able to load my styling and access any files or directories that is in var/www/html directory. How can I enable the permissions for my domains to access these resources.
Similarly my second Virtual host for another domain
DocumentRoot /var/www/html/app/login.php
Everything seems working while I am accessing the site using the IP...However... after setting up virtual host... the content is loading but can not access css and js... in the current root directory.
I am not sure what I need to do to set up my domains as expected.
I am using ../ notation to point my previous directory. There is no error with the site that is showing...just not loading CSS. Also I have the APIs in the root directory.... inside API folder.... that is not accessible while I am pointing them ../../API/v1/create_user.php

Related

XAMPP - How can I execute PHP files from home directory?

I've installed XAMPP on my Linux computer and server and database are running. Now I have a question how to execute my existing PHP-Files from /home path?
So far I have saved all of my PHP files in ./home directory (e.g. /home/Path-to-PHP-Files/Development_01/....php) and until now I have uploaded them with FTP to server. In order to save myself from constant uploading, I thought that usage with XAMPP is faster and more elegant.
I found out that XAMPP apparently wants to save the PHP files in /opt/lampp/htdocs directory. Unfortunately I can neither call my PHP files from .home-directory in browser nor can I copy them to /opt/lampp/htdocs because this path is read-only.
How can I configure XAMPP to make my PHP files run in /home directory?
Change the DocumentRoot in your virtual hosts file, then restart XAMPP. Here is an example:
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "/home/Path-to-PHP-Files/Development_01/"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log" common
<Directory C:/home/Path-to-PHP-Files/Development_01/> # or a linux path
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

How to Install my php (mvc architecture) on a apache server. Best way?

Situation:
I have a website that iam working on, in my local machine
Server apache, php language.
In local i can access to the page by typing
http://localhost/(mysite)/public/index.php
Problem:
In the Hosting webserver, i want to access by
simply typing http://www.(mysite).com/index.php
Note: My website is based on mvc (Model view controller) architecture.
Question:
What is the appropriate way to « root » my website on the
« public » directory in my apache server.
Should i point the hosting web server document root to The « public/« directory ?
Thanks
On linux go edit /etc/apache2/sites-available/000-default.conf or mysite.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And append /public to whatever path you have as DocumentRoot

Test apache site on mobile phone locally

I am using ubuntu 16.04 and working on a laravel project.
I have a new laravel project in /var/www/html named myproject and can access it by going to
localhost/myproject/public/
But in order for the routes to work, I found a solution to create a site in /etc/apache2/sites-available like this:
<VirtualHost *:80>
ServerName myproject.dev
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/myproject/public
<Directory /var/www/html/myproject>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
and then added a new entry in /etc/hosts file, like this:
127.0.0.1 localhost
127.0.1.1 myhostname
127.0.0.1 myproject.dev
restarted apache now can access the project using http://myproject.dev in the browser.
Now I want to test some responsive stuff using an actual phone.
I tried http://myproject.dev it doesn't work, also if I do
http://myhostname/myproject/public
It takes me to the landing page, but accessing any other route gives a 404. and this format also doesn't work on the computer browser.
However this works on the computer:
http://localhost/myproject/public
but not on the phone.
How can I access the myproject site on my phone? And also both my computer and phone are connected to the same wifi access point.
Remember you phone does not know about your sites url as it is not in any DNS server and you cannot fiddle with the hosts file on the phone unless you jailbreak it.
What I normally do is create a new Virtual Host to be used when accessing the site from a phone. But on this Virtual Host I use another port number as adding a port number is easy on the phones browser.
So add another VH like this and another Listen command
Listen 81
<VirtualHost *:81>
ServerName myproject.dev
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/myproject/public
<Directory /var/www/html/myproject>
AllowOverride All
# add access from any ip on your subnet
Require ip 192.168.1
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Then assuming the Server is running on 192.168.1.100 you use this url to get to the site
http://192.168.1.100:81
and your routes should work without any fiddling
Add an alias on the IP address to your <VirtualHost>-directive. Something like ServerAlias 192.168.1.100. Then, you should be able to reach the web application on that IP address, like this: http://192.168.1.100/myproject/public.
The main principles at work here are pretty independent of laravel, but more related to DNS and networking, and apache configuration.
Consider using .xip.io solution.
Example:
if your local computer IP address is 192.168.1.2 , and you can visit your local site as http://192.168.1.2/somepage.html, in your phone you can access it with http://192.168.1.2.xip.io/somepage.html. To make this solution work, you need to edit your apache virtualhost, something like:
<VirtualHost *:80>
DocumentRoot "/path/to/site"
ServerAlias site.*.xip.io
ServerName site.dev
</VirtualHost>
Note: Don't forget to restart apache.

Accessing files above the web server document root folder

Reading elsewhere, I have come across the concept of placing files I don't want accessible to the public above my web root folder. Doing this on my local WAMP stack works without issue, but once transferring over to my VPS (running Ubuntu 10.04.4 LTS) the PHP scripts can only see files under the root directory.
VirtualHost setup for my subdomain:
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin ##########.com
ServerName admin.########.com
ServerAlias www.admin.########.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php admin.php
DocumentRoot /srv/www/#######.com/public_html/admin/web_pages
<Directory /srv/www/#######.com/public_html/admin/web_pages>
Options +Indexes
AllowOverride All
</Directory>
# Log file locations
LogLevel warn
ErrorLog /srv/www/########.com/logs/error.log
CustomLog /srv/www/########.com/logs/access.log combined
</VirtualHost>
Log error given: File does not exist: /srv/www/#######.com/public_html/admin/web_pages/css, referer: http://www.admin.########.com/
(My css folder is above the web_pages folder, and is called via this path: ../css/####.css)
Is this an issue with my configuration settings, or am I attempting to do something I shouldn't be, and instead have everything I need under my web root? I would really like constants files and application logic not accessible, but css,html pages, and images are ok.
If you want to grant public access to some folders above web root, you may use Alias directive:
Alias /css/ "/srv/www/#######.com/public_html/admin/css"
<Directory /srv/www/#######.com/public_html/admin/css>
# some configuration directives here
</Directory>
After that, your css files will be accessible via new path (without ..): /css/####.css
Don't forget to switch on mod_alias Apache module

Wordpress site returning 404 error when requesting content from subfolders

I'm using WAMPSERVER to run a wordpress site that has to be acessible by the users in my network, like a environment for testing together.
Localhost works fine, but at first i couldn't access the aplication from other computers in the network. I created a rule to open the port 80, created an Alias in the Apache configuration, and it worked but the theme and any images inside subfolders wouldn't load and the console was returning lots of 404 responses.
I want to map the subfolders and its files that are under the 'base dir' of the site. I tried tons of options in the Apache conf. file but i couldn't make it.
Attached a printscreen of the responses, the apache conf file and the windows hosts file
Httpd.conf :
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot "c:/wamp/www"
ServerName localhost
ErrorLog "logs/localhost-error.log"
CustomLog "logs/localhost-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#animamidia
DocumentRoot "C:\wamp\www\website"
ServerName www.animamidia
ErrorLog "logs/animamidia-error.log"
CustomLog "logs/animamidia-access.log" common
<Directory "C:\wamp\www\website\*">
Options FollowSymLinks Includes
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
Windows hosts file:
127.0.0.1 www.animamidia
The site with it's theme broken because the requisition returned a 404
Well i found the problem.
Even though i couldn't enter the wordpress-admin i decided to check the 'wp-config.php' file just in case something is wrong or missing.
And what i did find is that the 'Home-URL' and the 'WP-URL' were set at 'localhost/website' which means that all the requisitions would point at this location as a base directory.
All i had to do was to use the 'define' comand and set the site's URL to the machine's IP and now the environment is all set up for the guys over here to develop and test their stuff.

Categories