Difference between revisions of "User:Saul/nginx"

From Organic Design wiki
(Nginx)
(Wordpress Install)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= LEMP Stack =
+
== LEMP Stack ==
== Install ==
+
=== Install ===
=== Nginx ===
+
==== Nginx ====
 
<source lang="bash">
 
<source lang="bash">
 
sudo apt-get install nginx
 
sudo apt-get install nginx
 
</source>
 
</source>
  
=== PHP ===
+
==== PHP ====
 
<source lang="bash">
 
<source lang="bash">
 
sudo apt-get install php7.0 php7.0-fpm
 
sudo apt-get install php7.0 php7.0-fpm
 
</source>
 
</source>
  
=== MySQL ===
+
==== MySQL ====
 
<source lang="bash">
 
<source lang="bash">
 
sudo apt-get install mysql-server php7.0-mysql
 
sudo apt-get install mysql-server php7.0-mysql
 
</source>
 
</source>
  
== Configure ==
+
=== Configure ===
=== Nginx ===
+
==== Nginx ====
 
To use php change:
 
To use php change:
 
<source lang="bash">
 
<source lang="bash">
Line 37: Line 37:
 
</source>
 
</source>
  
=== MySQL ===
+
==== MySQL ====
 
Configure MySQL
 
Configure MySQL
 
<source lang="bash">
 
<source lang="bash">
Line 43: Line 43:
 
</source>
 
</source>
  
= Wordpress =
+
== Wordpress ==
== MySQL ==
+
=== MySQL ===
 
Create the database for wordpress:
 
Create the database for wordpress:
 
<source lang="bash">
 
<source lang="bash">
Line 54: Line 54:
 
quit; # exit mysql
 
quit; # exit mysql
 
</source>
 
</source>
== Nginx ==
+
=== Nginx ===
== Wordpress Install ==
+
==== Single Site ====
 +
<source>
 +
sudo nano /etc/nginx/sites-enabled/default
 +
 
 +
upstream php {
 +
        server unix:/run/php/php7.0-fpm.sock;
 +
        server 127.0.0.1:9000;
 +
}
 +
 
 +
server {
 +
        ## Your website name goes here.
 +
        server_name example.com;
 +
        ## Your only path reference.
 +
        root /var/www/public_html;
 +
        ## This should be in your http block and if it is, it's not needed here.
 +
        index index.php;
 +
 
 +
        location = /favicon.ico {
 +
                log_not_found off;
 +
                access_log off;
 +
        }
 +
 
 +
        location = /robots.txt {
 +
                allow all;
 +
                log_not_found off;
 +
                access_log off;
 +
        }
 +
 
 +
        location / {
 +
                # This is cool because no php is touched for static content.
 +
                # include the "?$args" part so non-default permalinks doesn't break when using query string
 +
                try_files $uri $uri/ /index.php$is_args$args;
 +
        }
 +
 
 +
        location ~ \.php$ {
 +
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
 +
                include fastcgi.conf;
 +
                fastcgi_intercept_errors on;
 +
                fastcgi_pass php;
 +
        }
 +
 
 +
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
 +
                expires max;
 +
                log_not_found off;
 +
        }
 +
}
 +
</source>
 +
==== Multi Site ====
 +
<source>
 +
upstream php {
 +
        server unix:/run/php/php7.0-fpm.sock;
 +
        server 127.0.0.1:9000;
 +
}
 +
 
 +
map $uri $blogname{
 +
    ~^(?P<blogpath>/[^/]+/)files/(.*)      $blogpath ;
 +
}
 +
 
 +
map $blogname $blogid{
 +
    default -999;
 +
 
 +
    #Ref: http://wordpress.org/extend/plugins/nginx-helper/
 +
    #include /var/www/wordpress/wp-content/plugins/nginx-helper/map.conf ;
 +
}
 +
 
 +
