How to Install nginx, MySQL and PHP on Fedora



As promised in my first post, here is a hopefully simple guide to get your site up and running with nginx. This will involve using FastCGI for the PHP part and a configuration to get a single site running (no virtual hosts). You will need some knowledge of working on Linux through a command line. Shell commands and configuration will be bolded for easier reading.

Ok, lets start…

1. Login as root on your server.

2. Install MySQL and related stuff:

[[email protected]]$ yum install mysql mysql-server

3. Install the nginx server:

[[email protected]]$ yum install nginx

4. Install PHP and related packages:

[[email protected]]$ yum install php php-mysql

5. We are going to be using Lighttpd’s spawn-fcgi to get bridge nginx with PHP. You can download spawn-fcgi separately and we do not need to install Lighttpd in the process. You may download it from here. Follow the steps below to download and install (remember to replace the link after wget with the correct link you get from the spawn-fcgi site since it might be a different version!):

[[email protected]]$ wget http://spawnfcgifile.tar.gz

[[email protected]]$ tar –xvzf spawnfcgifile.tar.gz

[[email protected]]$ cd spawnfcgifolder

[[email protected]]$ ./configure

[[email protected]]$ make

[[email protected]]$ cp src/spawn-fcgi /usr/bin/spawn-fcgi

Note: we did not follow the instruction as shown in the spawn-fcgi readme, we skipped the last step which is usually “make install”. Instead we copied the only file we really need for this tutorial.

6. Alright we are now done installing stuff we need to continue. The next part of this tutorial is configuring everything to work together.

7. Let’s start by starting and securing MySQL:

[[email protected]]$ service mysqld start

[[email protected]]$ mysqladmin –u root password ‘newpassword’ (include the single quotes)

[[email protected]]$ mysql –u root –p (enter your just modified root password to login to MySQL)

The prompt now changes to “>mysql”, enter these commands to remove the test database and anonymous users:

mysql> DROP DATABASE test;

mysql> DELETE FROM mysql.user WHERE user=’’;

mysql> FLUSH PRIVILEGES;

mysql> exit; (now we are back to the regular shell)

8. Edit php.ini and look for “cgi.fix_pathinfo =1”. This line will probably be commented, so remove the “#” to uncomment it or insert the line if it wasn’t there. Save the file and exit the editor.

9. Now its time to configure nginx. Edit the file /etc/nginx/nginx.conf. The important parts are below:

location / {

root   /usr/share/nginx/html;

index  index.php index.html index.htm; #We added index.php to this line.

The next part we need to edit is a bit further down, this part is commented, so uncomment by removing the “#”s:

location ~ \.php$ {

root           /usr/share/nginx/html; #Edit this to the same path we set above.

fastcgi_pass   127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;#Also path above

include        fastcgi_params;

}

10. Save nginx.conf, remember to make sure files are owned by user “nginx” in the document root. You may change this user inside nginx.conf at the top where it says “user”. Otherwise you will get a forbidden message due to permissions.


11. We did not install spawn-fcgi through yum or rpm, so we do not have a convenient start/stop/restart script. Create a file in /etc/init.d called phpfcgi. This init.d script is completely based on this one:

#!/bin/sh
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ “$NETWORKING” = “no” ] && exit 0
spawnfcgi=”/usr/bin/spawn-fcgi” #Path to the spawn-fcgi binary.
php_cgi=”/usr/bin/php-cgi” #Patch to php-cgi.
prog=$(basename $php_cgi)
server_ip=127.0.0.1
server_port=9000
server_user=nginx #User to run as.
server_group=nginx #Group to run as.
server_childs=1 #Number of processes to start.
pidfile=”/var/run/php_cgi.pid”
# do not edit, put changes in /etc/sysconfig/phpfastcgi
[ -f /etc/sysconfig/phpfastcgi ] && . /etc/sysconfig/phpfastcgi
start() {

[ -x $php_cgi ] || exit 1
[ -x $spawnfcgi ] || exit 2
echo -n $”Starting $prog: “
daemon $spawnfcgi -a ${server_ip} -p ${server_port} -u ${server_user} -g ${server_group} -P ${pidfile} –C

${server_childs} -f ${php_cgi}

retval=$?
echo
return $retval

}

stop() {

echo -n $”Stopping $prog: “
killproc -p ${pidfile} $prog –QUIT
retval=$?
echo
[ -f ${pidfile} ] && /bin/rm -f ${pidfile}
return $retval

}

restart(){

stop
sleep 2
start

}

rh_status(){

status -p ${pidfile} $prog

}

case “$1” in

start)
start;;

stop)
stop;;

restart)
restart;;

status)
rh_status;;

*)

echo $”Usage: $0 {start|stop|restart|status}”
exit 3
esac

12. Exit the editor, then issue the following to make it executable: chmod +x phpfcgi

13. Now lets startup everything we have installed and configured.

[[email protected]]$ service mysqld start (note: you may have already started this in the MySQL config part)

[[email protected]]$ service nginx start

[[email protected]]$ service phpfcgi start

14. Assuming everything went ok above (you get [OK] for each as you issue start), you would like all of this to start automatically on reboot:

[[email protected]]$ chkconfig mysqld

[[email protected]]$ chkconfig nginx

[[email protected]]$ chkconfig phpfcgi

15. Place some test php files in your document root and check if it is working. The phpinfo is a good test. It is simply:

<?phpphpinfo();?>

16. Create a file in /etc/sysconfig/ called phpfastcgi. We will enter an important fcgi directive, every certain number of requests the phpfcgi processes will restart and server another batch of requests and so on. Without this I noticed that nginx stopped serving PHP files and there were no php-cgi processes running. So enter the following and save the file (our init.d script calls this file if you haven’t noticed):

export PHP_FCGI_MAX_REQUESTS=1000

17. That’s it! If something seems broken try to backtrack what you did. I think I have covered everything that got my setup running. If you run into trouble and need some help post a comment below.


One thought on “How to Install nginx, MySQL and PHP on Fedora”

Leave a Reply