everyone. How are you all?
I'm a beginner at PHP, and I was hoping you could help me with something.
I have two web applications that used to be independent from one another. Now, however, there's the need for them to run in the same server, share the same database and so on.
I thought the best (and more organized) way to do this would be to use a new index.php that require the index.php from each of these applications, but I'm having trouble importing their CSS and JAVASCRIPT correctly.
So, here's the thing:
I had two web applications with the following structure:
bower_components/
templates/
images/
scripts/
index.php
(It's actually larger than this, but the idea is the same).
I tried this, then:
webapp1/
webapp2/
index.php
(webapp1 and webapp2 both have the structure presented above).
My index.php is something like this:
<?php
require_once 'webapp1/index.php';
require_once 'webapp2/index.php';
?>
That... Somewhat works. But then the applications can't correctly import the files from bower_components, for instance. These files are imported in a header.html file located in templates folder. The code is something like this:
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../bower_components/owl.carousel/dist/owl.carousel.min.js"></script>
<script src="../bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
Used to work just fine when they were, in fact, independent. Now, not so much.
It would work if I changed these to:
<script src="../webapp1/bower_components/jquery/dist/jquery.min.js"></script>
BUT!! I don't want to do that. It's a lot of changes to be made, across a lot of files, there has to be a simpler way.
Anyway, so sorry for the long question, hope it was clear enough. So, is there a way to do this without changing the html files?
Thank you in advance.
Additional notes: I'm running them in the simple PHP built in server, but have Apache available (just didn't want to use it). I'm using SLIM framework in one of them.
You would need to configure your web server.
Apache example configuration:
# Ensure that Apache listens on port 80
Listen 80
<VirtualHost *:80>
DocumentRoot "/www/example1"
ServerName www.example.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/www/example2"
ServerName www.example.org
# Other directives here
</VirtualHost>
And than you can have them as 2 separate applications one in folder
/www/example2 another /www/example1 with own set of libraries each.
For Nginx see tutorial
It is actually not a good idea to share code components from one app to another directly. They can use same library and same version but I would still have 2 copies of them, disk space is cheap!
Related
I have an Apache server with PHP support. I also installed Python with mod_wsgi and with mysql-connector. Besides I installed Django. Now, I want to try to use PHP and Python simultaneously at the server side. The catch is, I worked with PHP for a couple of years and I see that it is becoming less and less popular, so I plan to port some of my PHP-code to Python-code, or just to try it, to see how they work together. So, I now have a site located at C:\Apache\htdocs and I created a first Django project at C:\WebPython\djsite. Inside djsite I have djsite folder and four files _init_.py, settings.py, urls.py and wsgi.py. In my site I want to address both to PHP handlers (or scripts) and to Python scripts, so, I guess, the problem is in how to config httpd.conf. I looked through many forum threads here at stackoverflow and outside, but still I can't make it work. Now, my httpd.conf looks like this:
...
ServerName localhost
<Directory "c:/Apache/htdocs">
Options Indexes FollowSymLinks
</Directory>
<IfModule dir_module>
DirectoryIndex index.html index.htm index.php
</IfModule>
...
You should see this question then:
PHP script inside Django template
It has a link to this:
http://animuchan.net/django_php/
Running PHP with Django would be a mess though.
Hello Like Every Body Else Said Its A terrible idea but Refer To Django Documentation adding this to http.conf on your apache2 and tweek
the wsgi.py file will work
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
and change
If multiple Django sites are run in a single mod_wsgi process, all of them will use the settings of whichever one happens to run first. This can be solved by changing:
in wsgi.py, to:
os.environ["DJANGO_SETTINGS_MODULE"] = "{{ project_name }}.settings"
or by using mod_wsgi daemon mode and ensuring that each site runs in its own daemon process.
Fixing UnicodeEncodeError for file uploads
If you get a UnicodeEncodeError when uploading files with file names that contain non-ASCII characters, make sure Apache is configured to accept non-ASCII file names:
export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'
A common location to put this configuration is /etc/apache2/envvars.
See the Files section of the Unicode reference guide for details.
See More At https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/
I've had this problem for a while, and have unsuccessfully searched far and wide for an answer.
<img src="/images/test.jpg" />
Gets an image from (root path - in my case in production in LAMP)
htdocs/images/test.jpg
Whether it's called from htdocs/index.php or htdocs/foo/bar/index.php
I use XAMPP in development, and inside htdocs have project folders, so the method described above although will work when live requires me to alter it to:
<img src="/projectName/images/test.jpg" />
when working locally.
To make this simpler i define a constant BASE, which in development I use:
define('BASE','/projectname/)
And then when it's live I change to:
define('BASE','/')
<img src="<?php echo BASE;?>images/test.jpg" />
This is obviously really annoying and ends up causing several issues. Please can someone shed some light on this situation, what I'm specifically looking to do is use root path in my image/script sources but for:
<img src="/images/test.jpg" />
when called from htdocs/projectName/foo/test.php
to look for the image in:
htdocs/projectName/images/
Is this possible?
An alternate way to handle this is with the use of Virtual Hosts.
A virtual host acts like a second version of localhost that works specifically for a subfolder. For example, you could set up Apache so that when you visit http://example (no .com or anything), it shows you the content from http://localhost/example/. All CSS and JavaScript and links would act as if they were operating from the root folder of a website, since the leading example folder has been trimmed out of the URL.
I can't find a walkthrough that I used to use for XAMPP, but here a similar one that covers all of the main points. It was written for Windows, but I imagine that there are similar mechanisms that you can use for LAMP:
To summarize, here's what the article tells you to do:
Enable Virtual Hosts within Apache
Set it up so that when you visit example, you are sent to 127.0.0.1
Configure Apache so that when someone visits 127.0.0.1 (but the name of the website is example), then it shows content from the example folder.
This is how your production site (which is a single server with multiple websites) has a different "root" for each website.
Have you ever thought about the base-tag in the header of your html content? http://www.w3schools.com/tags/tag_base.asp
<head>
...
<base href="<$path />">
...
</head>
get base path:
$path = $_SERVER['SERVER_NAME'] == 'production.host' ? '/' : 'projectName';
You can either try the solution provided by others in this thread (which are programming solutions), or as an alternative, I do something different (a setup solution).
For me, I like to create an independent environment, in which my projects and development files are separate from XAMPP as much as possible (I am using XAMPP, but the principle applies to other hosting environments). This allows me to easily install new updates for XAMPP whenever they become available without worrying about my projects, and also I like to have all my projects in one folder dedicated to development. This development folder will contain projects for web, mobile, and other environments.
The way I set it up, is I have a c:\dev\ folder, that will contain a list of my projects, each project is on its own. So, for example, c:\dev\project1\, c:\dev\project2\ and so on.
Now, after I create those folders for development, I make sure that the httpd-vhosts.conf file (located at c:\xampp\apache\conf\extra\) and the hosts file (located at c:\Windows\System32\drivers\etc\) have the correct references.
Lets assume that one of my projects is called project1. and it is typically located at c:\xampp\htdocs\project1 then I would normally access it via the browser as http://localhost/project1
However, in order to have an independent environment, and as explained earlier, I would create a development folder in the c: drive called c:\dev, then I would move project1 into it, and ended up with c:\dev\project1
Then, to access this project via typing project1.dev in the web browser, I appended the httpd-vhosts.conf file located at c:\xampp\apache\conf\extra\ as follows:
<VirtualHost project1.dev:80>
ServerAdmin admin#project1.dev
DocumentRoot "C:/dev/project1"
ServerName project1.dev
ServerAlias www.project1.dev
ErrorLog "logs/project1.dev.error.log"
CustomLog "logs/project1.dev.access.log" combined
<Directory "C:/dev/project1">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
Also, I have to update the hosts file (located at c:\Windows\System32\drivers\etc\, and add the following entry:
127.0.0.1 project1.dev
127.0.0.1 www.project1.dev
(where 127.0.0.1 is the same as your localhost. Also note that you have to add the second entry in the hosts file for the www alias).
This allows me to access my project as: http://project1.dev
Once this is set, I can now write my code consistently for both my development and production environments, and my references to the root will work just fine.
Also, with this setup, I do not care if I need to update my XAMPP or switch to LAMP or anything else, all I care about is making sure I take care of one file only, which is my setup file httpd-vhosts.conf. And as I mentioned, I always prefer having a separate folder for development, and I can have different types of projects in the development environemt, for example, mobile projects, web projects, ... etc.
Hope this helps.
Note about multisite (subdomain setup)
If you would like to setup a multisite (subdomain setup), then after you enable WordPress for multisite according to the WordPress instructions, you have to do the following:
Assuming you want the following structure for multisite:
project1.dev
www.project1.dev (this is an alias to project1.dev)
sub.project1.dev (this is another setup, subdomain)
Then you have to add the following entry to httpd-vhosts.conf
<VirtualHost sub.project1.dev:80>
ServerAdmin admin#project1.dev
DocumentRoot "C:/dev/project1"
ServerName sub.project1.dev
ErrorLog "logs/sub.project1.dev.error.log"
CustomLog "logs/sub.project1.dev.access.log" combined
<Directory "C:/dev/project1">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
And update the hosts file to include
127.0.0.1 www.project1.dev
Note that DocumentRoot points to the same directory for both the main site and the subdomain site.
Repeat the process for each new subdomain you add in your WordPress network.
I think the best way to do this is with a baseurl, set your baseurl in your config and use it for external files.
$config = new Config();
public $baseurl = "http://dev050.nl";
You can use it then as
$config->baseurl;
And maybe this is something interesting for you:
http://twig.sensiolabs.org/
hope it helped.
I do a similar thing to switch databases between my dev server and the hosting server if it's any use to you. No reason why it shouldn't work for a base URL.
// db connect
if($_SERVER['SERVER_NAME'] != "dev.mydomain.org"){
try{
$pdo = new PDO('mysql:host=mysql.***.net;dbname=***;charset=utf8', '***', '***');
}catch(PDOException $ex){
header('Location: error_db.php');
}
}else{
try{
$pdo = new PDO('mysql:host=localhost;dbname=***;charset=utf8', '***', '***');
}catch(PDOException $ex){
header('Location: error_db.php');
}
}
A possible solution could be to use mod_rewrite to automatically change the path on you development server
RewriteCond %{HTTP_REFERER} ^https?://localhost/testsite/
RewriteCond %{REQUEST_URI} \.(jpg|gif|png|bmp)$
RewriteRule ^ /testsite/$1
I believe the above mod_rewrite rule is correct but it would be best to verify this yourself.
I got this task form school, to make a PHP web application. But I don't really understand what this requirement might mean
It should be possible to run this application outside the domain root
e.g. sample URL: http://localhost/task/.
I searched a little bit on the internet but was not able to find anything that I could understand ?
I have wamp, and the folder where is my sites is wamp/www/task
When they say "outside of domain root" it means that you should not be forced to go to
http://localhost/yourfile.php
but you could put it in a subdir, like
http://localhost/task/yourfile.php
What they want you to do is harder to guess, but it could mean that you need to be able to run it in any subdir, so take care of you imports to be able to handle that (e.g.: not hardcode the dir you're working in).
Domain root seems to be at localhost, this just means it should be easy to rename your web application folder and make it still work at anywhere.
# http://localhost/task
$ cd wamp/www/
# http://localhost/task2 - should be accessible without you needing to change anything
$ mv task task2
From technical point of view, you should use relative path for all your links and images as well as external resources such as javascript / css files
you can set vitual host for your web server & access your PHP Application likw www.oorja.local
in the wamp server, just add below code at end of your httpd.conf file, which allow you access your PHP application without localhost, Document root and Directory have your physical pathe of your application directory.
ServerName oorja.local
DocumentRoot E:/LAMPSYSTEM/wamp/www/oorja/public
<Directory E:/LAMPSYSTEM/wamp/www/oorja/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Bit of a noob here. There is a probably a simple solution here - but I can’t get it to work or create a non-kludgey environment for testing. This may be answered in part elsewhere, but I'm still pulling hair out - so I'm going to ask.
The live path on a Linux VPS (with many sites in vhosts directory):
/var/www/vhosts/mysite.com/subdomains/mysubDomain/httpdocs/index.php /* the public, everyone can see web root directory */
I want to be able to configure a private level includes file folder:
/var/www/vhosts/mysite.com/subdomains/mySubdomain/includes/myincludes.php /*where I want the webserver only to read */
Note: I’d also like to use a same-level directory for moving private files after upload.
Locally, I’ve tried to create a similar structure at:
C:\xampp\htdocs\mySubdomain\httpdocs
One bad ideas is to set DocumentRoot in a local vHosts file to the mySubdomain folder – but that exposes the directory structure in things like:
<img src=”/httpdocs/images/image.png">
– and will not be acceptable in the live site.
On my XAMPP testing server, I’ve set up the vhosts file, for mysite.local to create the following
NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
DocumentRoot C:/xampp/htdocs/
ServerName localhost
</VirtualHost>
<VirtualHost mysite.local>
DocumentRoot "C:/xampp/htdocs/mySubDomain/httpdocs"
ServerName mysite.local
<Directory "C:/xampp/htdocs/mysite" ></Directory>
</VirtualHost>
But, then I can't access my includes in the the mySubdomain folder.
Questions:
Is this whole approach provide any real benefit over htaccess in ‘live’ level folders (ie httpdocs/includes)? I’ve seen multiple references that storage “below public” will be a more secure file and preferred folder structure.
Is it includepath and the Document Root settings? Some other magic and delicate balance of settings? How does one create local testing access where the httpdocs(not the htdocs local folder is identified as the public directory?
Bonus (and the noob giveaway), if it’s possible to setup DreamweaverCS5.5 in this config for leveraging some of it’s fine features(like auto-discovery of the includes file) for testing and browser checking.
Yes, it does. For instance, if somehow something or somebody screws up the webserver enough the break PHP, but not the webserver itself, people maybe able to download public/somebusinesslogic.php, but NOT private/somefilewithpasswords.php. .htaccess files can also be disabled on Apache, which depending on the actual contents of it can take a while to notice.
At no single point did you provide the contents of your errors, so I cannot tell you what goes wrong, but it should not be a problem to mimic this setup on Windows, provided you use relative includes...
I have enough hate for products created with DreamWeaver that I have seen then I wouldn't touch it with a ten foot pole. Granted, this was 5+ years ago. It may have become less awful.
I have to work with several of someone else's PHP projects that have paths hard-coded in such a way that I have to put each project in my /var/www/ directory one at a time to run it - instead of being able to it in its own subdirectory, like /var/www/project_name.
Is there some way to work around this so I don't have to put each project directly in my webroot directory? Having to do that lets me only work with one project at a time with my local LAMP server!
Edit: For doing it the "VirtualHost" way, what would my ServerName be for project_name? I've tried just project_name but that doesn't seem to work.
Add
something like this to your hosts file
localproject1 127.0.0.1
localproject2 127.0.0.1
...
In apache create a virtual server for each of them, each with their own webroot
(and what could be more important) logfiles.
browse to them via http://localproject1, http://localproject2, ...
Basically you have many options, if you want to use different URL's per project you need to edit your hosts file, enable NameVirtualHost and use VirtualHost.
The other way is by placing each project in /var/www/project_name and set the rewrite base in your .htaccess on each folder as follows, so you can use absolute URLs and the project won't break
RewriteEngine On
RewriteBase /var/www/project_name