Skip to main content

Nginx + Slim Framework + phpMyAdmin on Ubuntu

Yesterday I was giving Slim Framework a test ride on my Ubuntu machine. But the machine was not ready with Nginx, or phpMyAdmin (I needed it to check the output of an ORM called RedBean). So I'm writing what I did exactly for my reference and for other's benefit.




First, in case you don't have any of the following packages, go ahead and do it

Installations:


Install MySQL
sudo apt-get install mysql-server php5-mysql

and remember your root password you will enter in the installation process.

Install Nginx
sudo apt-get install nginx


Install PHP-FPM (PHP FastCGI Process Manager)
sudo apt-get install php5-fpm


Install phpMyAdmin
sudo apt-get install phpmyadmin

During installation, you will be asked to choose your web server (apache2/lighttpd). But since we will use Nginx, just press the TAB key then Enter. Then you will be asked if you want to proceed with phpMyAdmin database configurations, choose yes and continue then enter your MySQL root password when asked to.



Configurations:

FPM

sudo nano /etc/php5/fpm/php.ini

or as I prefer
sudo gedit /etc/php5/fpm/php.ini

Find the line, cgi.fix_pathinfo=1. Uncomment it and change the 1 to 0.

Then
sudo gedit /etc/php5/fpm/pool.d/www.conf

and find the value given to listen =, to be used later in Nginx configuration.
It should be /var/run/php5-fpm.sock or 127.0.0.1:9000. 

Now restart fpm
sudo service php5-fpm restart

Nginx

Now let's configure Nginx for both phpMyAdmin and Slim Framework project.

sudo gedit /etc/nginx/sites-enabled/default


And add the configurations for both phpMyAdmin and the Slim Framework project.

server {
    listen 80;
    server_name phpmyadmin.local;
    root /usr/share/phpmyadmin;
    index index.php;
    location / {
        try_files $uri $uri/ /index.html;
    }
    # pass the PHP scripts to FastCGI server listening on the php-fpm socket
    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;       
    }
}

server {
    listen 80;
    server_name slimtest.local;
    root /home/madly/slim;
    try_files $uri /index.php;
    # pass the PHP scripts to FastCGI server listening on the php-fpm socket
    location /index.php {
        fastcgi_connect_timeout 3s;     # default of 60s is just too long
        fastcgi_read_timeout 10s;       # default of 60s is just too long
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;      
    }
}

Now let's restart Nginx server to apply the changes
sudo service nginx restart


Notes on these server configs:

1- The fastcgi_pass value is set to unix:/var/run/php5-fpm.sock because of the value I mentioned earlier in fpm configs. It may be 127.0.0.1:9000.

2- I chose to listen to port 80 but with different server names. So I must edit my hosts file
sudo gedit /etc/hosts
and add these two lines
127.0.0.1 phpmyadmin.local
127.0.0.1 slimtest.local 

3- During my tests with these configs, I had the css/img files accessible in my project with paths like
<link rel="stylesheet" type="text/css" href="/css/main.css">
by having a project structure like this


A problem I had:
The following day I had a problem with a project giving me the page "no input file specified". After inspecting the "/var/log/nginx/error.log" file, I found "(13: Permission denied)" errors. So I had to add extra permissions to the project:
chmod -R 755 ./


Comments

Popular posts from this blog

Prayer Times Calculations: Pure C++ Code

[25-March-2017] Update: For brothers and sisters in Indonesia or any country below the equator, thanks to Reza's comment ,  the code now works correctly. [18-January-2012 Update: I refined the code to make it easier and more elegant to use] [06-July-2011 Update: I found this page http://praytimes.org/calculation#Implementation ] This idea came to me when a friend of mine was asking for an 'offline database' for prayer times. I searched for algorithms to make my own database and almost all search results lead to the same algorithm. And all codes are divided into two categories: a simple but approximate one, and a complicated but precise one. I tried with the complicated one to understand it but it needs more time because of its nested functions and -unfortunately- it is not a priority right now. So I went with the easy one as a start. One thing I noticed is that every person is trying to make the ultimate use of the framework he is working on (mostly dotNet). But co...

[Qt] Custom QListView delegate with word wrap

Using QListView with a custom item layout has been a very popular demand by many developers. The problem with using  a custom delegate was having some basic features like word wrap depending on the allowed list size. Here is a screenshot from a project prototype I've been working on. The text will wrap around to fit in multiple lines depending on text length and list view width. My custom delegate is about using two important methods: QFontMetrics::boundingRect() and QPainter::drawText() . The first method will return the minimum Rect needed to draw the given text using the given origin and font. The second method draws the given text within the given Rect. Note that the flag Qt::TextWordWrap is given to both methods when called. The code is simple: Get system default font. Process the font to suite your needs. In my case: I have a normal font and another bold one for the header. Create a QFontMetrics instance for each font. Use the QFontMetrics instances to cal...

Qt Folder Compression

I needed a compression module that does not depend on anything but Qt, and I mean a module to compress a complete folder with its children. This is because I wanted the compression module to work on any platform supported by Qt with no other dependencies. So I made this modules that only uses only qCompress() & qUncompress() methods plus some logic. It is mainly a recursive algorithm that serializes data into a single file, each with its relative path to the parent folder. And deserializing is just taking binary data, creating needed subfolders, and saving the file. Download: QtFolderCompressor.zip [FolderCompressor.h] #ifndef FOLDERCOMPRESSOR_H #define FOLDERCOMPRESSOR_H #include <QFile> #include <QObject> #include <QDir> class FolderCompressor : public QObject { Q_OBJECT public: explicit FolderCompressor(QObject *parent = 0); //A recursive function that scans all files inside the source folder //and serializes all files in a row of f...