Install Portainer to Manage Containers – Nginx, MySQL, WordPress

Portainer is a lightweight management UI which allows you to easily manage your different Docker environments (Docker hosts or Swarm clusters). It is meant to be as simple to deploy as it is to use. It consists of a single container that can run on any Docker engine (can be deployed as Linux container or a Windows native container). Portainer allows you to manage your Docker containers, images, volumes, networks and more !

1. Installation in CentOS / Ubuntu
1.1 CentOS 7/Debian 9

Here is an easiest way to install docker into your CentOS / Debian system. It is just one command:

`


curl -sSL https://get.docker.com/ | sh

```

Start docker service and enable it when system started

```

sudo -i

systemctl start docker.service
systemctl enable docker.service

create a volume dedicated for Portainer's data storage:

docker volume create portainer_data

use docker command to run Portainer container:

docker run -d -p 9000:9000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

1.2 Ubuntu

System update

sudo apt update
sudo apt upgrade

Install docker into Ubuntu:

sudo apt install docker.io -y

Start docker service and enable it when system started

sudo -i

systemctl start docker
systemctl enable docker

docker version

create a volume dedicated for Portainer's data storage:

docker volume create portainer_data

use docker command to run Portainer container:

docker run -d -p 9000:9000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

docker ps

[![|400x76](https://s3.tenten.co/images/2020/05/4112828c9f5e9190c3bd79cf8d5ea8a8-thumb.jpg)](https://s3.tenten.co/images/2020/05/4112828c9f5e9190c3bd79cf8d5ea8a8-thumb.jpg)

2. Log into Portainer Web Gui

Use your browser to access URL http://:9000

First time, it will ask you to set up admin user's password.

[![|400x241](https://s3.tenten.co/images/2020/05/b3854a53c2ab166a48bf98e153396f9c-thumb.jpg)](https://s3.tenten.co/images/2020/05/b3854a53c2ab166a48bf98e153396f9c-thumb.jpg)

Since portainer is deployed on local machine and we are managing it from local, we will choose local to connect.

[![|400x196](https://s3.tenten.co/images/2020/05/76ef7448bf2416dbb2c4673a1eec34f0-thumb.jpg)](https://s3.tenten.co/images/2020/05/76ef7448bf2416dbb2c4673a1eec34f0-thumb.jpg)

Now it is Portainer's home page.

[![|400x230](https://s3.tenten.co/images/2020/05/cec11d03e8c01cc7ea80655f48655c4d-thumb.jpg)](https://s3.tenten.co/images/2020/05/cec11d03e8c01cc7ea80655f48655c4d-thumb.jpg)

As you can see from the following screenshot, Portainer container has been deployed.

[![|400x172](https://s3.tenten.co/images/2020/05/2023e947843495293e8e457ddbee21a3-thumb.jpg)](https://s3.tenten.co/images/2020/05/2023e947843495293e8e457ddbee21a3-thumb.jpg)

3. Deploy WordPress App Container

3.1 Deploy Nginx

Click button "+Add container" and enter following information for Nginx container.

[![|400x327](https://s3.tenten.co/images/2020/05/1109e5e858c722e13779bf1fb5e0fd26-thumb.jpg)](https://s3.tenten.co/images/2020/05/1109e5e858c722e13779bf1fb5e0fd26-thumb.jpg)

You can use your browser to access your virtual machine's public ip to confirm nginx is runing.

[![|400x161](https://s3.tenten.co/images/2020/05/ef36595a37800c0a08d1e80d375b1061-thumb.jpg)](https://s3.tenten.co/images/2020/05/ef36595a37800c0a08d1e80d375b1061-thumb.jpg)

3.2 Deploy MySQL DB - MariaDB

[![|400x315](https://s3.tenten.co/images/2020/05/ddb4dc29a8226448c7c0bdab6efd6177-thumb.jpg)](https://s3.tenten.co/images/2020/05/ddb4dc29a8226448c7c0bdab6efd6177-thumb.jpg)

There are two environment variables need to be added in.

MYSQL_ROOT_PASSWORD

MYSQL_DATABASE

Also do not forget to change Restart policy to always to make sure your container survive system reboot.

3.3 Deploy WordPress

[![|400x315](https://s3.tenten.co/images/2020/05/cc6e383d6c471c445253bd4de40d97e6-thumb.jpg)](https://s3.tenten.co/images/2020/05/cc6e383d6c471c445253bd4de40d97e6-thumb.jpg)

Or You can deploy WordPress Stack to add WordPress and MySql at the same time, which is the way I prefer.

[![|400x315](https://s3.tenten.co/images/2020/05/cfb0955c0350facea280810587409059-thumb.jpg)](https://s3.tenten.co/images/2020/05/cfb0955c0350facea280810587409059-thumb.jpg)

3.4 Configure Nginx Reverse Proxy

[![|320x156](https://s3.tenten.co/images/2020/05/ed46cb972892cf185aea58c563a6f857-thumb.jpg)](https://s3.tenten.co/images/2020/05/ed46cb972892cf185aea58c563a6f857-thumb.jpg)[![|320x170](https://s3.tenten.co/images/2020/05/10df5138448bf0020795ae01efb569d0-thumb.jpg)](https://s3.tenten.co/images/2020/05/10df5138448bf0020795ae01efb569d0-thumb.jpg)

apt -y update
apt -y install nano

create a wordperss Nginx configuration file:

nano /etc/nginx/conf.d/wordpress.conf

copy following configuration into the file:(make sure to change port 80, server_name and proxy_pass to your own configuration)

server {
listen 80;
server_name www.51sec.org 51sec.org;

location / {
proxy_pass http://3.45.23.194:20000;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

[![|400x258](https://s3.tenten.co/images/2020/05/a8d326c01beae70234d7e93b55a18738-thumb.jpg)](https://s3.tenten.co/images/2020/05/a8d326c01beae70234d7e93b55a18738-thumb.jpg)

Once saved the conf file, restart nginx service to apply the new configuration.

/etc/init.d/nginx restart

[![|640x376](https://s3.tenten.co/images/2020/05/563e4d38793db87d586a10726847ecbf-thumb.jpg)](https://s3.tenten.co/images/2020/05/563e4d38793db87d586a10726847ecbf-thumb.jpg)

There is a bug for command /etc/init.d/nginx restart. The restarting nginx status will not change although restarting procedure has been completed.

3.5 Configure Nginx to do load balance

If you have multiple sites, you can configure Nginx to load balance between multiple servers.

There are two files you will need to change:

root@f0e9bd28fe02:/# cat /etc/nginx/nginx.conf
user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http { upstream mysec {
ip_hash;
server 3.81.70.239:30000 weight=3;
server 34.73.78.142:80 weight=2;
}

include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;
}
root@f0e9bd28fe02:/#

root@f0e9bd28fe02:/# cat /etc/nginx/conf.d/wordpress.conf
server {
listen 80;
server_name www.51sec.org;

location / {
proxy_pass http://mysec;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
root@f0e9bd28fe02:/#

Another simple Nginx Load Balance Configuration

http {
upstream project1 {
server 127.0.0.1:8000 weight=3;
server 127.0.0.1:8001 weitht=2;
server 127.0.0.1:8002;
}

server {
listen 80;
server_name www.51sec.org;
location / {
proxy_pass http://project1;
}
}
}

Related YouTube Videos:

References:

```

- [使用Portainer轻松将Docker用在生产环境](https://lala.im/4983.html)
- [How to Manage Docker Containers using Portainer on Ubuntu](https://www.howtoforge.com/tutorial/ubuntu-docker-portainer/)

### _Related_