LEMP Server on Fedora 15

Updated by Linode Written by Linode

Contribute on GitHub

Report an Issue | View File | Edit File

Deprecated
This guide has been deprecated and is no longer being maintained.

This guide will help you get up and running quickly with a LEMP (Linux, nginx, MySQL, PHP) stack on your Linode. If you haven’t done so already, please follow the instructions in our getting started guide before proceeding. If you are new to Linux server administration, you may be interested in our introduction to Linux concepts guide, beginner’s guide and administration basics guide.

Set the Hostname

Before you begin installing and configuring the components described in this guide, please make sure you’ve followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly:

hostname
hostname -f

The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

Install the nginx Web Server

Issue the following commands to update your system, install nginx, set it to start on boot, and start it now.

yum update
yum install nginx
chkconfig nginx on
service nginx start

Configure nginx Virtual Hosting

Replace the contents of the file /etc/nginx/nginx.conf with the following contents.

/etc/nginx/nginx.conf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;

pid /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include /etc/nginx/mime.types;
    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;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Issue the following commands to create directories for your web content and logs, replacing “example.com” with your domain name.

mkdir -p /srv/www/www.example.com/public_html
mkdir -p /srv/www/www.example.com/logs

Issue the following command to create directories for your nginx configuration files.

mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled

Create the file /etc/nginx/sites-available/www.example.com, replacing “example.com” with your domain name. It should contain the following configuration directives; again, be sure to replace “example.com” with your domain name.

/etc/nginx/sites-available/www.example.com
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
server {
    server_name www.example.com example.com;
    access_log /srv/www/www.example.com/logs/access.log;
    error_log /srv/www/www.example.com/logs/error.log;
    root /srv/www/www.example.com/public_html;

    location / {
        index index.html index.htm;
    }
}

Issue the following commands to enable your site, replacing “example.com” with your domain name.

cd /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/www.example.com

Create a test index page for your website with the following contents.

/srv/www/www.example.com/public\\_html/index.html
1
2
3
4
5
6
7
8
9
<html>
<head>
<title>Welcome to example.com</title>
</head>
<body>
<h1>Welcome to example.com</h1>
<p>If you can see this, nginx is configured to serve your site.</p>
</body>
</html>

Issue the following command to restart nginx.

service nginx restart

Once nginx has restarted, you should be able to view your test page in a web browser.

Configure PHP-FastCGI

Install and Configure Packages

Issue the following command to install packages required for PHP-FastCGI.

yum install php spawn-fcgi

Edit the file /etc/sysconfig/spawn-fcgi to match the following contents.

/etc/sysconfig/spawn-fcgi
1
2
3
4
5
6
7
8
9
FASTCGI_USER=nginx
FASTCGI_GROUP=nginx
SOCKET=/var/run/spawn-fcgi.sock
PIDFILE=/var/run/spawn-fcgi.pid
PHP5_SOCKET=/var/run/php-fcgi.sock
CHILDREN=6
PHP5=/usr/bin/php-cgi
MODE=0600
OPTIONS="-s $PHP5_SOCKET -S -M $MODE -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5"

Issue the following command to set PHP-FastCGI to start on boot and start it now.

chkconfig spawn-fcgi on
service spawn-fcgi start

Edit your site’s nginx configuration file to resembled the following example.

/etc/nginx/sites-available/www.example.com
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
server {
    server_name www.example.com example.com;
    access_log /srv/www/www.example.com/logs/access.log;
    error_log /srv/www/www.example.com/logs/error.log;
    root /srv/www/www.example.com/public_html;

    location / {
        index  index.html index.htm;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
    }
}

Issue the following command to restart nginx.

service nginx restart

Create a PHP test page so you can verify that everything is working correctly.

/srv/www/www.example.com/public\\_html/test.php
1
2
<?php phpinfo(); ?>

Important Security Considerations

If you’re planning to run applications that support file uploads (images, for example), the above configurations may expose you to a security risk by allowing arbitrary code execution. The short explanation for this behavior is that a properly crafted URI which ends in “.php”, in combination with a malicious image file that actually contains valid PHP, can result in the image being processed as PHP.

To mitigate this issue, you may wish to modify your configuration to include a try_files directive. Please note that this fix requires nginx and the php-fcgi workers to reside on the same server.

/etc/nginx/sites-available/www.example.com
1
2
3
4
5
6
7
location ~ \.php$ {
    try_files $uri =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/run/php-fcgi.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
}

Additionally, it’s a good idea to secure any upload directories your applications may use. The following configuration excerpt demonstrates securing an “/images” directory.

/etc/nginx/sites-available/www.example.com
1
2
3
4
5
6
7
8
location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    if ($uri !~ "^/images/") {
        fastcgi_pass unix:/var/run/php-fcgi.sock;
    }
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
}

Install MySQL Database Server

Issue the following commands to install the MySQL database server, set it to start at boot, and secure the installation.

yum install mysql-server
chkconfig mysqld on
service mysqld start
mysql_secure_installation

If you need support for PHP, issue the following commands to install the required package and restart php.

yum install php-mysql
service spawn-fcgi restart

Monitor for Software Updates and Security Notices

Please follow the announcements, lists, and RSS feeds on the pages linked below to ensure you are aware of all security updates and can upgrade appropriately or apply patches and recompile as needed:

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

Join our Community

Find answers, ask questions, and help others.

This guide is published under a CC BY-ND 4.0 license.