How to use symlinks with PHP and Nginx (or Lighttpd)? - php

I've installed Nginx + FPM-PHP under Debian and ArchLinux. Both are working fine. However, I can't figure out how to get the PHP part to follow symlinks. I've also tried with Lighttpd, but to no avail.
Here is what I want to do:
Root directory configured as usual: /srv/http
Symlinks from each project to /srv/http (example: /home/user/projects/site1 to /srv/http/#site1), in such a manner that to test a local development, I just need to add the name of the site to the URL: "http://localhost/site1/xyz" or "http://localhost/site2/abc", etc;
I can make it work (after fixing permissions) with html and other static files, but whenever there is a PHP script, I get the famous "Primary script unknown" error. Inside the "real root" directory (/srv/http) there is not such an error. But when I tried to copy a subdirectory to another location (ie /home/user/projects/xyz) and symlink it to /srv/http/, I get that error.
I've followed many tutorials, including excellent posts by a Danish guy. But I can't make it work. I've tried to use all combinations of "fastcgi_param SCRIPT_FILENAME", ALIAS, etc, including hardcoding another directory into it to make sure this was (or not) the cause of the problem.
So, here are the questions:
Is it possible to use symlinks for PHP execution under nginx or lighttpd?
This setup is for development only, is there a reason not to use it?
What kind of magic would be necessary to make it work?
Since the target environment is going to be nginx (there are other projects already working), I would like to stick to nginx for development, if possible. For the sake of speed, I would also use lighttpd too. I would like to avoid Apache (only because of resources), if feasible.

Related

Netbeans does not find all my include_once paths

I've just imported a largish php project into NetBeans. Under the top directory I have "app1", "app2", "app3", etc. (each of which are mapped to a domain name), then a "shared" directory for (you guessed it) files used by all the apps.
In app1/route/Search.inc it has:
include_once "../shared/lib/search.inc";
But ctrl-B on this line does nothing. Makes sense, from NetBeans point of view of this as one large application, it should be "../../shared/lib/search.inc". But, for the way the apps are configured, the above is correct, and NetBeans is wrong.
How do I tell NetBeans it needs to go one extra directory up?
Under Include Path, I tried adding "/full/path/to/app1" (so then "../shared/lib/" would be found) (I tried Private tab, then Shared tab, with same results.) But it rejects that, telling me the app1 directory is already part of the application. There is no "Do what I say, and don't think about it, slap!" button.
Adding symbolic links in the file system, just for NetBeans, feels a bit ugly.
I'm wondering if I should make one NetBeans project per app? (I actually tried that first, but as all apps are in the same git repository it gave a lot of noise, so I assumed that was the wrong way.)
ADDITIONAL: I'm most interested in the answer to my last question - is it standard practice to keep each app as a separate NetBeans project, even if they are all in the same git repository?
Product Version: NetBeans IDE 8.0.2 (Build 201411181905)
Updates: NetBeans IDE is updated to version NetBeans 8.0.2 Patch 1
Java: 1.7.0_79; OpenJDK 64-Bit Server VM 24.79-b02
Runtime: OpenJDK Runtime Environment 1.7.0_79-b14
(on linux Mint 17)
Make shure that all files of your project are in the project's include path (righ click project -> properties -> include path). Usually there is only the "global include path", which you configure in the NetBeans settings (e.g. to point to your PEAR directory). Add all directories which contain source code you want auto completion for to this include path. Hint: This include path has nothing to do with the include_path used in PHP itself.
I'm wondering if I should make one NetBeans project per app? (I actually tried that first, but as all apps are in the same git repository it gave a lot of noise, so I assumed that was the wrong way.)
Yes you should have different NetBeans projects if your apps are separate, and since they are on different domains, it sounds like they are pretty distinct.
One way you can reduce the noise level of this approach is to right click the other app directories and find the option to Mark directory as > excluded and it should disappear (and the files won't be indexed for autocomplete / search / etc).
I have no problems with such a scenario, if I only select the filename. Then NetBeans search for a file with this name in the whole project directory.
If I select the filename and the path, it does not work. Maybe that is your problem?
I have changed nothing else and it works like a charm.
there is a alternative way to set path i.e. via php.ini
add the following line in php.ini set path of
include_path = ".:c:\xampp\htdocs\project_name\your_include_folder_name"
restart the apache server

Linux : Eclipse PDT and running as PHP Web application

I installed Eclipse PDT for PHP development on my Ubuntu box (running Apache2 as server),
I tried to create a PHP project and run it as "PHP Web Application" as show in the picture:
But when I click "Run" the browser pops out and giving 404 error saying it cant find localhost/test/newfile.php .
Does Eclipe PDT having its own internal webserver? (If so, I tried by shutting down my Apache2 ,but still giving the same error)
Finally, as a work around, I changed my workspace location to "/var/www" and created my projects there. But wondering whether its the best way to develop using PDT?
The questions you ask are not directly related to Eclipse PDT but how to setup the development server.
I suggest not to move the project into the webroot but to make Apache visit your development/project directory instead.
There are multiple ways to do that:
You can create a symlink (softlink) making a URL-Path point to a directory. You need to allow Apache to follow symlinks.
You can make use of the Alias directive to do something similar.
Virtual Hosts to make the webserver point to the location you're looking for via it's domain name. You can then create "fake" domain names within your hosts file like example.com.loc for the local development version of your website.
You can even use mod_rewrite for that (not that good option).
As you can see there are multiple ways to have this in a manageable fashion. Depending on your needs, the symlink variant is probably most easy to accomplish. If you need more security, the Alias Directive is similarly easy to accomplish. Virtual hosts start to make sense if you're more familiar with apache configuration and you need more control of the URLs (some software is that dumb that it needs to reside on the top path / beneath an URL otherwise it just does not work (yeah, crap, but virtual hosts come to the rescue then). The mod_rewrite "solution" is just named for completeness reasons, it's more asking for trouble if you're not firm with mod_rewrite and as you're new to apache configuration, leave it next to your road and ignore that suggestion.
All these ways allow you to have one or more development projects next to static websites on your server. I personally have configured virtual hosts on my devbox, and on my earlier devbox I was just using alias directives.
Keep in mind that you need to restart the apache service if you edited the configuration:
$ sudo /etc/init.d/apache2 restart
And most importantly: Before you edit the configuration file, copy it over to a backup file-name. Always backup configuration files before you edit them. You don't want to loose the working settings, believe me ;)

How to run different web apps on different port in same server?

I want to run different web app on different ports on same server, I have a apache php server running on a Linux machine. How can I achieve this ? and when I Google I stumble upon 'tomcat', what is this ? I am a noob, so please forgive if that is a stupid question.
Check the apache manual for the VirtualHost directive. It'll explain everything.
One thing to remember (it caught me out first time) - if you use virtual hosts, you have to define a virtual host for the default page too.
Don't worry about Tomcat at the moment. If you don't know what it is, you don't need it yet.
Step 1: Install the Tomcat files
Download Tomcat 4.1 or 5.5, and unzip it into an appropriate directory. I usually put it in /usr/local, so it ends up in a directory called /usr/local/apache-tomcat-5.5.17 (5.5.17 being the current version as of this writing), and make a symlink named /usr/local/tomcat to that directory. When later versions come out, I can unzip them and relink, leaving the older version in case things don’t work out (which rarely if ever happens, but I’m paranoid).
Step 2: Make directories for each instance
For each instance of Tomcat you’re going to run, you’ll need a directory that will be CATALINA_BASE. For example, you might make them /var/tomcat/serverA and /var/tomcat/serverB.
In each of these directories you need the following subdirectories: conf, logs, temp, webapps, and work.
Put a server.xml and web.xml file in the conf directory. You can get these from the conf directory of the directory where you put the tomcat installation files, although of course you should tighten up your server.xml a bit.
The webapps directory is where you’ll put the web applications you want to run on the particular instance of Tomcat.
I like to have the Tomcat manager webapp installed on each instance, so I can play with the webapps, and see how many active sessions there are. See my instructions for configuring the Tomcat manager webapp.

How to run scripts outside the main web directory effectively?

I have Web-Developer Server Suite, with PHP 5.25, which I downloaded from SourceForge.
My operating system is Windows 7 Home Edition.
I upgraded Apache to the latest version and will be upgrading PHP to 5.38 soon.
It works well, apart from one small problem; virtual hosts outside the main webroot
(C:/www/vhosts) do not display PHP scripts, or indeed any scripts at all; just the plain code. All virtualhosts work within C:/www/vhosts, but not any of the other locations I've declared in vhosts.
The other locations are:
C:/www/
C:/www/vhosts-test/
C:/www/businessweb/
C:/www/newmediasite/public_html/
This is despite the fact I declared the locations in httpd-vhosts.conf !
How would I get the server to understand PHP outside the main webroot, and also ASP.NET
(bear in mind I added mod_aspdotnet as well).
I'd gladly appreciate any advice on this; just spent a day-and-a-half rebuilding my webserver, moving files from a backup (all PHP and MySQL) after reinstalling Web-Developer Server Suite (which, IMHO, is pretty good for a beginner).
I'm not sure where to go with this, but I'll add, just for relevance, that this is strictly a development server for testing.
You can place your scripts in protected directory outside your Webroot so they are not accessible by your visitors.
However you need to place a script in your Webroot, that include these scripts. Most of the Firmwares implement a similar functionality.
<?php
require_once('/var/usr/myConfigDir/config.php');
?>
As I understand, only the directory configured as Webroot will be able to execute scripts. Already Apache adds a security layer for not executing scripts outside Webroot.
So if your target is to protect some of your scripts, you can do that, otherwise, I don't think it's possible.
From my understanding of your question, you want to be able to have your PHP files anywhere on your computer, yet have them still be able to execute. I do not believe this is possible, as PHP files are opened through the webserver i.e 127.0.0.1/foo.php instead of file://foo.php/
The webserver is configured to execute the files in /www, unless there is a setting somewhere (which I am unaware of) all PHP scripts will have to rest in /www.

Switching between Htdoc Webroots - Change my "DocumentRoot" variable in "httpd.conf" or use Symbolic Links?

I've officially reached the end of my wits on this problem. As someone who constantly works on several websites at a time (I'm sure I'm not alone here), it becomes frustrating renaming my "htdocs" directory whenever I want to work on a different site, so I decided to throw together a simple script.
My first approach was to reset the "DocumentRoot" variable in "httpd.conf" based on the directory input I provided, however, having a DocumentRoot outside of the main "/Applications/XAMPP/" directory has proven to be a bigger problem. As you can see, I'm running OSX with an XAMPP installation. Ideally, I'd like to have all my sites located at "/Users/Me/Sites/*". Even when I tried to move my sites within XAMPP's base directory, Apache does not want to load anything other than "htdocs" as my DocumentRoot.
My next approach was to simply add symbolic links to all the files/directories within each site I was working on at the time to "htdocs". This loads fine in the browser for basic html files, however, I work primarily with PHP and this does not appear to want to render (I can see the file but it will not display as a webpage).
Neither of these approaches in themselves sound too difficult, but from learning Xcode, to AppleScript, to Apache's httpd.conf documentation, I feel I've experienced enough headaches. I've played with permissions for each attempt I've gone through and nothing has panned out for me yet.
I'm sure there are plenty of other webdesigners out there who have faced this problem in the past, what's your solution?
This is actually really easily achieved, given that you are using XAMPP, I am not 100% but if you go under /bin/apache2/conf/extras/ you should see a file called httpd-vhosts.conf in this file you can add as many virtual hosts as you want. Make sure you set the ServerName to be something unique. Once that is done, make sure that the Include Vhosts is enabled in the httpd.conf
Now, you just edit your hosts file. I am not sure where this is on OSX, since it is linux based I would say /etc/hosts but google will help you. Add a line for each of your sites like so:
127.0.0.1 site1.dev
127.0.0.1 site2.dev
etc. Now restart apache and in your browser type site1.dev and apache should understand it and take you to that site. Pretty simple.
Brad's solution should work.
If you are on OSX, you might also want to check out MAMP (http://www.mamp.info/en/index.html). There is a free and "Pro" version. I've only used the free version but it's pretty drop dead simple to change the root directory from one project to the other. Only kind of gotcha is the MySQL which uses a socket to connect but if you know what you are doing, this isn't a big deal.
Once you'll understand the virtualHost things, let'say you'll use names based virtualhosts and edit you host file to get all theses names matching your development server IP, once done, you'll see you will have to adjust documentRoot and directory settings on all theses virtualhosts for each new project.
So a generic solution exists: mod_vhost_alias : http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html
All you virtualhosts will be the same, with variables (documentRoot, serverName,etc). If you want specific setting for some projects ensure you have allowed .htaccess files and set the specific things there.

Categories