server {
 +
        ## Your website name goes here.
 +
        server_name syncedapps.com;
 +
        ## Your only path reference.
 +
        root /var/www/public_html;
 +
        ## This should be in your http block and if it is, it's not needed here.
 +
        index index.php;
 +
 
 +
        if (!-e $request_filename) {
 +
                rewrite /wp-admin$ $scheme://$host$uri/ permanent;
 +
                rewrite ^(/[^/]+)?(/wp-.*) $2 last;
 +
                rewrite ^(/[^/]+)?(/.*\.php) $2 last;
 +
        }
 +
 
 +
        location = /favicon.ico {
 +
                log_not_found off;
 +
                access_log off;
 +
        }
 +
 
 +
        location = /robots.txt {
 +
                allow all;
 +
                log_not_found off;
 +
                access_log off;
 +
        }
 +
 
 +
        location / {
 +
                # This is cool because no php is touched for static content.
 +
                # include the "?$args" part so non-default permalinks doesn't break when using query string
 +
                try_files $uri $uri/ /index.php$is_args$args;
 +
        }
 +
 
 +
        location ~ \.php$ {
 +
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
 +
                include fastcgi.conf;
 +
                fastcgi_intercept_errors on;
 +
                fastcgi_pass php;
 +
        }
 +
 
 +
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
 +
                expires max;
 +
                log_not_found off;
 +
        }
 +
}
 +
</source>
 +
 
 +
=== Wordpress Install ===
 
<source lang="bash">
 
<source lang="bash">
 
cd /var/www
 
cd /var/www
Line 68: Line 178:
 
sudo rm -R wordpress # delete the old wordpress folder
 
sudo rm -R wordpress # delete the old wordpress folder
 
</source>
 
</source>
 +
 +
=== Domain Mapping ===
 +
Us the '''WordPress MU Domain Mapping''' plugin.
 +
 +
== See also ==
 +
*[[Install a new server]]
 +
*[[Nginx]]
 +
*[[SSL]]
 +
*[[Wordpress]]

Latest revision as of 22:21, 17 September 2018

LEMP Stack

Install

Nginx

sudo apt-get install nginx

PHP

sudo apt-get install php7.0 php7.0-fpm

MySQL

sudo apt-get install mysql-server php7.0-mysql

Configure

Nginx

To use php change:

sudo nano /etc/nginx/sites-enabled/default
	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html index.php;

	# pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }
sudo nginx -s reload

MySQL

Configure MySQL

sudo mysql_secure_installation # set a unique password, remove anonymous user accounts, disable remote root login, and remove the test database

Wordpress

MySQL

Create the database for wordpress:

mysql -u root -p # OR:
sudo mysql --user=root --password="ROOTPASSWORD" # Enter the MYSQL database
	create database example; # create the database example.com (can be anything) for wordpress
	create user 'USER' identified by 'PASSWORD'; # create a user by the name of USER with the password PASSWORD
	grant all on example.com.* to 'USER' identified by 'PASSWORD'; # grant a user by the name of USER the permissions to modify the database with the password PASSWORD
	quit; # exit mysql

Nginx

Single Site

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

upstream php {
        server unix:/run/php/php7.0-fpm.sock;
        server 127.0.0.1:9000;
}

server {
        ## Your website name goes here.
        server_name example.com;
        ## Your only path reference.
        root /var/www/public_html;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }

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

Multi Site

upstream php {
        server unix:/run/php/php7.0-fpm.sock;
        server 127.0.0.1:9000;
}

map $uri $blogname{
    ~^(?P<blogpath>/[^/]+/)files/(.*)       $blogpath ;
}

map $blogname $blogid{
    default -999;

    #Ref: http://wordpress.org/extend/plugins/nginx-helper/
    #include /var/www/wordpress/wp-content/plugins/nginx-helper/map.conf ;
}

server {
        ## Your website name goes here.
        server_name syncedapps.com;
        ## Your only path reference.
        root /var/www/public_html;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;

        if (!-e $request_filename) {
                rewrite /wp-admin$ $scheme://$host$uri/ permanent;
                rewrite ^(/[^/]+)?(/wp-.*) $2 last;
                rewrite ^(/[^/]+)?(/.*\.php) $2 last;
        }

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }

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

Wordpress Install

cd /var/www
sudo rm -R html
sudo mkdir public_html logs backups
cd public_html
sudo chown -R www-data:www-data /var/www/ # ensure that the files are owned by the webserver
sudo wget http://wordpress.org/latest.tar.gz # download the latest wordpress
sudo -u www-data tar -xvf latest.tar.gz # extract it
sudo mv latest.tar.gz ../backups/wordpress-`date "+%Y-%m-%d"`.tar.gz # archive the compressed folder OR just delete it
sudo mv wordpress/* ./ # move the files out of the wordpress folder so the site will use them
sudo rm -R wordpress # delete the old wordpress folder

Domain Mapping

Us the WordPress MU Domain Mapping plugin.

See also