Skip to content

Instantly share code, notes, and snippets.

@RoverWire
Last active February 17, 2016 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RoverWire/dd0f48a529f3a7f71587 to your computer and use it in GitHub Desktop.
Save RoverWire/dd0f48a529f3a7f71587 to your computer and use it in GitHub Desktop.
Ubuntu Edge Configuration

Ubuntu + Apache + Nginx (as reverse proxy) + PHP + MariaDB + phpMyAdmin

Before Start

Log in to your ubuntu server as a root user.

Supposing that

  • server ip address is: 1.2.3.4
  • domain my-domain.com
  • hostname server.my-domain.com
  • web folder is /var/www/my-domain/public

Optional: if you going to use php 7, you must add this ppa

$ add-apt-repository ppa:ondrej/php

Note: If your system's locale is set to anything other than UTF-8, adding the PPA may fail due to a bug handling characters in the author's name. As a workaround, you can install language-pack-en-base to make sure that locales are generated, and override system-wide locale settings while adding the PPA:

$ apt-get install -y language-pack-en-base 
$ LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php

Update and Upgrade

$ apt-get update && sudo apt-get upgrade

Install Apache

$ apt-get install apache2

By default, apache listens on port 80. We have to configure apache to run on port 8080 for our proxy setup as port 80 will be used by nginx later. We have to edit the apache configuration file /etc/apache2/ports.conf. And then proceed with the virtual host configuration in the /etc/apache2/sites-available/ directory.

First change the port for apache to 8080 by editing the file “ports.conf” with the vim editor.

$ vim /etc/apache2/ports.conf

Change port 80 to 8080 as follows:

 # If you use virtual host this appears too
NameVirtualHost *:8080 
 # If you don't have any virtual host, only this appears
Listen 8080

Copy the default 000-default.conf to another file, I usually name the file as domain, on this case: my-domain.com.conf

$ cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/my-domain.com.conf

and edit the file with $ vim /etc/apache2/sites-available/my-domain.conf, change the port:

<VirtualHost *:8080>

and add this lines:

ServerName www.my-domain.com
ServerAlias my-domain.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/my-domain/public

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

Since HTTP requests is now handled by Nginx, we can disable KeepAlive in Apache. Edit /etc/apache2/apache2.conf and change:

KeepAlive Off

Install mod_rpaf in Apache to forward visitor IP to Apache. Otherwise, your scripts will read REMOTE_ADDR values as server IP:

$ apt-get install libapache2-mod-rpaf
$ vim /etc/apache2/mods-available/rpaf.conf

and add server ip on the line 10

RPAFproxy_ips 127.0.0.1 1.2.3.4 ::1

Test the configurations with $ apachectl configtest and if it has no errors, proceed to disable default config, enable new config, disable unuse modules and restart apache:

$ a2dissite 000-default
$ a2ensite my-domain.com
$ a2dismod deflate
$ a2dismod cgi
$ a2dismod autoindex
$ a2dismod negotiation
$ a2dismod ssl
$ service apache2 restart

You need to create a test file on your web folder:

$ echo "It Works!" > /var/www/my-domain/index.html

Now the test index should be showed at http://1.2.3.4:8080/ (or http://my-domain.com:8080 if you already setup DNS).

Install PHP

Once apache is installed, you must install PHP. If you use 5.x version of php, then you should install with:

$ apt-get install php5 php5-mysql libapache2-mod-php5 php5-gd

for php 7 you should use:

$ apt-get install php7.0 php7.0-mysql libapache2-mod-php7.0 php7.0-gd

Now, replace the index.html for an index.php on your web folder and restart apache:

$ rm /var/www/my-domain/index.html
$ echo "<?php phpinfo();?>" > /var/www/my-domain/index.php
$ service apache2 restart

Now the php info should be showed at http://1.2.3.4:8080/

Install NGINX

Install and once nginx is installed, you need to edit configuration to use it as reverse proxy for apache:

$ apt-get install nginx
$ vim /etc/nginx/nginx.conf

Uncomment all gzip related lines to enable compression:

 # Gzip Settings
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

Right under gzip settings, add these proxy cache settings:

 # Proxy Cache Settings
proxy_cache_path /var/cache levels=1:2 keys_zone=reverse_cache:60m inactive=90m max_size=1000m;

Remove all the enabled configurations and create a new one:

