... because from time to time I'm a web developer, too
About me
Projects
Contact
Links

PHP via apache on the server with nginx and django

October 27, 2009 | linux, other, python, tools
author: Karol Zielinski | comments: 5 | views: 2801
Tags: , , , , ,

I already described how to configure WordPress on nginx + lighttpd + FastCGI + php (via spawn-fcgi), as well as how to configure php-fpm on server with nginx. Now… I will try to describe alternative way to serve php scripts on the machine with python apps (django) and nginx installed – via installed apache.

Ok, we have apache already installed, so we don’t need to install anything else (like php-fpm or spawn-fcgi) to serve php scripts. We can use installed apache. I prefer to use php-fpm instead of this (because of its performance), however… sometimes it’s good to know that we can “go this way”, too.

So…

I assume that you have apache and nginx already installed.

Now, we need to install all necessary packages (such as php5 for apache):

apt-get install libapache2-mod-php5

if you need something more – install it right now.

configure apache

Next, we need to create VirtualHost in our apache.

vim /etc/apache2/sites-available/site.com

and add there:


<VirtualHost 127.0.0.1:80>
    ServerName www.site.com
    ServerAlias site.com

    DocumentRoot /opt/php-projects/site.com
    <Directory /opt/php-projects/site.com/>
        Order deny,allow
        Allow from all
    </Directory>

    LogLevel warn
    ErrorLog  /var/log/site-apache_error.log
    CustomLog /var/log/site-apache_access.log combined
</VirtualHost>

now, we need to add this VirtualHost to our apache configuration.

ln -s /etc/apache2/sites-available/site.com /etc/apache2/sites-enabled/

configure nginx

vim /etc/nginx/sites-available/apache-site.com

and add there:

server {
        listen YOUR_IP:80;
        server_name site.com;
        rewrite ^(.*) http://www.site.com permanent;
}

server {
    listen YOUR_IP:80;
    server_name www.site.com;

    access_log /var/log/site-nginx-access.log;
    error_log /var/log/site-nginx-error.log;

    location / {
        proxy_pass    http://127.0.0.1:80/;
        include       /etc/nginx/proxy.conf;
    }
}

how should /etc/nginx/proxy.conf looks like – I desribed earlier.

now add it to nginx:

ln -s /etc/nginx/sites-available/apache-site.com /etc/nginx/sites-enabled/

run everything

/etc/init.d/apache2 restart
/etc/init.d/nginx restart

Check ‘site.com’ in your web browser. You should see your php script served by apache.

The end.

Bookmark and Share
Post PHP via apache on the server with nginx and django to develway Post PHP via apache on the server with nginx and django to Delicious Post PHP via apache on the server with nginx and django to Digg Post PHP via apache on the server with nginx and django to Facebook Post PHP via apache on the server with nginx and django to Reddit Post PHP via apache on the server with nginx and django to StumbleUpon

Related news and resources

Comments (5)

4Avatars v0.3.1 v0.3.1
barszcz
October 27, 2009, 4:31 pm

I don’t get it. You are running both Apache and nginx on the same port (80)? I think it shouldn’t work.

4Avatars v0.3.1 v0.3.1
Karol Zielinski
October 28, 2009, 2:21 am

It works.

You just need to have:
NameVirtualHost 127.0.0.1:80
Listen 127.0.0.1:80
in ‘/etc/apache2/ports.conf’.

and ’127.0.0.1:80′ in your ‘VirtualHost’ like above.

after that in ‘/etc/nginx/sites-enabled/site.com’ you need to have ‘listen YOUR_IP:80′ instead of ‘listen 80′.

4Avatars v0.3.1 v0.3.1
barszcz
October 29, 2009, 5:44 am

Ah OK, I understand it now. So nginx is listening on eth0 device and Apache on loopback device.

4Avatars v0.3.1 v0.3.1
Karol Zielinski
October 29, 2009, 5:54 am

That’s right.

4Avatars v0.3.1 v0.3.1
Allow short form of PHP’s open tag
November 26, 2009, 4:19 am

[...] I moved from php on apache + nginx to php-fpm + nginx and I forgot about short form of PHP’s open tag. I did some reasearch [...]

Write a comment

Karol Zielinski :: Just a tech stuff Hello, I'm Karol Zielinski, internet evangelist, an entrepreneur, project manager and a web developer from Gdynia, Poland. I like creative design, good advertisement, social media and all kind of stuff around the web.

Most popular posts

Much more links

Karol Zielinski    |   contact me
Gdynia, Poland
RSS - Just a tech stuff - python, java blog - web development blog Karol Zielinski on twitter Karol Zielinski on LinkedIn Karol Zielinski on facebook Karol Zielinski on delicious Karol Zielinski on digg Karol Zielinski on flickr Karol Zielinski on stumbleupon Karol Zielinski on technorati