These instructions install NGINX Mainline on Debian 9 from NGINX Inc's official repository. For other distributions, see the NGINX admin guide. For information on configuring NGINX for production environments, see our Getting Started with NGINX series.
/etc/apt/sources.listin a text editor and add the following line to the bottom:
- Import the repository's package signing key and add it to
sudo wget http://nginx.org/keys/nginx_signing.key sudo apt-key add nginx_signing.key
- Install NGINX:
sudo apt update sudo apt install nginx
- Ensure NGINX is running and and enabled to start automatically on reboots:
sudo systemctl start nginx sudo systemctl enable nginx
- Install the MariaDB server and MySQL/MariaDB-PHP support. You may be prompted to set a root password during installation:
sudo apt install mariadb-server php7.0-mysql
- Ensure MariaDB is running and enabled to start automatically on reboot:
sudo systemctl start mariadb sudo systemctl enable mariadb
- Run the mysql_secure_installation script.
If you were not prompted to create a MySQL root user password when installing MariaDB, press Y when prompted.Answer Y at the following prompts:
- Remove anonymous users?
- Disallow root login remotely?
- Remove test database and access to it?
- Reload privilege tables now?
- Log in to MariaDB's SQL shell. Enter the
rootuser's password when prompted:
mysql -u root -p
- Create a test database and user with access permission. Replace
testuserwith appropriate names for your setup. Replace
passwordwith a strong password:
CREATE DATABASE testdb; CREATE USER 'testuser' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'; quit
- Install the PHP FastCGI Processing Manager, which will bring in the core PHP dependencies:
sudo apt install php7.0-fpm
- Tell PHP to only accept URIs for files which actually exist on the server:
sudo sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php/7.0/fpm/php.ini
- Ownership of PHP's listening UNIX sockets is set to
www-databy default, but they need to match the user and group NGINX is running as. If you installed NGINX from the NGINX repository as in the steps above, NGINX will be using the
nginxuser and group.Change the
sudo sed -i 's/listen.owner = www-data/listen.owner = nginx/g' /etc/php/7.0/fpm/pool.d/www.conf sudo sed -i 's/listen.group = www-data/listen.group = nginx/g' /etc/php/7.0/fpm/pool.d/www.conf
- Create the site's root directory where its content will live. Replace
example.comwith your site's domain:
sudo mkdir -p /var/www/example.com/
- Rename the default example site configuration provided with the package. This disables it. Or if you have no use for it, delete it:
sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.disabled
- Website configuration files should be kept in
/etc/nginx/conf.d/. Create a configuration file for your site. Again replace
example.comwith your site's domain:
- NGINX is listening on port
80for incoming connections to
- The site is served out of
/var/www/example.com/and its index page (
index.html) is a simple
.htmlfile. If your index page will use PHP, substitute
try_filestells NGINX to verify that a requested file or directory actually exists in the site's root filesystem before further processing the request. If it does not, it returns a
location ~* \.php$means that NGINX will apply this configuration to all
.phpfiles (file names are not case sensitive) in your site's root directory, including any subdirectories containing PHP files.
~* \.php$location directive indicates that PHP file names are not case sensitive. This can be removed if you prefer to enforce letter case.
fastcgi_passspecifics the UNIX socket where PHP listens for incoming connections from other local processes.
include fastcgi_paramstells NGINX to process a list of
fastcgi_paramdirectives contain the location (relative to the site's root directory) and file naming convention of PHP scripts to be served when called by NGINX.
- Restart PHP and reload the NGINX configuration:
sudo systemctl restart php7.0-fpm sudo nginx -s reload
- Create a test page to verify NGINX can render PHP and connect to the MySQL database. Replace the
passwordfields with the MySQL credentials you created above.
- Go to
http://example.com/test.phpin a web browser. Remember to substitute
example.comwith your site's domain or Linode's IP address. The page should report that You have connected successfully. If you see an error message or if the page does not load, re-check your configuration.
- Remove the test file once you have verified that the stack is working correctly:
sudo rm /var/www/example.com/test.php