In order to install a PHP-based CMS on our Ubuntu 16.04 web server, we need to complete our LEMP webstack. LEMP stands for Linux, Nginx (pronounced “Engine X“), MySQL, PHP. This is a popular alternative to the LAMP web stack (where Apache is used instead of Nginx). Nginx is faster and lighter than Apache, but note that it’s also a bit trickier to configure.
That being said, let’s build on our LEMP web stack!
Note: This tutorial follows the one dedicated to Installing a Ubuntu 16.04 Web Server.
1. Install Nginx
Install NGINX and the PHP dependencies
apt-get install nginx php7.0-cli php7.0-cgi php7.0-fpm
2. Configure Nginx Virtual Hosting and the PHP Processor
We’re using example.com in this guide, but it needs to be replaced with your own domain name. NGINX uses server directives to specify name-based virtual hosts. NGINX calls these server blocks. All server blocks are contained within server directives in site files, located in /etc/nginx/sites-available.
Create a sample configuration based on the default file by running this command:
tail /etc/nginx/sites-available/default -n 13 | cut -c 2- | sudo tee /etc/nginx/sites-available/example.com 1> /dev/null
Open the file in Nano or Vi:
nano /etc/nginx/sites-available/example.com
Replace all instances of example.com with your domain, modify the root path as shown below, and add the location ~ \.php$ block:
server { listen 80; listen [::]:80; server_name example.com; root /var/www/example.com/public_html; index index.html index.php; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; include fastcgi_params; fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_param SCRIPT_FILENAME /var/www/example.com/public_html$fastcgi_script_name; } }
Create the root directory referenced in this configuration, replacing example.com with your domain name:
sudo mkdir -p /var/www/example.com/public_html
Enable the site, disable the default host, and restart the web server:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled sudo rm /etc/nginx/sites-enabled/default sudo systemctl restart php7.0-fpm nginx
NB: To deactivate a site, simply delete the symbolic link:
sudo rm /etc/nginx/sites-enabled/example.com sudo systemctl restart nginx
You may also want to edit the http block in /etc/nginx/nginx.conf, which applies across all sites and allows the following options, among others:
- Hide HTTP header information using server_tokens
- Configure SSL/TLS settings
- Customize log file paths
3. Test PHP with FastCGI
Create a file called test.php in your site’s public_html directory:
nano /var/www/example.com/public_html/test.php
With the following contents:
When you visit http://www.example.com/test.php in your browser, the standard “PHP info” output is shown.
4. Install Additional PHP Extensions
To complete the install in the perspective of using it with CMS and themes:
sudo apt-get install php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc php7.0-zip
Restart NGINX:
sudo systemctl restart nginx
5. Allow Permalinks (WordPress)
Modify the virtual host to allow permalinks in WordPress, by adding these lines in the location / { block of the file.
location / { index index.php index.html index.htm; try_files $uri $uri/ /index.php?$args;
Restart PHP and NGINX:
sudo systemctl restart php7.0-fpm nginx
6. Install MySQL
Install the MySQL Database Server:
sudo apt-get install mysql-server php7.0-mysql
7. Create a Database and User
Open MySQL:
mysql -u root -p
Create a database and user with permissions for it.
CREATE DATABASE web; GRANT ALL PRIVILEGES ON web.* TO 'webuser' IDENTIFIED BY 'password'; quit
Restart PHP:
sudo systemctl restart php7.0-fpm
8. Test The LEMP Stack
Create a test file phptest.php, in the public_html directory.
nano /var/www/example.com/public_html/phptest.php
Copy and paste the code available here.
If everything is working fine, it’s now possible to install a CMS like WordPress or Osclass. You have a web server running and ready to go!
Leave A Comment