I have once written Install nginx, PHP 5.3 and FastCGI on CentOS 5.5, Fedora 13, Red Hat RHEL 5.5/6 guide, but this guide is “updated” version, howto install Nginx with PHP 5.4.13 and PHP-FPM on Fedora 18/17/16/15/14, CentOS 6.4/6.3/6.2/6.1/6/5.9 and Red Hat (RHEL) 6.4/6.3/6.2/6.1/6/5.9. PHP-FPM is easier to use and configure than FastCGI and some tests says that PHP-FPM is even much more efficient than FastCGI.
Nginx (engine x) is a robust, small and high performance http server, reverse proxy server and also mail proxy server.
PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.
Install Nginx, PHP 5.4.13 and PHP-FPM on Fedora 18/17/16/15/14, CentOS 6.4/6.3/6.2/6.1/6/5.9, Red Hat (RHEL) 6.4/6.3/6.2/6.1/6/5.9
CentOS 6.4/6.3/6.2/6.1/6/5.9 and Red Hat (RHEL) 6.4/6.3/6.2/6.1/6/5.9 Remi repository
## Remi Dependency on CentOS 6 and Red Hat (RHEL) 6 ##
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
## CentOS 6 and Red Hat (RHEL) 6 ##
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
## Remi Dependency on CentOS 5 and Red Hat (RHEL) 5 ##
rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
## CentOS 5 and Red Hat (RHEL) 5 ##
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
CentOS 6.4/6.3/6.2/6.1/6/5.9 and Red Hat (RHEL) 6.4/6.3/6.2/6.1/6/5.9 Nginx repository
Create file /etc/yum.repos.d/nginx.repo and add following content to repo file: CentOS
Note: I use apache user and group here, because PHP-FPM runs as apache default (apache choosed to be able to access some dir as httpd). If you use some other user on your php-fpm conf then change this accordingly.
Create and configure virtual host directories under /etc/nginx
Nov 1922:06:09 fedora nginx[17850]: Starting nginx: nginx: [emerg]bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 1922:06:10 fedora nginx[17850]: nginx: [emerg]bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 1922:06:10 fedora nginx[17850]: nginx: [emerg]bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 1922:06:11 fedora nginx[17850]: nginx: [emerg]bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 1922:06:11 fedora nginx[17850]: nginx: [emerg]bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 1922:06:12 fedora nginx[17850]: nginx: [emerg] still could not bind()
Nov 1922:06:12 fedora nginx[17850]: [FAILED]
Nov 1922:06:12 fedora systemd[1]: nginx.service: control process exited, code=exited status=1
Nov 1922:06:12 fedora systemd[1]: Unit nginx.service entered failed state.
You maybe have httpd (apache) running and using port 80, stop it and try to start nginx again:
systemctl stop httpd.service
/etc/init.d/nginx start
## OR more fedora 16 way ##
systemctl start nginx.service
Then you can run following command to disable httpd (apache):
Yes systemctl stop httpd.service is more Fedora 16 style, but also /etc/init.d/httpd stop and service httpd stop works, because it is actually redirected to systemctl.
# service httpd stop
Stopping httpd (via systemctl): [ OK ]
Before doing these steps, I needed to run the following:
yum -y update rpm
To fix:
error: Failed dependencies:
rpmlib(FileDigests) <= 4.6.0-1 is needed by epel-release-6-5.noarch
rpmlib(PayloadIsXz) <= 5.2-1 is needed by epel-release-6-5.noarch
I too am getting this but did run yum update before starting the installations.
error: Failed dependencies:
rpmlib(FileDigests) <= 4.6.0-1 is needed by epel-release-6-5.noarch
rpmlib(PayloadIsXz) <= 5.2-1 is needed by epel-release-6-5.noarch
Thanks for this tuts, I already set my server this way and all is just working, One problem persist is that after I install WordPress on it, when I try to add new themes on wp-content/themes/ directory manually, the themes doesn’t appears on the dashboard. And when I try to add it from upload menu, it doesn’t works either. Could you help me please :) Thanks a lot before.
Hi JR..
In step 7, after run this command : cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/testsite.local
service nginx restart
i got this error : [root@glenn etc]# /etc/init.d/nginx start
Starting nginx (via systemctl): Job failed. See system logs and 'systemctl status' for details.
[FAILED]
here my log : [root@glenn etc]# grep nginx /var/log/messages
Dec 12 15:22:01 glenn nginx[1130]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 12 15:22:01 glenn nginx[1130]: [FAILED]
Dec 12 15:22:02 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 12 15:22:02 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 12 15:26:56 glenn nginx[1155]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 12 15:26:56 glenn nginx[1155]: [FAILED]
Dec 12 15:26:56 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 12 15:26:56 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 12 15:35:40 glenn nginx[1129]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 12 15:35:40 glenn nginx[1129]: [FAILED]
Dec 12 15:35:40 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 12 15:35:40 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 13 03:42:07 glenn nginx[1156]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 13 03:42:07 glenn nginx[1156]: [FAILED]
Dec 13 03:42:07 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 13 03:42:07 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 13 03:45:39 glenn nginx[1130]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 13 03:45:39 glenn nginx[1130]: [FAILED]
Dec 13 03:45:39 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 13 03:45:39 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 13 06:07:39 glenn nginx[1144]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 13 06:07:39 glenn nginx[1144]: [FAILED]
Dec 13 06:07:39 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 13 06:07:39 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 13 06:13:51 glenn nginx[1205]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 13 06:13:51 glenn nginx[1205]: [FAILED]
Dec 13 06:13:51 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 13 06:13:51 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 13 06:22:55 glenn nginx[1242]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 13 06:22:55 glenn nginx[1242]: [FAILED]
Dec 13 06:22:55 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 13 06:22:55 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 02:36:45 glenn nginx[1149]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 02:36:45 glenn nginx[1149]: [FAILED]
Dec 15 02:36:45 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 02:36:45 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:04:30 glenn nginx[1161]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:04:30 glenn nginx[1161]: [FAILED]
Dec 15 08:04:30 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:04:30 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:48:58 glenn nginx[3408]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:48:58 glenn nginx[3408]: [FAILED]
Dec 15 08:48:58 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:48:58 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:49:15 glenn nginx[3452]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:49:15 glenn nginx[3452]: [FAILED]
Dec 15 08:49:15 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:49:15 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:50:16 glenn nginx[3485]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:50:16 glenn nginx[3485]: [FAILED]
Dec 15 08:50:16 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:50:16 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:52:36 glenn nginx[3545]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:52:36 glenn nginx[3545]: [FAILED]
Dec 15 08:52:36 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:52:36 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:54:19 glenn nginx[3588]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:54:19 glenn nginx[3588]: [FAILED]
Dec 15 08:54:19 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:54:19 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:54:34 glenn nginx[3605]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:54:34 glenn nginx[3605]: [FAILED]
Dec 15 08:54:34 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:54:34 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:54:43 glenn nginx[3623]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:54:43 glenn nginx[3623]: [FAILED]
Dec 15 08:54:43 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:54:43 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:54:56 glenn nginx[3644]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:54:56 glenn nginx[3644]: [FAILED]
Dec 15 08:54:56 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:54:56 glenn systemd[1]: Unit nginx.service entered failed state.
Thanks for the help, and sorry for my long respond :)
the first two commands, show that themes that I add recently is owned by root, so the problem is only on the permission?
I new to this I’m not sure what to do, but I will try to use chmod if it is working.
Yes, your problem is directory permissions, and you can fix those with chmod command, but much better way is to use chown command and change your directories and files owner:group to same that PHP-FPM is using.
Everything went well except that I can’t see the webpages (e.g. testsite.local) across the network. I’m running my web server from a virtual machine, if this would matter. Furthermore, I checked the logs and it doesn’t contain any error or access messages.
Looks almost right, your testsite.local on your /etc/hosts should point to your virtual machine ip, so remove testsite.local from first line and add following line:
172.16.9.17 testsite.local
Or what ever your virtual server ip is…if you reboot your virtual machine and if you use dhcp, then it might be something else than 172.16.9.17.
I am having the same issue, and it appears to be with the IPTABLES configuration. I can open the page on the localhost using links ([jackman@localhost ~]$ links http://testsite.local) WITHOUT changing permissions, owners, etc. All steps in the tutorial were followed exactly.
Also, the MySQL server is suffering the same problem. It is completely inaccessible from outside the localhost. I have to establish an SSH tunnel to the server to use MySQL Workbench.
I’m using CentOS 6.2 in VirtualBox.
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all — anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp — anywhere anywhere
ACCEPT all — anywhere anywhere
ACCEPT tcp — anywhere anywhere state NEW tcp dpt:ssh
REJECT all — anywhere anywhere reject-with icmp-host-prohibited
ACCEPT tcp — anywhere anywhere state NEW tcp dpt:mysql
ACCEPT tcp — anywhere anywhere state NEW tcp dpt:http
[root@localhost ~]# cat /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A INPUT -j REJECT –reject-with icmp-host-prohibited
-A FORWARD -j REJECT –reject-with icmp-host-prohibited
COMMIT
The /etc/host file’s content is only this
127.0.0.1 testsite.local
but i run this code as root > cat /etc/hosts ## seems that
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
/etc/host file should not even exist, it should be /etc/hosts. So add your
127.0.0.1 testsite.local
to /etc/hosts file!
Could you tell more specific what is the problem with chown -R nginx:nginx /srv/www/testsite.local command?
Btw. Good to remember that chmod 777 is very dangerous on public servers…everybody can read, write and execute everything…so never use it, in real life.
[root@asusFEDORA asus]# ls -la /home/asus/
ls: /home/asus/.gvfs’e erişilemedi: Erişim engellendi
toplam 196
drwx——. 30 asus asus 4096 Ara 21 13:32 .
drwxr-xr-x. 3 root root 4096 Ara 20 22:30 ..
drwxrwxr-x. 2 asus asus 4096 Ara 20 22:31 .abrt
-rw——-. 1 asus asus 112 Ara 21 13:17 .bash_history
-rw-r–r–. 1 asus asus 18 Haz 22 17:38 .bash_logout
-rw-r–r–. 1 asus asus 193 Haz 22 17:38 .bash_profile
-rw-r–r–. 1 asus asus 124 Haz 22 17:38 .bashrc
drwxr-xr-x. 2 asus asus 4096 Ara 20 22:31 Belgeler
drwx——. 10 asus asus 4096 Ara 21 05:56 .cache
drwx——. 14 asus asus 4096 Ara 21 13:36 .config
drwx——. 3 asus asus 4096 Ara 20 22:31 .dbus
-rw——-. 1 asus asus 16 Ara 20 22:31 .esd_auth
drwxr-xr-x. 2 asus asus 4096 Ara 21 03:02 .fontconfig
drwx——. 4 asus asus 4096 Ara 21 13:38 .gconf
drwxr-xr-x. 2 asus asus 4096 Ara 20 22:31 Genel
drwx——. 4 asus asus 4096 Ara 20 22:36 .gnome2
-rw-r–r–. 1 asus asus 3072 Ara 21 06:28 .grl-bookmarks
-rw-r–r–. 1 asus asus 2048 Ara 21 06:28 .grl-metadata-store
-rw-r–r–. 1 asus asus 4096 Ara 21 06:28 .grl-podcasts
drwxrwxr-x. 2 asus asus 4096 Ara 21 03:15 .gstreamer-0.10
-rw-rw-r– 1 asus asus 146 Ara 21 07:57 .gtk-bookmarks
d?????????? ? ? ? ? ? .gvfs
-rw——-. 1 asus asus 2170 Ara 21 07:57 .ICEauthority
-rw-r–r– 1 asus asus 3120 Ara 21 07:57 .imsettings.log
drwxr-xr-x. 2 asus asus 4096 Ara 21 08:11 İndirilenler
drwxr-xr-x. 3 asus asus 4096 Ara 20 22:31 .local
drwxr-xr-x. 2 asus asus 4096 Ara 20 22:31 Masaüstü
drwx——. 3 asus asus 4096 Ara 20 22:31 .mission-control
drwxr-xr-x. 5 asus asus 4096 Ara 20 22:31 .mozilla
drwxr-xr-x. 2 asus asus 4096 Ara 20 22:31 Müzik
drwxrwxr-x 3 asus asus 4096 Ara 21 08:24 .netbeans
drwxrwxr-x 3 asus asus 4096 Ara 21 09:06 .netbeans-derby
drwxrwxr-x 4 nginx nginx 4096 Ara 21 11:31 NetBeansProjects
drwxrwxr-x. 3 asus asus 4096 Ara 21 03:02 .openoffice.org
drwx——. 2 asus asus 4096 Ara 20 22:31 .pulse
-rw——-. 1 asus asus 256 Ara 20 22:31 .pulse-cookie
drwxr-xr-x. 2 asus asus 4096 Ara 21 13:13 Resimler
drwxrwxr-x 2 asus asus 4096 Ara 21 13:17 .shotwell
drwxr-xr-x. 2 asus asus 4096 Ara 20 22:31 Şablonlar
drwx——. 5 asus asus 4096 Ara 21 09:06 .thumbnails
drwxr-xr-x. 2 asus asus 4096 Ara 20 22:31 Videolar
drwxrwxr-x 4 asus asus 4096 Ara 21 12:58 www
-rw——-. 1 asus asus 24083 Ara 21 13:44 .xsession-errors
-rw-r–r–. 1 asus asus 658 Şub 8 2011 .zshrc
**********************************************
[root@asusFEDORA asus]# ls -la /home/asus/NetBeansProjects
toplam 24
drwxrwxr-x 4 nginx nginx 4096 Ara 21 11:31 .
drwx——. 30 asus asus 4096 Ara 21 13:32 ..
-rw-rw-r– 1 nginx nginx 10 Ara 21 11:31 index.php
-rw-rw-r– 1 nginx nginx 11 Ara 21 11:23 index.php~
drwxrwxr-x 2 nginx nginx 4096 Ara 21 09:15 logs
drwxrwxr-x 3 nginx nginx 4096 Ara 21 09:51 PhpProject1
***************************************************
[root@asusFEDORA asus]# ls -la /srv/www/
toplam 16
drwxr-xr-x 4 root root 4096 Ara 21 12:35 .
drwxr-xr-x. 3 root root 4096 Ara 21 08:45 ..
drwxr-xr-x 4 nginx nginx 4096 Ara 21 12:35 sargin.domain
drwxrwxrwx 4 nginx nginx 4096 Ara 21 08:45 testsite.local
[root@asusFEDORA asus]# ls -la /
toplam 188
dr-xr-xr-x. 21 root root 4096 Ara 21 07:57 .
dr-xr-xr-x. 21 root root 4096 Ara 21 07:57 ..
-rw-r–r– 1 root root 0 Ara 21 07:57 .autorelabel
dr-xr-xr-x. 2 root root 4096 Ara 21 03:48 bin
dr-xr-xr-x. 4 root root 4096 Ara 21 00:28 boot
drwxr-xr-x 21 root root 3800 Ara 21 13:41 dev
drwxr-xr-x. 132 root root 12288 Ara 21 13:42 etc
drwxr-xr-x. 3 root root 4096 Ara 20 22:30 home
dr-xr-xr-x. 20 root root 12288 Ara 21 03:48 lib
drwx——. 2 root root 16384 Ara 21 00:04 lost+found
drwxr-xr-x 7 root root 140 Ara 21 13:32 media
drwxr-xr-x. 2 root root 4096 Tem 29 11:49 mnt
drwxr-xr-x. 4 root root 4096 Ara 21 06:11 opt
dr-xr-xr-x 185 root root 0 Ara 21 09:57 proc
-rw-r–r– 1 root root 86132 Ara 21 07:57 .readahead
dr-xr-x—. 9 root root 4096 Ara 21 14:33 root
drwxr-xr-x 35 root root 1220 Ara 21 13:42 run
dr-xr-xr-x. 2 root root 12288 Ara 21 03:48 sbin
drwxr-xr-x. 3 root root 4096 Ara 21 08:45 srv
drwxr-xr-x 13 root root 0 Ara 21 09:57 sys
drwxrwxrwt. 23 root root 4096 Ara 21 14:22 tmp
drwxr-xr-x. 13 root root 4096 Ara 20 22:59 usr
drwxr-xr-x. 18 root root 4096 Ara 21 00:26 var
********************************************************
[root@asusFEDORA asus]# ls -la /home
toplam 12
drwxr-xr-x. 3 root root 4096 Ara 20 22:30 .
dr-xr-xr-x. 21 root root 4096 Ara 21 07:57 ..
drwx——. 30 asus asus 4096 Ara 21 13:32 asus
Work like a charm, even for the first time (server name=blright.dev) it keep showing message that the root directory isn´t where it should be (/var/nginx/www). But after redo from step number 7 with new server name (kam.ty) all is well.
Great tutorial but I am still having issues with getting the site to display properly. At once point i was getting a 403, and that has since gone away and I only get a blank white page now.
I had a look at /var/www/sites/mysite/logs and access.log has nothing, error.log only has “Worker connection is not enough”, but that has since been increased. That problem would result in the page timing out and when i increased them it turned to the white screen.
I should mentioned i have virtualmin/webmin installed but am doing everything in terminal at the moment. Also, I am a linux NOOOOOOOOOOB so my conf files will be messy I am sure.
I think I may have a issue with my virtual host as I am not sure if it should be in /etc/nginx/sites-enabled or in /etc/nginx/conf.d/. Also, I may have a issue because I am trying to test 2 sites, james.XXX.com and james2.XXX.com and I think they are either conflicting or I am not setting it up proper for a subdomain. james.XXX.com is basic HTML and works fine, james2.XXX.com is PHP and thats where I get my white screen.
Yes, some problems with config files…your logs tell following:
...
[error]26499#0: *1 directory index of "/var/www/sites/james2.XXX.com/" is forbidden, client: XX.XX.40.188, server: james2.XXX.com, request: "GET / HTTP/1.1", host: "james2.XXX.com"
...
[emerg]27156#0: "server_name" directive is not allowed here in /etc/nginx/conf.d/james.XXX.com.conf:2
...
Could you post all your config files and config file locations? Btw. do you have james2.XXX.com virtualhost config on nginx.conf file and another separated file?
Could you also check/post php-fpm logs under /var/log/php-fpm/ path?
Yes *blush* I just noticed there is multiple virtualhosts, in fact 3 of them maybe. I deleted james2.xxx.com.conf from /eta/nginx/conf.d/ but still have it inside nginx.conf and sites-available (lm to sites-enabled)
Currently I can only recommend you to setup both virtualhosts again, do clean setup (remove old virtualhosts and one from nginx.conf file), because you will later have huge problems if you have situation where you run multiple sites and you are not sure how or why your setup is working or not. Best setup is use separated files for virtualhosts, location is up to you, both sites-enabled and sites-available or conf.d works, or any other included path works, but just be sure that you know how your configuration files are organized and where they are located…or later you will have big big problems, if you have to change/update/modify configuration that works by chance.
Easiest and best way to configure it is use multiple step process where you test your setup between those steps…you can example do following:
1. Create one virtualhost, with plain html and test that it works
2. Create another virtualhost and test that html works too and test also that the first virtual host works still
3. Test PHP scripts
4. …
If you do this, then you know later where the problem is, just example if html works but PHP not, then you know that problem is between Nginx and PHP-FPM or PHP config, but not your basic Nginx config. And then all “debugging” is much easier. ;)
Please let me know if you encounter some problems or do you got it working? :D
That worked perfectly! I removed the virtual host from my nginx.conf, added a new virtual host for james.XXX.com to sites-enabled and everything is working perfectly! Thanks so much for all your help. You are the first person able to explain how and where to put the virtual host and that it does not really matter where it is as long as you tell nginx where it is.
Thank you for such a wonderful guide :) The only thing I couldn’t get working was the PHP-FPM, but a google solved that issue. Other than that, flawless!
You are welcome! Nice to hear that this guide was helpful, what kind of problems you had with PHP-FPM? If I setup PHP-FPM with this guide steps, it’s working normally… :)
I got problem with nginx. Its not installing. Problem in the Repository.
yum --enablerepo=remi install nginx php php-fpm php-common
Loaded plugins: refresh-packagekit, rhnplugin
Repository google-chrome is listed more than once in the configuration
This system is not registered with RHN.
RHN support will be disabled.
google-chrome | 951 B 00:00 http://nginx.org/packages/rhel/6Server/i386/repodata/repomd.xml: [Errno 14] HTTP Error 404 : http://nginx.org/packages/rhel/6Server/i386/repodata/repomd.xml
Trying other mirror.
Error: Cannot retrieve repository metadata (repomd.xml) for repository: nginx. Please verify its path and try again
But when i delete include /etc/nginx/conf.d/*.conf; from etc/nginx/nginx.conf it works and when i type http://testsite.local/index.php , it shows me the info about php.
If you have some time could you please help me to figure out how to fix my problem?
If you type testsite.local, then your virtualhost under /etc/nginx/sites-enabled catch that url and show right data. If you type just 127.0.0.1 then your config from /etc/nginx/conf.d/default.conf catch that request and show page from /usr/share/nginx/html.
If you want to remove the default.conf, then you have to move listen, server_name, etc. to your virtualhost conf or leave default.conf and comment out example location / block. :)
Then you can get the testsite.local and 127.0.0.1 to show same content. On real server environment this is not actually problem, because 127.0.0.1 is not used anyway and all virtualhost config files have their own real domain name. :)
i followed your guide until 7th step. i didnt use testsite.local .
i put my info.php file at /usr/share/nginx/html .
my nginx.conf file configuration is: (inside http block)
Want to get your advice..
I’m facing a problem. Wish you can give me some advice.
For example, when I type in URL http://www.abc.com, it will redirect to correct working directory… But when I type in URL abc.com, it will redirect to the page.. “Welcome to nginx”!, which is the default.. Is that possible I can set when type in abc.com will redirect to http://www.abc.com?
Just try it, seem like not working…
If I not put in the code from server_name below, I added in http://www.abc.com & abc.com server {
server_name http://www.abc.com abc.com;
access_log /var/log/nginx/abc.com/access.log;
error_log /var/log/nginx/abc.com/error.log;
root /home/abc.com/public_html;
}
It should work without http:// and I think that server_name with http:// is wrong nginx syntax. Could you post your full config again with your changes?
My config now is below,
server {
server_name http://www.abc.com abc.com;
access_log /var/log/nginx/abc.com/access.log;
error_log /var/log/nginx/abc.com/error.log;
root /home/abc.com/public_html;
Oh, sorry…no I got it. You have exactly right config, but code-block on here just convert www. to http://www. So yes your config looks good and if it works, then it’s okay… :)
If you want to get always permanent 301 redirect to your www. address then it’s needed. This is important if you want do search engine friendly site… :)
Hi, thank you for this clear tutorial. I have getting this error.
[root@localhost ~]# service nginx restart
nginx: [crit] pread() “/etc/nginx/sites-enabled/sites-enabled” failed (21: Is a directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
You can check your php-fpm processes memory usage simply with top.
Do following:
1. Log in your server.
2. Start top.
3. Press Shift+F
4. Press n and Enter
Then you have your processes ordered by memory usage. Then you should see something like:
32679 web 200 540m 40m 26m S 0.08.20:16.57 php-fpm
32752 web 200 540m 38m 25m S 0.07.90:01.96 php-fpm
32759 web 200 541m 36m 22m S 0.07.40:03.60 php-fpm
So looks like your php-fpm process is ~25 Mb. I think it might be sensible to use about half of the main memory to php-fpm processes (another half for the system and nginx). So let’s do some calculations:
1024 / 25 = ~40
Good pm.max_children value is 40. So I can recommend you to test following config (change only following pm. values):
Note you should uncomment (remove ;) from pm.max_requests value.
Then you need to restart php-fpm.
You can change later pm.max_requests to higher, but I have noticed that the lower value is much better than unlimited…I can’t say that this is absolutely best config, but you can try this. You can of course backup your original config, before you change it.
You are welcome! Let me know if you still have problems with it. :)
If you check top output you see RES column which stands for the resident size, which is an accurate representation of how much actual physical memory a process is consuming. If you check all RES values then you can see php-fpm processes using 22m – 32m memory. Average 25m is just my best guess based on your top output. :)
If you have more virtual hosts you can change your config, but your current configuration is working also. So you can create PHP-FPM configs per site or just use one bigger pool for all. It’s good to remember that the pm.max_children value (40) has nothing to do with the fact that how much a requests your web server can handle. If you have hundreds simultaneous PHP requests then those requests go to the PHP-FPM queue and are processed as soon as possible. Often it’s a much more efficient handle example 15 php requests simultaneously than 100 requests. This of course depends on number of CPU cores, amount of RAM, PHP processes execution time and memory usage. :)
You can use default value formula to calculate pm.start_servers, but actually it’s just value how many child processes are created on startup. So if you set value to 15, PHP-FPM starts 15 servers on startup and if you don’t have 15 PHP-FPM requests then it will kill processes (one by one) until current process count reaches pm.min_spare_servers. So you can freely choose any (other) value between 5-25 if you want / need. :)
I personally setup first just some reasonable values and fine-tune them later, if needed. Most important values are pm.max_children and pm.max_requests, because too high (unlimited) values might cause big problems with memory usage.
Currently possible methods to get newer version of nginx to Fedora 17:
- Install nginx from Fedora rawhide repo
- Download nginx source tarball, compile and install it
Thanks to this site I’ve done a lot of jobs. Now it’s a strange problem.
I installed nginx using yum and remove it, and copy configure info. Then installed another from source pack.
After all steps described above, I wrote a php file in /etc/nginx/html and went to localhost, it works. But can’t find testsite.local
My settings are # Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8090 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
Could you post testsite.local virtual host configuration? If you followed exactly this guide, then it’s /etc/nginx/sites-available/testsite.local file content. :)
Hello JR !
First of all EXCELLENT tutorials. Thanks for all your hard work.
Second of all I’m having a little issue. After completing your tutorial when I’m trying to check the configuration using a index.php file I’m getting ” File not found. ” instead of php-info. With index.html works like a charm !
JR thanks again for you answer, as you suspected it was all my fault.
I misspelled a path in virtualhost. To be more precisely the pat is fastcgi_param SCRIPT_FILENAME /srv/www/testsite.local/public_html$fastcgi_script_name;
Anyway in order to figure this I’ve reinstalled my vps and started again.
THANKS again for your time and effort !
(I’ll probably ask you something else on a different post)
P.S. This time I installed PHP 5.4 and skiped step 4 (install php 5.4 modules) is that wrong ?
I’m having another difficulty.
I need ftp access, more specific in /var/www the location of all virtualhost websites.
for the ftp access I’ve configured a new user, set his homepage /etc/www + chrome the user to home directory. The only problem is that I can’t modify or add files since /etc/www is owned by root drwxr-xr-x 8 root root 4096 Oct 27 01:46 www
changingg the owner of www from root to www group (where my user is memmber) will cause any problems with nginx php-fpm phpmyadmin or mysql ?
If true or bad practice can you recommend me a different method. Please thate in consideration that I want full access over all websites hosted on the vps.
Thanks again.
BTW if there is any way to return at least a pat of the favors you did for me please just let me know.
Do you need old school ftp or is it possible to use sftp? With sftp you can use even root user directly and do everything over encrypted connection.
Of course you can spread the word and follow if-not-true-then-false.com on fb/twitter, but for me this site is just hobby and it’s very nice to help, if only I can. :)
I’ll be using sftp with a self-sign certificate. vsftp offers this option.
From a security point of view it’s OK-ish, from a best practice point of view it’s almost never ok to use root account with remote access.
I’m still wondering what are the effects but I’ll give it a shot and just hope for the best.
I was thinking to include root and ftpaccount in a group and change ownership to that group.As a result root will shtill have full access to those folders.
It’s totally true that root account usage with remote access is not very good practice.
Let’s think other options.
1. Your original question was about effects changing owner and group to www. Until nginx can read files it should work, you can easily test if it’s working for you. There is not direct effects to php-fpm phpmyadmin and mysql, if you want use php upload, then just change some upload directory permission example to apache user. So you can easily try your original idea, if it’s working as you wish.
2. Then another option is make your root login much more secure, limiting login by ip address, change port and maybe even user name, but still it’s not so secure option.
3. One totally different approach is use normal user (example your www user) home dir where you have desired directory structure with files and you make all changes there, then setup simple local cron rsync script which sync your changes every minute to /var/www as root user. :)
Hi I follow your steps and install nginx successfully. I am bit new to this stuff and I have a question.
My current nginx version is v0.8.something
How can I update to latest version of nginx?
I used your tutorial for installing nginx + php-fpm in centos but php no working. I can see ‘index.html’ page in my localhost; but, when I have tested with a php page – test.php- it is not working, just downloading the php page through browser.
One CentOS you don’t need to install php to use php-fpm. Yeah that’s right. The php package actually includes mod_php and requires apache. We want neither of those. So just install php-fpm and the extension specific php packages like php-mbstring and php-gd.
Just wanted to say a great THANK YOU – everything worked like a charm on a new VPS, and very clear and understandable for someone with only limited *nix knowledge :)
Hello JR, Great article. But i can’t seem to run my centos 5.8 with this setup(maybe it’s not suitable for this version but i had no errors during installation). I’ve made every step like in this article(changed name from testsite.local to salomon, added 90.156.232.212 salomon
to /etc/hosts file), but when i try to show test page by adressing ip adress http://90.156.232.212/ – error 118 (net::ERR_CONNECTION_TIMED_OUT): operation timeout is written.
First make sure nginx is running: service nginx status
If it’s running make sure in /etc/sysconfig/iptables you added the line: -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
before the line containing REJECT one and not after.
Nginx - default.conf file missing - [...] can checkout this link also for installing and configuring nginx. Link You may configure in nginx.conf file [...]
Hey, thank you for this clean tutorial.
Just one little thing, don’t know why but in order to install php-fpm I had to use
yum --enablerepo=remi install php-fpmYou are welcome! :)
So you could not use following command?
Ops, my bad.
Okey! :)
Nice to hear that you get it working! ;)
Thanks for your post. nginx fails to start after following the directions for fedora 16. this is what systemctl status says:
Loaded: loaded (/etc/rc.d/init.d/nginx)
Active: failed since Sat, 19 Nov 2011 09:02:52 -0700; 25s ago
Process: 14752 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=1/FAILURE)
CGroup: name=systemd:/system/nginx.service
I am new to this, do you have any suggestions for what to do to make it work?
Thanks
Hi ebobo,
Check your /var/log/messages log file:
Do you see something like following:
You maybe have httpd (apache) running and using port 80, stop it and try to start nginx again:
Then you can run following command to disable httpd (apache):
That was the problem, thanks you!
Hi JR, thanks for the great guide.
In Step 5, should the Stop httpd (Apache) commands be ‘stop’ rather than ‘start’?
/etc/init.d/httpd stop## OR ##
service httpd stop
Thanks Ben, yes stop it should be…it’s fixed now! :)
if you are using f16
systemctl stop httpd.service
Yes systemctl stop httpd.service is more Fedora 16 style, but also /etc/init.d/httpd stop and service httpd stop works, because it is actually redirected to systemctl.
Before doing these steps, I needed to run the following:
yum -y update rpm
To fix:
error: Failed dependencies:
rpmlib(FileDigests) <= 4.6.0-1 is needed by epel-release-6-5.noarch
rpmlib(PayloadIsXz) <= 5.2-1 is needed by epel-release-6-5.noarch
This allowed me to follow this tutorial.
Hi Josh,
Thanks for this note. I normally assume that every users run up-to-date systems… :)
I too am getting this but did run yum update before starting the installations.
error: Failed dependencies:
rpmlib(FileDigests) <= 4.6.0-1 is needed by epel-release-6-5.noarch
rpmlib(PayloadIsXz) <= 5.2-1 is needed by epel-release-6-5.noarch
running CentOS release 5.8 32 bit
Ignore that. RTFM.. I did but copied wrong line. DOH
Thanks for this tuts, I already set my server this way and all is just working, One problem persist is that after I install WordPress on it, when I try to add new themes on wp-content/themes/ directory manually, the themes doesn’t appears on the dashboard. And when I try to add it from upload menu, it doesn’t works either. Could you help me please :) Thanks a lot before.
Hi Tiyo,
Could you post following output from your wp-content/ and wp-content/themes/ directory:
Hi JR..
In step 7, after run this command :
cd /etc/nginx/sites-enabled/ln -s /etc/nginx/sites-available/testsite.local
service nginx restart
i got this error :
[root@glenn etc]# /etc/init.d/nginx start
Starting nginx (via systemctl): Job failed. See system logs and 'systemctl status' for details.
[FAILED]
here my log :
[root@glenn etc]# grep nginx /var/log/messagesDec 12 15:22:01 glenn nginx[1130]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 12 15:22:01 glenn nginx[1130]: [FAILED]
Dec 12 15:22:02 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 12 15:22:02 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 12 15:26:56 glenn nginx[1155]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 12 15:26:56 glenn nginx[1155]: [FAILED]
Dec 12 15:26:56 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 12 15:26:56 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 12 15:35:40 glenn nginx[1129]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 12 15:35:40 glenn nginx[1129]: [FAILED]
Dec 12 15:35:40 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 12 15:35:40 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 13 03:42:07 glenn nginx[1156]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 13 03:42:07 glenn nginx[1156]: [FAILED]
Dec 13 03:42:07 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 13 03:42:07 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 13 03:45:39 glenn nginx[1130]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 13 03:45:39 glenn nginx[1130]: [FAILED]
Dec 13 03:45:39 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 13 03:45:39 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 13 06:07:39 glenn nginx[1144]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 13 06:07:39 glenn nginx[1144]: [FAILED]
Dec 13 06:07:39 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 13 06:07:39 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 13 06:13:51 glenn nginx[1205]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 13 06:13:51 glenn nginx[1205]: [FAILED]
Dec 13 06:13:51 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 13 06:13:51 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 13 06:22:55 glenn nginx[1242]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 13 06:22:55 glenn nginx[1242]: [FAILED]
Dec 13 06:22:55 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 13 06:22:55 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 02:36:45 glenn nginx[1149]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 02:36:45 glenn nginx[1149]: [FAILED]
Dec 15 02:36:45 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 02:36:45 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:04:30 glenn nginx[1161]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:04:30 glenn nginx[1161]: [FAILED]
Dec 15 08:04:30 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:04:30 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:48:58 glenn nginx[3408]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:48:58 glenn nginx[3408]: [FAILED]
Dec 15 08:48:58 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:48:58 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:49:15 glenn nginx[3452]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:49:15 glenn nginx[3452]: [FAILED]
Dec 15 08:49:15 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:49:15 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:50:16 glenn nginx[3485]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:50:16 glenn nginx[3485]: [FAILED]
Dec 15 08:50:16 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:50:16 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:52:36 glenn nginx[3545]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:52:36 glenn nginx[3545]: [FAILED]
Dec 15 08:52:36 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:52:36 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:54:19 glenn nginx[3588]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:54:19 glenn nginx[3588]: [FAILED]
Dec 15 08:54:19 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:54:19 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:54:34 glenn nginx[3605]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:54:34 glenn nginx[3605]: [FAILED]
Dec 15 08:54:34 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:54:34 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:54:43 glenn nginx[3623]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:54:43 glenn nginx[3623]: [FAILED]
Dec 15 08:54:43 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:54:43 glenn systemd[1]: Unit nginx.service entered failed state.
Dec 15 08:54:56 glenn nginx[3644]: Starting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:1
Dec 15 08:54:56 glenn nginx[3644]: [FAILED]
Dec 15 08:54:56 glenn systemd[1]: nginx.service: control process exited, code=exited status=1
Dec 15 08:54:56 glenn systemd[1]: Unit nginx.service entered failed state.
So, how to solve it?
Thx b4.. :)
Hi glenn,
Could you post content of your /etc/nginx/nginx.conf file?
Here :
location ~ \.php$ {root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
include /etc/nginx/conf.d/*.conf
include /etc/nginx/sites-enabled/*;
Your /ect/nginx/nginx.conf config should example look like following:
This is Fedora’s Nginx package example nginx.conf, without comment lines and include /etc/nginx/sites-enabled/*; added.
Location block(s) should be always inside server block(s) and server block(s) should be always inside http block.
oke, thx JR..
it’s works.. :)
Excellent! =)
For more specific configuration check this guide Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks
Thanks for the help, and sorry for my long respond :)
the first two commands, show that themes that I add recently is owned by root, so the problem is only on the permission?
I new to this I’m not sure what to do, but I will try to use chmod if it is working.
Hi again Tiyo,
Yes, your problem is directory permissions, and you can fix those with chmod command, but much better way is to use chown command and change your directories and files owner:group to same that PHP-FPM is using.
More info about Nginx and PHP-FPM configuration you can find from Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks
Hi,
Everything went well except that I can’t see the webpages (e.g. testsite.local) across the network. I’m running my web server from a virtual machine, if this would matter. Furthermore, I checked the logs and it doesn’t contain any error or access messages.
Cheers
Hi Bar,
Could you post output of following command from you host (where you try to load pages):
And output of following commands from your virtual machine:
cat /etc/hosts
127.0.0.1 localhost localhost.localdomain testsite.local localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
ifconfig
eth2 Link encap:Ethernet HWaddr 46:50:06:A0:A6:1A
inet addr:172.16.9.17 Bcast:172.16.9.255 Mask:255.255.255.0
inet6 addr: fe80::4450:6ff:fea0:a61a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:141237 errors:0 dropped:0 overruns:0 frame:0
TX packets:70011 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:164894974 (157.2 MiB) TX bytes:4603652 (4.3 MiB)
Interrupt:16
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http
Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Hi Bar,
Looks almost right, your testsite.local on your /etc/hosts should point to your virtual machine ip, so remove testsite.local from first line and add following line:
172.16.9.17 testsite.local
Or what ever your virtual server ip is…if you reboot your virtual machine and if you use dhcp, then it might be something else than 172.16.9.17.
I am having the same issue, and it appears to be with the IPTABLES configuration. I can open the page on the localhost using links ([jackman@localhost ~]$ links http://testsite.local) WITHOUT changing permissions, owners, etc. All steps in the tutorial were followed exactly.
Also, the MySQL server is suffering the same problem. It is completely inaccessible from outside the localhost. I have to establish an SSH tunnel to the server to use MySQL Workbench.
I’m using CentOS 6.2 in VirtualBox.
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all — anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp — anywhere anywhere
ACCEPT all — anywhere anywhere
ACCEPT tcp — anywhere anywhere state NEW tcp dpt:ssh
REJECT all — anywhere anywhere reject-with icmp-host-prohibited
ACCEPT tcp — anywhere anywhere state NEW tcp dpt:mysql
ACCEPT tcp — anywhere anywhere state NEW tcp dpt:http
Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all — anywhere anywhere reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
This works:
[root@localhost ~]# cat /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A INPUT -j REJECT –reject-with icmp-host-prohibited
-A FORWARD -j REJECT –reject-with icmp-host-prohibited
COMMIT
i have same problem with Bar.
The /etc/host file’s content is only this
127.0.0.1 testsite.local
but i run this code as root > cat /etc/hosts ## seems that
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
i restart pc but it dont change. it is equal.
Thanks RJ…
i use Fedora 16, i run as root but this code don’t work
chown -R nginx:nginx /srv/www/testsite.local
it’s works. i apply this
chmod -R 777 /srv/www/testsite.local
Hi Sargin,
/etc/host file should not even exist, it should be /etc/hosts. So add your
127.0.0.1 testsite.local
to /etc/hosts file!
Could you tell more specific what is the problem with chown -R nginx:nginx /srv/www/testsite.local command?
Btw. Good to remember that chmod 777 is very dangerous on public servers…everybody can read, write and execute everything…so never use it, in real life.
domain name is sargin.domain i add sargin.domain to /etc/hosts
at /etc/nginx/sites-available exist sargin.domain
sargin.domain file content below:
server {
server_name sargin.domain;
access_log /home/asus/NetBeansProjects/logs/access.log;
error_log /home/asus/NetBeansProjects/logs/error.log;
root /home/asus/NetBeansProjects;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/asus/NetBeansProjects$fastcgi_script_name;
}
}
/etc/nginx/sited-enabled/ exist sargin.domain link
when i go sargin.domain
403 forbidden :(
how can i solve__?
Hi sargin,
Did you remember restart your Nginx server?
yes i tried uubss but dont change. sargin.domain 403 Forbidden
—-
i make a sample different source
/srv/www/sargin.host
i add sargin.host to /etc/hosts.
it works now.
is home folders special folder so it give 403 forbidden message????
Do you have right permissions on your home folder?
Please post output of following commands:
[root@asusFEDORA asus]# ls -la /home/asus/
ls: /home/asus/.gvfs’e erişilemedi: Erişim engellendi
toplam 196
drwx——. 30 asus asus 4096 Ara 21 13:32 .
drwxr-xr-x. 3 root root 4096 Ara 20 22:30 ..
drwxrwxr-x. 2 asus asus 4096 Ara 20 22:31 .abrt
-rw——-. 1 asus asus 112 Ara 21 13:17 .bash_history
-rw-r–r–. 1 asus asus 18 Haz 22 17:38 .bash_logout
-rw-r–r–. 1 asus asus 193 Haz 22 17:38 .bash_profile
-rw-r–r–. 1 asus asus 124 Haz 22 17:38 .bashrc
drwxr-xr-x. 2 asus asus 4096 Ara 20 22:31 Belgeler
drwx——. 10 asus asus 4096 Ara 21 05:56 .cache
drwx——. 14 asus asus 4096 Ara 21 13:36 .config
drwx——. 3 asus asus 4096 Ara 20 22:31 .dbus
-rw——-. 1 asus asus 16 Ara 20 22:31 .esd_auth
drwxr-xr-x. 2 asus asus 4096 Ara 21 03:02 .fontconfig
drwx——. 4 asus asus 4096 Ara 21 13:38 .gconf
drwxr-xr-x. 2 asus asus 4096 Ara 20 22:31 Genel
drwx——. 4 asus asus 4096 Ara 20 22:36 .gnome2
-rw-r–r–. 1 asus asus 3072 Ara 21 06:28 .grl-bookmarks
-rw-r–r–. 1 asus asus 2048 Ara 21 06:28 .grl-metadata-store
-rw-r–r–. 1 asus asus 4096 Ara 21 06:28 .grl-podcasts
drwxrwxr-x. 2 asus asus 4096 Ara 21 03:15 .gstreamer-0.10
-rw-rw-r– 1 asus asus 146 Ara 21 07:57 .gtk-bookmarks
d?????????? ? ? ? ? ? .gvfs
-rw——-. 1 asus asus 2170 Ara 21 07:57 .ICEauthority
-rw-r–r– 1 asus asus 3120 Ara 21 07:57 .imsettings.log
drwxr-xr-x. 2 asus asus 4096 Ara 21 08:11 İndirilenler
drwxr-xr-x. 3 asus asus 4096 Ara 20 22:31 .local
drwxr-xr-x. 2 asus asus 4096 Ara 20 22:31 Masaüstü
drwx——. 3 asus asus 4096 Ara 20 22:31 .mission-control
drwxr-xr-x. 5 asus asus 4096 Ara 20 22:31 .mozilla
drwxr-xr-x. 2 asus asus 4096 Ara 20 22:31 Müzik
drwxrwxr-x 3 asus asus 4096 Ara 21 08:24 .netbeans
drwxrwxr-x 3 asus asus 4096 Ara 21 09:06 .netbeans-derby
drwxrwxr-x 4 nginx nginx 4096 Ara 21 11:31 NetBeansProjects
drwxrwxr-x. 3 asus asus 4096 Ara 21 03:02 .openoffice.org
drwx——. 2 asus asus 4096 Ara 20 22:31 .pulse
-rw——-. 1 asus asus 256 Ara 20 22:31 .pulse-cookie
drwxr-xr-x. 2 asus asus 4096 Ara 21 13:13 Resimler
drwxrwxr-x 2 asus asus 4096 Ara 21 13:17 .shotwell
drwxr-xr-x. 2 asus asus 4096 Ara 20 22:31 Şablonlar
drwx——. 5 asus asus 4096 Ara 21 09:06 .thumbnails
drwxr-xr-x. 2 asus asus 4096 Ara 20 22:31 Videolar
drwxrwxr-x 4 asus asus 4096 Ara 21 12:58 www
-rw——-. 1 asus asus 24083 Ara 21 13:44 .xsession-errors
-rw-r–r–. 1 asus asus 658 Şub 8 2011 .zshrc
**********************************************
[root@asusFEDORA asus]# ls -la /home/asus/NetBeansProjects
toplam 24
drwxrwxr-x 4 nginx nginx 4096 Ara 21 11:31 .
drwx——. 30 asus asus 4096 Ara 21 13:32 ..
-rw-rw-r– 1 nginx nginx 10 Ara 21 11:31 index.php
-rw-rw-r– 1 nginx nginx 11 Ara 21 11:23 index.php~
drwxrwxr-x 2 nginx nginx 4096 Ara 21 09:15 logs
drwxrwxr-x 3 nginx nginx 4096 Ara 21 09:51 PhpProject1
***************************************************
[root@asusFEDORA asus]# ls -la /srv/www/
toplam 16
drwxr-xr-x 4 root root 4096 Ara 21 12:35 .
drwxr-xr-x. 3 root root 4096 Ara 21 08:45 ..
drwxr-xr-x 4 nginx nginx 4096 Ara 21 12:35 sargin.domain
drwxrwxrwx 4 nginx nginx 4096 Ara 21 08:45 testsite.local
Thanks and sorry, but could you also post also following outputs:
[root@asusFEDORA asus]# ls -la /
toplam 188
dr-xr-xr-x. 21 root root 4096 Ara 21 07:57 .
dr-xr-xr-x. 21 root root 4096 Ara 21 07:57 ..
-rw-r–r– 1 root root 0 Ara 21 07:57 .autorelabel
dr-xr-xr-x. 2 root root 4096 Ara 21 03:48 bin
dr-xr-xr-x. 4 root root 4096 Ara 21 00:28 boot
drwxr-xr-x 21 root root 3800 Ara 21 13:41 dev
drwxr-xr-x. 132 root root 12288 Ara 21 13:42 etc
drwxr-xr-x. 3 root root 4096 Ara 20 22:30 home
dr-xr-xr-x. 20 root root 12288 Ara 21 03:48 lib
drwx——. 2 root root 16384 Ara 21 00:04 lost+found
drwxr-xr-x 7 root root 140 Ara 21 13:32 media
drwxr-xr-x. 2 root root 4096 Tem 29 11:49 mnt
drwxr-xr-x. 4 root root 4096 Ara 21 06:11 opt
dr-xr-xr-x 185 root root 0 Ara 21 09:57 proc
-rw-r–r– 1 root root 86132 Ara 21 07:57 .readahead
dr-xr-x—. 9 root root 4096 Ara 21 14:33 root
drwxr-xr-x 35 root root 1220 Ara 21 13:42 run
dr-xr-xr-x. 2 root root 12288 Ara 21 03:48 sbin
drwxr-xr-x. 3 root root 4096 Ara 21 08:45 srv
drwxr-xr-x 13 root root 0 Ara 21 09:57 sys
drwxrwxrwt. 23 root root 4096 Ara 21 14:22 tmp
drwxr-xr-x. 13 root root 4096 Ara 20 22:59 usr
drwxr-xr-x. 18 root root 4096 Ara 21 00:26 var
********************************************************
[root@asusFEDORA asus]# ls -la /home
toplam 12
drwxr-xr-x. 3 root root 4096 Ara 20 22:30 .
dr-xr-xr-x. 21 root root 4096 Ara 21 07:57 ..
drwx——. 30 asus asus 4096 Ara 21 13:32 asus
thanks for answer RJ..
Thanks! :)
Try following (as root):
Is it working then?
Nice tutorial, i am noob in linux, is my first time in istall lemp on fedora 16…and this work without problems!
Very tks
Work like a charm, even for the first time (server name=blright.dev) it keep showing message that the root directory isn´t where it should be (/var/nginx/www). But after redo from step number 7 with new server name (kam.ty) all is well.
Thanks a lot! After about 15 tutorials, I got that thing to work. Great tuto, no mistakes, all went OK !
Thanks for your tutorial, but I have an error when I do this:
yum --enablerep=remi install php-fpmLoaded plugins: fastestmirror
Error getting repository data for remi, repository not found
Could you tell me how to fix it?
Thanks a lot!
This step I have an error:
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpmRetrieving http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
warning: /var/tmp/rpm-tmp.NEDpTq: Header V3 DSA/SHA1 Signature, key ID 00f97f56: NOKEY
error: Failed dependencies:
epel-release >= 6 is needed by remi-release-6-1.el6.remi.noarch
I fixed it:
rpm -Uvh http://download.fedora.redhat.com/pub/epel/6/i386/epel-release-6-5.noarch.rpmThanks!
Hey JR!
Great tutorial but I am still having issues with getting the site to display properly. At once point i was getting a 403, and that has since gone away and I only get a blank white page now.
Hi Hogie,
Thanks! :)
Could you check your error.log and access.log, do you find something suspicious? Or could you post your logs example to pastebin?
I had a look at /var/www/sites/mysite/logs and access.log has nothing, error.log only has “Worker connection is not enough”, but that has since been increased. That problem would result in the page timing out and when i increased them it turned to the white screen.
I should mentioned i have virtualmin/webmin installed but am doing everything in terminal at the moment. Also, I am a linux NOOOOOOOOOOB so my conf files will be messy I am sure.
I think I may have a issue with my virtual host as I am not sure if it should be in /etc/nginx/sites-enabled or in /etc/nginx/conf.d/. Also, I may have a issue because I am trying to test 2 sites, james.XXX.com and james2.XXX.com and I think they are either conflicting or I am not setting it up proper for a subdomain. james.XXX.com is basic HTML and works fine, james2.XXX.com is PHP and thats where I get my white screen.
/var/log/nginx/error.log – http://pastebin.com/HiQfiAcK
/var/log/nginx/access.log-20120131 (Lots more of this, only added the two different lines though) – http://pastebin.com/NKqdFpcs
nginx conf – http://pastebin.com/JJpbBVrw
Some strange things I noticed getting these logs for you. james2.XXX.com does not run any flash, and the logs show it is trying to find a flash file.
Yes, some problems with config files…your logs tell following:
Could you post all your config files and config file locations? Btw. do you have james2.XXX.com virtualhost config on nginx.conf file and another separated file?
Could you also check/post php-fpm logs under /var/log/php-fpm/ path?
Yes *blush* I just noticed there is multiple virtualhosts, in fact 3 of them maybe. I deleted james2.xxx.com.conf from /eta/nginx/conf.d/ but still have it inside nginx.conf and sites-available (lm to sites-enabled)
/etc/nginx/nginx.conf – http://pastebin.com/JJpbBVrw
/etc/nginx/sites-enabled/james2.XXX.com.conf – http://pastebin.com/9hRrxXMm
/var/log/php-fpm/error.log = http://pastebin.com/wxGX8Sph
Strangely enough, I do not have a single virtual host for james.XXX.com and it works fine.
Currently I can only recommend you to setup both virtualhosts again, do clean setup (remove old virtualhosts and one from nginx.conf file), because you will later have huge problems if you have situation where you run multiple sites and you are not sure how or why your setup is working or not. Best setup is use separated files for virtualhosts, location is up to you, both sites-enabled and sites-available or conf.d works, or any other included path works, but just be sure that you know how your configuration files are organized and where they are located…or later you will have big big problems, if you have to change/update/modify configuration that works by chance.
Easiest and best way to configure it is use multiple step process where you test your setup between those steps…you can example do following:
1. Create one virtualhost, with plain html and test that it works
2. Create another virtualhost and test that html works too and test also that the first virtual host works still
3. Test PHP scripts
4. …
If you do this, then you know later where the problem is, just example if html works but PHP not, then you know that problem is between Nginx and PHP-FPM or PHP config, but not your basic Nginx config. And then all “debugging” is much easier. ;)
Please let me know if you encounter some problems or do you got it working? :D
That worked perfectly! I removed the virtual host from my nginx.conf, added a new virtual host for james.XXX.com to sites-enabled and everything is working perfectly! Thanks so much for all your help. You are the first person able to explain how and where to put the virtual host and that it does not really matter where it is as long as you tell nginx where it is.
A+ JR!
Excellent to hear that you got it working! :D Yes, virtual host files location does not really matter as long as you include those. :)
Thank you for such a wonderful guide :) The only thing I couldn’t get working was the PHP-FPM, but a google solved that issue. Other than that, flawless!
Thank you again :)
Hi Josh,
You are welcome! Nice to hear that this guide was helpful, what kind of problems you had with PHP-FPM? If I setup PHP-FPM with this guide steps, it’s working normally… :)
Hi there!
I got problem with nginx. Its not installing. Problem in the Repository.
yum --enablerepo=remi install nginx php php-fpm php-common
Loaded plugins: refresh-packagekit, rhnplugin
Repository google-chrome is listed more than once in the configuration
This system is not registered with RHN.
RHN support will be disabled.
google-chrome | 951 B 00:00
http://nginx.org/packages/rhel/6Server/i386/repodata/repomd.xml: [Errno 14] HTTP Error 404 : http://nginx.org/packages/rhel/6Server/i386/repodata/repomd.xml
Trying other mirror.
Error: Cannot retrieve repository metadata (repomd.xml) for repository: nginx. Please verify its path and try again
Hi
Could you post output of following commands:
Now how does one do the following when not compiling from source?
http://h264.code-shop.com/trac/wiki/Mod-H264-Streaming-Nginx-Version2
Thanks for having this site up too. Great way to kill some time.
Hi JR! first of all thank you very much for this great tutorial!
I have a very strange problem.
When i try to open 127.0.0.1 it opens the file which is located : usr/share/nginx/html/index.html
But when i type http://testsite.local it opens the file which islocated : srv/www/testsite.local/public_html
My hosts file is looking like this : 127.0.0.1 localhost localhost.localdomain testsite.local
My second problem is when i add include /etc/nginx/sites-enabled/*; to etc/nginx/nginx.conf it does not show php files.
For example when i type http://testsite.local/index.php it gives 404
But when i delete include /etc/nginx/conf.d/*.conf; from etc/nginx/nginx.conf it works and when i type http://testsite.local/index.php , it shows me the info about php.
If you have some time could you please help me to figure out how to fix my problem?
Hi nightshift,
This is actually just normal behaviour. :)
If you type testsite.local, then your virtualhost under /etc/nginx/sites-enabled catch that url and show right data. If you type just 127.0.0.1 then your config from /etc/nginx/conf.d/default.conf catch that request and show page from /usr/share/nginx/html.
If you want to remove the default.conf, then you have to move listen, server_name, etc. to your virtualhost conf or leave default.conf and comment out example location / block. :)
Then you can get the testsite.local and 127.0.0.1 to show same content. On real server environment this is not actually problem, because 127.0.0.1 is not used anyway and all virtualhost config files have their own real domain name. :)
Ok now i see the big picture :) Thank you very much for your understanding and patience.
I’ll start over and try to use the default conf. also i’ll try to enable php under /usr/share/nginx/html
I’ll report it here.
JR i found a way to work just with default nginx root directory. I’ll try to explain it here :
After step 6 i didn’t follow other steps.
default nginx root directory is located under :
/usr/share/nginx/htmldefault nginx log files are located under :
/var/log/nginx/access.logand/var/log/nginx/error.logso first i chmod 711 both access.log and error.log
After that i changed the default.conf which is located under :
/etc/nginx/conf.d/to this :
server {listen 80;
server_name localhost;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /usr/share/nginx/html;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
}
}
so i’m able to use 127.0.0.1 and localhost with php support.
Thank you very much for your instructions JR!
Kind Regards.
You are very welcome!
Nice to hear that you got it working… :D
Hi,
I’ve a little bit problem, after following all these step I was able to call my website from localhost.
But when I’m trying to access from another PC I got this message:
curl: (7) couldn’t connect to host
What I’m doing wrong?
Thank’s.
Hi arif,
Do you have opened http port from firewall?
i followed your guide until 7th step. i didnt use testsite.local .
i put my info.php file at /usr/share/nginx/html .
my nginx.conf file configuration is: (inside http block)
“server {
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}”
i run it on a virtual machine.
my /etc/hosts file is:
“127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6″
my “inet addr:192.168.0.17″ .
and when i try localhost/test.php , firefox says:
“You have chosen to open
info.php
which is a: BIN file (20 bytes)
from: http://localhost
Would you like to save this file?”
how to solve this?
thanks a lot..
i wrote it test.php by mistake.. it should be localhost/info.php
thaks again
The current URL for the EPEL repo is http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm (or x86_64). See also http://fedoraproject.org/wiki/EPEL/FAQ#Using_EPEL
Hi JR,
Want to get your advice..
I’m facing a problem. Wish you can give me some advice.
For example, when I type in URL http://www.abc.com, it will redirect to correct working directory… But when I type in URL abc.com, it will redirect to the page.. “Welcome to nginx”!, which is the default.. Is that possible I can set when type in abc.com will redirect to http://www.abc.com?
Looking forward to receive your reply.
Thanks.
Hi woody,
Try following:
Hi, JR..
May I know this config where should I put?
Really appreciate for your help.
Thanks.
You should put this inside your server block.
Could you post your real config (server block) and I can add it for you. You can of course change your real domains… :)
Actually, currently I’m doing this way, and also working. Is it to configure in this way? or have to follow your step is recommend?
May need your advice. :) Appreciate it.
Thanks woody, then just add / change it following:
And reload/restart your nginx. Is it working then?
EDIT: I actually just updated if part again… :)
Hi JR,
Just try it, seem like not working…
If I not put in the code from server_name below, I added in http://www.abc.com & abc.com
server {server_name http://www.abc.com abc.com;
access_log /var/log/nginx/abc.com/access.log;
error_log /var/log/nginx/abc.com/error.log;
root /home/abc.com/public_html;
}
It should work without http:// and I think that server_name with http:// is wrong nginx syntax. Could you post your full config again with your changes?
My config now is below,
server {
server_name http://www.abc.com abc.com;
access_log /var/log/nginx/abc.com/access.log;
error_log /var/log/nginx/abc.com/error.log;
root /home/abc.com/public_html;
if ($host != 'www.abc.com' ) {
rewrite ^/(.*)$ http://www.abc.com/$1 permanent;
}
If without add in abc.com on,
server_name http://www.abc.com abc.com;It doesn’t work. :(
Yes you need both abc.com and http://www.abc.com, but not with the http:// so it should look like following:
Oh, sorry…no I got it. You have exactly right config, but code-block on here just convert www. to http://www. So yes your config looks good and if it works, then it’s okay… :)
Roger that JR. :)
Is this code still need?
if ($host != 'www.abc.com' ) {rewrite ^/(.*)$ http://www.abc.com/$1 permanent;
}
If you want to get always permanent 301 redirect to your www. address then it’s needed. This is important if you want do search engine friendly site… :)
Thanks JR. Really appreciate for your help. :)
You are very welcome! :)
Hi, thank you for this clear tutorial. I have getting this error.
[root@localhost ~]# service nginx restart
nginx: [crit] pread() “/etc/nginx/sites-enabled/sites-enabled” failed (21: Is a directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
my nginx.conf file;
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr – $remote_user [$time_local] “$request”$
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
how to fix this error? thanks.
Hi StmpDwn,
Could you post output of following command:
Hi JR,
I’m facing a problem that sometimes I browsing the website, it will pop out 504 Gateway Time-out (from nginx).
Appreciate if you can assist me on this matter.
Thanks.
Hi woody,
Could you tell more about your server, operating system and current nginx setup?
Hi JR,
Thanks for your reply. :)
My current OS is CentOS release 5.8 (Final) 32 bit.
It is running in 2GB RAM, 2 CPU & 50 GB HDD…
This is my nginx.conf
This is my php-fpm.conf
Hope you can assist me on this.
Thanks woody!
Could you also tell do you have any other services running on this machine, like database-server, memcache, etc.?
And could you tell how much one www (php-fpm) process uses memory on average?
Hi JR,
No database server is running in this machine.
This server is only running in nginx+php-fpm.
How can I calculate my one www (php-fpm) process uses memory on average?
Sorry about that, appreciate it if you can guide me on that. :)
Thanks.
Okay, very nice web server. :)
You can check your php-fpm processes memory usage simply with top.
Do following:
1. Log in your server.
2. Start top.
3. Press Shift+F
4. Press n and Enter
Then you have your processes ordered by memory usage. Then you should see something like:
Could you paste that output here…? :)
Hi JR,
Really cool that you share with me this command. Appreciate it.
Please find my memory usage below,
5364 apache 15 0 112m 32m 20m S 0.0 1.6 0:03.28 php-fpm
2124 root 11 -10 32752 31m 1716 S 0.0 1.6 0:00.73 iscsiuio
5367 apache 20 0 105m 24m 19m S 0.0 1.2 0:03.49 php-fpm
5420 apache 18 0 106m 24m 18m S 0.0 1.2 0:02.89 php-fpm
5365 apache 15 0 106m 24m 18m S 0.0 1.2 0:03.13 php-fpm
5366 apache 16 0 106m 24m 18m S 0.0 1.2 0:03.00 php-fpm
5369 apache 25 0 106m 23m 17m S 0.0 1.2 0:02.58 php-fpm
5417 apache 16 0 106m 22m 17m S 0.0 1.1 0:02.69 php-fpm
5370 apache 16 0 106m 22m 17m S 0.0 1.1 0:02.83 php-fpm
5412 apache 17 0 106m 22m 16m S 0.0 1.1 0:02.75 php-fpm
Thanks! :)
So looks like your php-fpm process is ~25 Mb. I think it might be sensible to use about half of the main memory to php-fpm processes (another half for the system and nginx). So let’s do some calculations:
1024 / 25 = ~40
Good pm.max_children value is 40. So I can recommend you to test following config (change only following pm. values):
Note you should uncomment (remove ;) from pm.max_requests value.
Then you need to restart php-fpm.
You can change later pm.max_requests to higher, but I have noticed that the lower value is much better than unlimited…I can’t say that this is absolutely best config, but you can try this. You can of course backup your original config, before you change it.
Please let me know, if this helps or not? :)
Hi JR,
Thanks for your help.
I just changed the config, will monitor it.
May I know how you get the 25 Mb usage for php-fpm process? can you tell me which line?
In the future, if this web server add on more virtual host, is it I have to change the php-fpm config again to match the load?
You are welcome! Let me know if you still have problems with it. :)
If you check top output you see RES column which stands for the resident size, which is an accurate representation of how much actual physical memory a process is consuming. If you check all RES values then you can see php-fpm processes using 22m – 32m memory. Average 25m is just my best guess based on your top output. :)
If you have more virtual hosts you can change your config, but your current configuration is working also. So you can create PHP-FPM configs per site or just use one bigger pool for all. It’s good to remember that the pm.max_children value (40) has nothing to do with the fact that how much a requests your web server can handle. If you have hundreds simultaneous PHP requests then those requests go to the PHP-FPM queue and are processed as soon as possible. Often it’s a much more efficient handle example 15 php requests simultaneously than 100 requests. This of course depends on number of CPU cores, amount of RAM, PHP processes execution time and memory usage. :)
Hi JR, Thanks for your explanation.
How we can calculate the amount below?
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 25
For the pm.start_servers is it need to use this formula?
Default Value: min_spare_servers + (max_spare_servers – min_spare_servers) / 2
Hi woody,
You can use default value formula to calculate pm.start_servers, but actually it’s just value how many child processes are created on startup. So if you set value to 15, PHP-FPM starts 15 servers on startup and if you don’t have 15 PHP-FPM requests then it will kill processes (one by one) until current process count reaches pm.min_spare_servers. So you can freely choose any (other) value between 5-25 if you want / need. :)
I personally setup first just some reasonable values and fine-tune them later, if needed. Most important values are pm.max_children and pm.max_requests, because too high (unlimited) values might cause big problems with memory usage.
Hi, JR, Thank you very much.. :)
You are very welcome! :)
Do you still have 504 Gateway Time-out errors from nginx?
Hi JR,
So far the errors not appear again. Have to monitor it again.
Is that possible I can have your email or any instant messenger ID?? :)
Thanks.
Hi woody,
Excellent to hear. :)
I’m not using IM’s, but you can easily contact via Contact form. :)
How can I get a more updated version of nginx to Fedora 17?
thanks
Hi Robson,
Currently possible methods to get newer version of nginx to Fedora 17:
- Install nginx from Fedora rawhide repo
- Download nginx source tarball, compile and install it
Thanks
Thanks to this site I’ve done a lot of jobs. Now it’s a strange problem.
I installed nginx using yum and remove it, and copy configure info. Then installed another from source pack.
After all steps described above, I wrote a php file in /etc/nginx/html and went to localhost, it works. But can’t find testsite.local
My settings are
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8090 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
127.0.0.1 localhost.localdomain localhost testsite.local
::1 localhost6.localdomain6 localhost6
Hi Ctu,
Could you post testsite.local virtual host configuration? If you followed exactly this guide, then it’s /etc/nginx/sites-available/testsite.local file content. :)
Could you also post output of following commands:
yep, it’s
-rw-r--r-- 1 root root 513 08-17 14:28 /etc/nginx/sites-available/testsite.local
lrwxrwxrwx 1 root root 41 08-17 14:05 /etc/nginx/sites-enabled/testsite.local -> /etc/nginx/sites-available/testsite.local
Thanks and could you post also testsite.local content?
server {
server_name testsite.local;
access_log /srv/www/testsite.local/logs/access.log;
error_log /srv/www/testsite.local/logs/error.log;
location / {
root /srv/www/testsite.local/public_html;
index index.html index.htm index.php;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/testsite.local/public_html$fastcgi_script_name;
}
}
and when i alter nginx.conf as:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /etc/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
and put an phpinfo page under html, it works. Still can’t access testsite.local.
Looks good. Do you have following include on your nginx.conf?
And are you sure that the nginx what your are using (build from source) use your nginx.conf file?
I’m sure it’s the right nginx.conf. You may forget I memtioned that when I set
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /etc/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
in this file, and then put
ls -la /etc/nginx/html/test.php
-rwxr-xr-x 1 root root 21 08-17 15:55 /etc/nginx/html/test.php
It works. PHP info is printed.
Sure, I’ve added following code.
http {
include mime.types;
include /etc/nginx/sites-enabled/*;
Woooooooooooo, I’ve fixed the problem. It’s quite simple. I’m able to access http://testsite.local/ after clearing browser cache…
WTF…orz…
Thanks for your help:)
You are welcome! Excellent to hear that the fix was so simple and you got it working! :)
Hello JR !
First of all EXCELLENT tutorials. Thanks for all your hard work.
Second of all I’m having a little issue. After completing your tutorial when I’m trying to check the configuration using a index.php file I’m getting ” File not found. ” instead of php-info. With index.html works like a charm !
thanks in advance for your time !
Hi Mosfet,
Could you post output of following command:
JR thanks again for you answer, as you suspected it was all my fault.
I misspelled a path in virtualhost. To be more precisely the pat is
fastcgi_param SCRIPT_FILENAME /srv/www/testsite.local/public_html$fastcgi_script_name;Anyway in order to figure this I’ve reinstalled my vps and started again.
THANKS again for your time and effort !
(I’ll probably ask you something else on a different post)
P.S. This time I installed PHP 5.4 and skiped step 4 (install php 5.4 modules) is that wrong ?
Ignore the PS, I’ve spotted my “stupidity”.
Hi Mosfet,
By the way you can use even following syntax:
And yes you are totally right that the php modules is totally optional and normally you need just some, like mysql, apc, etc. not all :)
Hello JR.
I’m having another difficulty.
I need ftp access, more specific in /var/www the location of all virtualhost websites.
for the ftp access I’ve configured a new user, set his homepage /etc/www + chrome the user to home directory. The only problem is that I can’t modify or add files since /etc/www is owned by root
drwxr-xr-x 8 root root 4096 Oct 27 01:46 wwwchangingg the owner of www from root to www group (where my user is memmber) will cause any problems with nginx php-fpm phpmyadmin or mysql ?
If true or bad practice can you recommend me a different method. Please thate in consideration that I want full access over all websites hosted on the vps.
Thanks again.
BTW if there is any way to return at least a pat of the favors you did for me please just let me know.
Hello Mosfet,
Do you need old school ftp or is it possible to use sftp? With sftp you can use even root user directly and do everything over encrypted connection.
Of course you can spread the word and follow if-not-true-then-false.com on fb/twitter, but for me this site is just hobby and it’s very nice to help, if only I can. :)
I’ll be using sftp with a self-sign certificate. vsftp offers this option.
From a security point of view it’s OK-ish, from a best practice point of view it’s almost never ok to use root account with remote access.
I’m still wondering what are the effects but I’ll give it a shot and just hope for the best.
I was thinking to include root and ftpaccount in a group and change ownership to that group.As a result root will shtill have full access to those folders.
I’ll follow on twitter, but I’m not a Fb fan.
It’s totally true that root account usage with remote access is not very good practice.
Let’s think other options.
1. Your original question was about effects changing owner and group to www. Until nginx can read files it should work, you can easily test if it’s working for you. There is not direct effects to php-fpm phpmyadmin and mysql, if you want use php upload, then just change some upload directory permission example to apache user. So you can easily try your original idea, if it’s working as you wish.
2. Then another option is make your root login much more secure, limiting login by ip address, change port and maybe even user name, but still it’s not so secure option.
3. One totally different approach is use normal user (example your www user) home dir where you have desired directory structure with files and you make all changes there, then setup simple local cron rsync script which sync your changes every minute to /var/www as root user. :)
Hi I follow your steps and install nginx successfully. I am bit new to this stuff and I have a question.
My current nginx version is v0.8.something
How can I update to latest version of nginx?
Hi kazunn,
Nginx 0.8x is very old, what Linux distro you are using?
I used your tutorial for installing nginx + php-fpm in centos but php no working. I can see ‘index.html’ page in my localhost; but, when I have tested with a php page – test.php- it is not working, just downloading the php page through browser.
One CentOS you don’t need to install php to use php-fpm. Yeah that’s right. The php package actually includes mod_php and requires apache. We want neither of those. So just install php-fpm and the extension specific php packages like php-mbstring and php-gd.
Hi abdussamad,
Excellent note, I will test this and update this guide accordingly. Thank you!
Just wanted to say a great THANK YOU – everything worked like a charm on a new VPS, and very clear and understandable for someone with only limited *nix knowledge :)
Hello JR, Great article. But i can’t seem to run my centos 5.8 with this setup(maybe it’s not suitable for this version but i had no errors during installation). I’ve made every step like in this article(changed name from testsite.local to salomon, added 90.156.232.212 salomon
to /etc/hosts file), but when i try to show test page by adressing ip adress http://90.156.232.212/ – error 118 (net::ERR_CONNECTION_TIMED_OUT): operation timeout is written.
First make sure nginx is running:
service nginx statusIf it’s running make sure in /etc/sysconfig/iptables you added the line:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPTbefore the line containing REJECT one and not after.