$ rm -rf /etc/nginx/sites-enabled/*
$ vim /etc/nginx/sites-available/my-domain.com.conf

and add this lines

server {
    listen 80;

    # Site Directory same in the apache virtualhost configuration
    root /var/www/my-domain/public; 
    index index.php index.html index.htm;

    # Domain
    server_name www.reverse.com reverse.com;

    location / {
        index index.php index.html
        try_files $uri $uri/ =404;
    }


    # Reverse Proxy and Proxy Cache Configuration
    location ~ \.php$ {
 
        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;

        # Cache configuration
        proxy_cache reverse_cache;
        proxy_cache_valid 3s;
        proxy_no_cache $cookie_PHPSESSID;
        proxy_cache_bypass $cookie_PHPSESSID;
        proxy_cache_key "$scheme$host$request_uri";
        add_header X-Cache $upstream_cache_status;
    }

    # Enable Cache the file 30 days
    location ~* .(jpg|png|gif|jpeg|css|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ {
        proxy_cache_valid 200 120m;
        expires 30d;
        proxy_cache reverse_cache;
        access_log off;
    }

    # Disable Cache for the file type html, json
    location ~* .(?:manifest|appcache|html?|xml|json)$ {
        expires -1;
    }

    location ~ /\.ht {
        deny all;
    }
}

Then activate the new virtualhost configuration and restart nginx.

$ ln -s /etc/nginx/sites-available/my-domain.com.conf /etc/nginx/sites-enabled/my-domain.com.conf 
$ service nginx restart

At this point, your web folder is served on port 80 and you can view it through http://1.2.3.4/

Maria DB

Forked from MySQL, MariaDB has been known to be a drop-in replacement that brings enhancements and performance optimizations over MySQL. To install and configure just need to type

$ apt-get install mariadb-server
$ mysql_secure_installation

After installation, run mysql -u root -p. Enter your password when prompted. You will see output similar to the following:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is XXXX
Server version: 5.5.X

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

Install phpMyAdmin

if you are using php5 you can install it with

$ apt-get install phpmyadmin

Skip the automatic configuration for apache or nginx.

If you are using php 7, the automatic install will not work since throws warnings for required php5 modules, so we need to download it manually and extract:

$ cd /usr/share
$ wget https://files.phpmyadmin.net/phpMyAdmin/4.5.4.1/phpMyAdmin-4.5.4.1-all-languages.zip
$ unzip phpMyAdmin-4.5.4.1-all-languages.zip
$ mv phpMyAdmin-4.5.4.1-all-languages phpmyadmin
$ chmod -R 0755 phpmyadmin

and add this lines con /etc/apache2/sites-available/my-domain.com.conf anywhere after DocumentRoot declaration:

Alias /phpmyadmin "/usr/share/phpmyadmin/"
<Directory "/usr/share/phpmyadmin/">
     Order allow,deny
     Allow from all
     Require all granted
</Directory>

Note: You can add it on /etc/apache2/apache.conf to avoid to do it on each apache site configuration

and then just need to disable site, enable it again and restart apache for the changes take effect

$ a2dissite my-domain.com
$ a2ensite my-domain.com
$ service apache2 restart

If you navigate to http://1.2.3.4/phpmyadmin/index.php you will notice that the images are not shown, cause nginx serve the static files and show the folder index on port 80 request. To solve this, you need this lines on /etc/nginx/sites-available/my-domain.com.conf within server declaration

server {
[...]
	location /phpmyadmin {
	    root /usr/share;
	    index index.php;
	    location ~ ^/phpmyadmin/(.+\.php)$ {
	        try_files $uri =404;
	        root /usr/share/;
	        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;
	
	        # Cache configuration
	        proxy_cache reverse_cache;
	        proxy_cache_valid 3s;
	        proxy_no_cache $cookie_PHPSESSID;
	        proxy_cache_bypass $cookie_PHPSESSID;
	        proxy_cache_key "$scheme$host$request_uri";
	        add_header X-Cache $upstream_cache_status;
	    }
	
	    location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
	        root /usr/share/;
	    }
	}
	
[...]
}

and then refresh enabled configuration and restart nginx

$ rm -rf /etc/nginx/sites-enabled/*
$ ln -s /etc/nginx/sites-available/my-domain.com.conf /etc/nginx/sites-enabled/my-domain.com.conf 
$ service nginx restart

And that's it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment