PHP Classes

PHP Nginx Config: Read and write Nginx server configuration files

Recommend this page to a friend!
  Info   View files Documentation   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 201 All time: 8,459 This week: 68Up
Version License PHP version Categories
nginx-config 1.0Custom (specified...5.5PHP 5, Files and Folders, Configuration, H...
Description 

Author

This package can read and write Nginx server configuration files.

It can create a new configuration file for the Nginx server and add new directive parameters or groups of parameters to it.

The class can also load existing Nginx configuration from file or a string to retrieve change its values.

The edited configuration can be saved back to the same file or to a new file.

Innovation Award
PHP Programming Innovation award nominee
March 2016
Number 3


Prize: PhpStorm IDE personal permanent license
Nginx is a popular Web server used by many PHP sites.

This class provides means to create and edit configuration files for Nginx servers.

Manuel Lemos
Picture of Aleksey Nemiro
  Performance   Level  
Name: Aleksey Nemiro <contact>
Classes: 6 packages by
Country: Russian Federation Russian Federation
Age: 40
All time rank: 201758 in Russian Federation Russian Federation
Week rank: 312 Up13 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Documentation

NginxConf.PHP Latest Stable Version Total Downloads License

Classes for working with configuration files of the Nginx web server.

Code licensed under Apache License Version 2.0.

Requirements

  • PHP5 >= 5.5, PHP7;
  • Nginx >= 1.9.

NOTE: Working with the earlier versions were not tested, but it is possible that everything is working.

How to use

Include Conf.php file and import the class `Nemiro\Nginx\Conf`.

# include the class file (use own path of the file location)
require_once 'Nginx/Conf.php';

# import class
use Nemiro\Nginx\Conf as NginxConf;

Load config from file

# create instance and load config from file
$conf = new NginxConf('/etc/nginx/sites-available/example.org.conf');
# or
# $conf = NginxConf::CreateFromFile('/etc/nginx/sites-available/example.org.conf');

# get values
var_dump($conf['server']);

if ($conf['server']->ContainsChild('listen'))
{
  print_r($conf['server']['listen']->ParametersAsString());
}

var_dump($conf['server']['server_name']->ParametersAsString());
var_dump($conf['server']['root']->ParametersAsString());
var_dump($conf['server']['location']);

Load config from string

# config data
$str = 'server {
  # server name
  server_name            example.org;
  root                   /home/example.org/html; # root path
  auth_basic             "Test server";
  auth_basic_user_file   /home/example.org/.htpasswd;

  # location #1
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For  $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080;
  }

  # location #2
  location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
  }

}';

# parse string
$conf = NginxConf::CreateFromString($str);

# get values
var_dump($conf['server']);
var_dump($conf['server']['server_name']->ParametersAsString());
var_dump($conf['server']['root']->ParametersAsString());

# get first location
$location = $conf['server']['location'][0];
var_dump($location);

# get second location
$location2 = $conf['server']['location'][1];
var_dump($location2);

Save to file

# load from file
$conf = NginxConf::CreateFromFile('/etc/nginx/sites-available/example.org.conf');

# set values
$conf['server']['server_name']->Parameters = array('example.org', 'www.example.org');
$conf['server']['root']->Parameters = array('/home/example.org/www');

# create a new directive
$new_location = NginxConf::CreateDirective('location');

# single name directives
$new_location->AddDirective('index', array('Default.aspx', 'default.aspx'));
$new_location->AddDirective('proxy_pass', array('http://127.0.0.1:8080'));

# directives with same name (group)
$proxy_set_header = NginxConf::CreateGroup('proxy_set_header');
$proxy_set_header->AddDirective(array('X-Real-IP', '$remote_addr'));
$proxy_set_header->AddDirective(array('X-Forwarded-For', '$remote_addr'));
$proxy_set_header->AddDirective(array('Host', '$host'));

# add the proxy_set_header to the new location
$proxy_set_header->AddTo($new_location);

# add the new location to the server directive
$new_location->AddTo($conf['server']);

# save
$conf->Save();

# or save as...
# $conf->Save('newFileName.conf');

Get string from current instance

# load from file
$conf = new NginxConf::CreateFromFile('/etc/nginx/sites-available/example.org.conf');

# set values
$conf['server']['server_name']->Parameters = array('example.org', 'www.example.org');
$conf['server']['root']->Parameters = array('/home/example.org/www');

# create a new directive
$new_location = NginxConf::CreateDirective('location');

# single name directives
$new_location->AddDirective('index', array('Default.aspx', 'default.aspx'));
$new_location->AddDirective('proxy_pass', array('http://127.0.0.1:8080'));

# directives with same name (group)
$proxy_set_header = NginxConf::CreateGroup('proxy_set_header');
$proxy_set_header->AddDirective(array('X-Real-IP', '$remote_addr'));
$proxy_set_header->AddDirective(array('X-Forwarded-For', '$remote_addr'));
$proxy_set_header->AddDirective(array('Host', '$host'));

# add the proxy_set_header to the new location
$proxy_set_header->AddTo($new_location);

# add the new location to the server directive
$new_location->AddTo($conf['server']);

# get as string
$string = $conf->GetAsString();

# show string
var_dump($string);

Create a new config

# create an instance
$conf = new NginxConf();

# create and add server directive
$conf->Add(NginxConf::CreateDirective('server'));

# add directives to server directive
$conf['server']->AddDirective('server_name', array('example.org', 'www.example.org'));
$conf['server']->AddDirective('root', array('/home/example.org/www'));

# create a new location directive
$location = NginxConf::CreateDirective('location');

# add sub-directives
$location->AddDirective('index', array('index.php', 'index.html', 'index.htm'));

# add the location to the server directive
$location->AddTo($conf['server']);

# get as string
$string = $conf->GetAsString();

# show string
var_dump($string);

# or save
# $conf->Save('example.org.conf');

  Files folder image Files  
File Role Description
Files folder imageUnitTests (2 files)
Plain text file Conf.php Class Main class.
Plain text file Directive.php Class Class source
Plain text file DirectiveCollection.php Class Class source
Plain text file DirectiveGroup.php Class Class source
Plain text file DirectiveType.php Class Class source
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  UnitTests  
File Role Description
  Accessible without login Plain text file ConfTest.php Test Unit test script
  Accessible without login Plain text file test.conf Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:201
This week:0
All time:8,459
This week:68Up