Not signed in (Sign In)
    • CommentAuthorsfong15
    • CommentTimeSep 6th 2008 edited
     permalink
    Saw a few threads here making reference to a few articles/posts so I tried LEMP again on a new slice. I gave up LEMP because setting up LAMP was so much easier for me. My earlier failure was due to php/fastcgi now I got that OK.

    I made sure php is running by testing it at domain.com/folder/test.php with echo phpinfo() inside that file test.php.

    I even have a WP installation running at domain.com/wp/ however setting vhost gave me problem, hereunder file listing of the vhost file at /etc/nginx/sites-enabled/

    what I want is subdomain my.domain.com pointing to folder /etc/nginx/sites-enabled/wp/
    I have a CNAME record made already for subdomain my.domain.com in my DNS setup.

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

    server {
    listen 80;
    server_name my.domain.com;

    access_log /var/www/nginx-default/wp/log/access.log;
    error_log /var/www/nginx-default/wp/log/error.log;

    location / {
    root /var/www/nginx-default/wp;
    index index.php index.html;
    }
    }

    after stopping and starting nginx php stopped working, pointing browser to my.domain.com will trigger download of index file.

    what have I missed?
    • CommentAuthorSchultz
    • CommentTimeSep 6th 2008
     permalink
    you need to include your fastcgi_pass directives in each vhost that you wish to use.
    server {
    listen 80;
    server_name my.domain.org;

    access_log /var/log/nginx/my.domain.org.access_log main;
    error_log /var/log/nginx/my.domain.org.error_log info;

    root /var/www/my.domain.org/htdocs;

    if (!-e $request_filename) {
    rewrite ^/(.*)$ /index.php?q=$1 last;
    }

    location ~ .*\.php$ {
    if (!-f $request_filename) {
    return 404;
    }
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    }
    }
    I have all my fastcgi paramater's included in the top level http here they are.
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;

    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param REQUEST_URI $request_uri;
    fastcgi_param DOCUMENT_URI $document_uri;
    fastcgi_param DOCUMENT_ROOT $document_root;
    fastcgi_param SERVER_PROTOCOL $server_protocol;

    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

    fastcgi_param REMOTE_ADDR $remote_addr;
    fastcgi_param REMOTE_PORT $remote_port;
    fastcgi_param SERVER_NAME $server_name;
    • CommentAuthorsfong15
    • CommentTimeSep 6th 2008
     permalink
    thanks Schultz, tried your way, didn't work for me still not directing to /var/www/ngnix-default/wp/ folder.

    Decided to follow the official way at http://wiki.codemongers.com/NginxVirtualHostExample, now it's like this

    server {
    listen 80;
    server_name www.my.domain.com my.domain.com;
    root /var/www/nginx-default/wp;
    access_log /var/www/nginx-default/wp/log/access.log;
    location / {
    root /var/www/nginx-default/wp;
    index index.php;
    }

    location ~ .php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;

    fastcgi_param SCRIPT_FILENAME /var/www/nginx-default/wp$fastcgi_script_name;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_intercept_errors on;
    }

    location ~ /\.ht {
    deny all;
    }
    }

    Now nginx is redirecting between my.domain.com and domain.com without loading index.php?
    • CommentAuthorSchultz
    • CommentTimeSep 6th 2008
     permalink
    what does your full config look like. As in your main http section? If you want i can post my full working config files so you can see them.
    Thankful People: sfong15
    • CommentAuthorsfong15
    • CommentTimeSep 7th 2008
     permalink
    Schultz,

    my nginx.conf file under /etc/nginx/ mostly from http://wiki.codemongers.com/NginxFullExample

    user www-data;
    worker_processes 1;

    error_log /var/log/nginx/error.log;
    pid /var/run/nginx.pid;

    events {
    worker_connections 1024;
    }

    http {
    include /etc/nginx/mime.types;
    include /etc/nginx/fasctcgi.conf;
    include /etc/nginx/proxy.conf;

    default_type application/octet-stream;
    log_format main '$remote_addr - $remote_user [$time_local] $status'
    '"request" $body_bytes_sent "$http_referer"' '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log;

    sendfile on;
    tcp_nopush on;
    server_names_hash_bucket_size 128;
    #keepalive_timeout 0;
    keepalive_timeout 65;
    tcp_nodelay on;

    gzip on;
    include /etc/nginx/sites-enabled/*;

    }

    file /etc/nginx/fasctcgi.con referenced so I don't need fastcgi params in vhost file?
    • CommentAuthorsfong15
    • CommentTimeSep 7th 2008
     permalink
    nginx made me mad.

    I rebuilt my slice, start from scratch again, making sure every step is written down and without error along the way. Now I'm one step backward.

    I got LEMP all done, seeing 'welcome to nginx' at domain.com, all default files not touched. Made new folder /var/www/nginx-default/wp/ checked out a svn copy of wordpress there, created a database at mysql command line. Point browser to domain.com/wp/ I got 403 forbidden?!!

    Note all default files from nginx apt-get install not touched, any one has a clue or point me to good tutorials about setting up subdomain for LEMP.
    • CommentAuthoralphagears
    • CommentTimeSep 7th 2008 edited
     permalink
    Here's a few subdomains out of my config. The 403 error may be due to the permissions you have on your wp directory. Make sure it's owned by the user you have nginx running as.


    server {
    listen 80;
    server_name host.com;

    rewrite ^/$ http://host.com/wp/;

    access_log /web/host.com/log/access.log;

    location / {
    root /web/host.com/default;
    index index.php index.html;
    # this serves rewrites if the file doesn't exsist
    if (!-e $request_filename) {
    rewrite ^/wp/(.*)$ /wp/index.php?q=$1 last;
    }
    }

    location ~ .*\.php[345]?$ {
    include /etc/nginx/fcgi.conf;
    fastcgi_pass 127.0.0.1:10005;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /web/host.com/default$fastcgi_script_name;
    }
    }

    server {
    listen 80;
    server_name billing.host.com;
    client_max_body_size 2m;

    access_log /web/host.com/log/access.log;

    location / {
    root /web/host.com/billing;
    index index.php;
    }

    location ~ .*\.php[345]?$ {
    include /etc/nginx/fcgi.conf;
    fastcgi_pass 127.0.0.1:10005;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /web/host.com/billing$fastcgi_script_name;
    }
    }

    server {
    listen 80;
    server_name projects.host.com;
    client_max_body_size 2m;

    access_log /web/host.com/log/access.log;

    location / {
    root /web/host.com/projects;
    index index.php;

    if (!-e $request_filename) {
    rewrite ^/([0-9]+)$ /index.php?go=itemView&id=$1 last;
    rewrite ^/([a-zA-Z]+)$ /index.php?go=globalView&id=$1 last;
    }
    }

    location ~ .*\.php[345]?$ {
    include /etc/nginx/fcgi.conf;
    fastcgi_pass 127.0.0.1:10005;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /web/host.com/projects$fastcgi_script_name;
    }
    }
    Thankful People: sfong15
    • CommentAuthorSchultz
    • CommentTimeSep 7th 2008
     permalink
    yes it sounds like a permissions problem. Also outta the box i don't think nginx will pass php pages off..
    here is my complete set of config files that i have currently working with a drupal installation on a subdomain too. Also was testing wp on default host at /blog
    http://pastie.org/private/0kfezq6p7nvn0rzxrojrhg
    Thankful People: sfong15
    • CommentAuthorsfong15
    • CommentTimeSep 8th 2008
     permalink
    Thanks alphagears, wonder what 'rewrite ^/$ http://host.com/wp/' does, is this just for a cleaner url without '/wp/'?

    Thanks Schultz, I'll try that again tonite.

    Now I think you both are right it's probably a permission issue. My first attempt I did everything at root, granted 777 to subfolder /wp/, 2nd trial I logged in as new user so obviously permission wasn't granted to root.
    • CommentAuthoralphagears
    • CommentTimeSep 8th 2008 edited
     permalink
    The "rewrite ^/$ http://host.com/wp/" is so that when people go to http://host.com/ it will redirect them to http://host.com/wp/

    I've just had my wordpress install in a directory off of my web root. You wouldn't want that line since you have your wp installed in your web root.

    Also. If you just granted 777 to the wp directory and you didn't get the files in the directory it will still fail. (plus 777 is a bad idea)

    If nginx is running as www-data, then do the following to secure up the directory while giving enough permission to nginx to serve the files.

    chown -R www-data.www-data /path/to/wp
    chmod -R u+rw /path/to/wp
    chmod -R g+rw /path/to/wp
    chmod -R o-rwx /path/to/wp
    • CommentAuthorsfong15
    • CommentTimeSep 8th 2008
     permalink
    Perhaps that's part of my confusions on nginx. I know a bit how LAMP works, never had to worry about rewrite rules at server level. There are few cases I guess.

    (1) use subdomain say my.domain.com then place files in a folder at my choice, e.g. /nginx-default/my.domain/, by using vhost when visitor type url my.domain.com files in .../my.domain/ will be served
    (2) use rewrite rules, as in your case, no need for a subfolder under /default/ in
    'fastcgi_param SCRIPT_FILENAME /web/host.com/default$fastcgi_script_name' in the 'location ~' directive? I also don't see you point to a subfolder under /default/ in 'location /' directive?

    which is a proper way?
    • CommentAuthorsfong15
    • CommentTimeSep 9th 2008
     permalink
    Now I probably know what had happened. The 403 forbidden error was probably due to default rewrite rule (or not being there) of nginx. When url is domain.com/wp/ I'll get 403, it'll be OK if I point to domain.com/wp/index.php

    Whereabout in nginx.conf I could add a rewrite such that url pointing to folder will direct to default index file of either .php or .html?
    • CommentAuthoralphagears
    • CommentTimeSep 9th 2008 edited
     permalink
    I believe there are two places you need to put it. See the comments in the following vhost config...
    server {
    listen 80;
    server_name host.com;

    access_log /web/host.com/log/access.log;

    location / {
    root /web/host.com/default;
    index index.php index.html; #<------- ***HERE***
    # this serves static files that exist without running other rewrite tests
    if (!-e $request_filename) {
    rewrite ^/wp/(.*)$ /wp/index.php?q=$1 last;
    }
    }

    location ~ .*\.php[345]?$ {
    include /etc/nginx/fcgi.conf;
    fastcgi_pass 127.0.0.1:10005;
    fastcgi_index index.php; #<------- ***HERE***
    fastcgi_param SCRIPT_FILENAME /web/host.com/default$fastcgi_script_name;
    }
    }
    Thankful People: sfong15
  1.  permalink
    You were so close before! For the record - I know you've moved on to a different problem, but I had experienced the redirecting problem before too,
    "Now nginx is redirecting between my.domain.com and domain.com without loading index.php? "

    I had the exact same problem with a wordpress install. The problem is the latest versions of wordpress have a redirect built in to take care of the www/non-www (canonicalization) problem. So wordpress was trying to redirect to the www version and nginx was redirecting to the non-www version (or vice-versa) causing a loop. When you get back to this stage you'd basically be OK. You'd just need to change the nginx redirect to point the opposite way (non-www to www) and you'll be able to navigate to your wordpress install. Then you can change it in your wordpress settings and save, change the nginx redirect back, and you'll be all set.
    Thankful People: sfong15
    • CommentAuthorsfong15
    • CommentTimeSep 9th 2008
     permalink
    Thanks alphagears and nomadhacker,

    This is part of the fun isn't it. I did 99% right first time, 2nd time put that 1% right but omitted something which I did right the first time. Yes I totally forgot about the index.php the 2nd time so I thought it's a permission problem which it wasn't. I rebuilt the slice and installed LEMP again. In fact that didn't prove anything because the 403 error wasn't caused by that.

    Now, the last hurdle I hope is redirect to subfolder, my vhost file looks like
    but it's still not redirecting from host.com to host.com/wp/ ?

    server {
    listen 80;
    server_name host.com *.host.com;

    rewrite ^/$ http://host.com/wp/;

    access_log /web/host.com/log/access.log;

    location / {
    root /web/host.com/default;
    index index.php index.html;
    # this serves static files that exist without running other rewrite tests
    if (-f $request_filename) {
    break;
    }
    rewrite ^/wp/(.+)$ /wp/index.php?q=$1 last;
    }

    location ~ .*\.php[345]?$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
    }
    }

    I'll be good enough to write a LEMP for dummies howto after this.
  2.  permalink
    Hmmm... your rewrite looks correct. Are you getting an error or just the index page of http://host.com/?
    • CommentAuthoralphagears
    • CommentTimeSep 10th 2008 edited
     permalink
    Emailed By: sfong15Nginx is actually doing rewrite to url, i.e. from host.com -> host.com/wp/ is it possible to make url prettier, e.g. visitor is actually seeing url of host.com and making root of this domain at .../wp/, as in Apache?

    Sure, just delete the following line from your config.
    rewrite ^/$ http://host.com/wp/;
    and change the following
    root /var/www/nginx-default;
    change to
    root /var/www/nginx-default/wp;

    rewrite ^/wp/(.*)$ /wp/index.php?q=$1 last;
    change to
    rewrite ^/(.*)$ /index.php?q=$1 last;

    fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
    change to
    fastcgi_param SCRIPT_FILENAME /var/www/nginx-default/wp$fastcgi_script_name;
    Thankful People: sfong15
    • CommentAuthoralphagears
    • CommentTimeSep 11th 2008
     permalink
    The rewrite syntax of nginx are very similar to apache's. If you need a better understanding of how rewrite rules work, check out...

    http://wiki.codemongers.com/NginxHttpRewriteModule#rewrite
    • CommentAuthorsfong15
    • CommentTimeSep 12th 2008
     permalink
    Thanks everyone that jumped in to help.

    I have now done a little howto http://am.notsowise.net/lemp for aide memoire of installing LEMP using fastcgi from lighttpd. To spot typo I have rebuilt a slice, followed the steps, pasting codes into files and IT WORKS.

    There are other tweaks I need to learn of course, e.g. am hitting problem of importing large mysql dump via phpmyadmin ...etc.
    • CommentAuthorSchultz
    • CommentTimeSep 12th 2008
     permalink
    for importing large dumps it's better to just copy the file to your slice and manually run the mysql command something like
    mysql -u username -p password tablename < mysqldump.sql

    because using phpmyadmin you have your php upload limits, timeouts etc etc.