Install Flarum beta-12 on Ubuntu Nginx server
I have done a tutorial like this in the past for beta 8 but as time went by I learned more and some people pointed out problems so here it is a new one but now for beta 12.
I just recently had to migrate content to a new server and fresh install Flarum beta 12. For that reason I have encountered a lot of strange problems and dispersed information.
You might also be interested in checking out @DursunCan script (here) that does all this with a single command. But if it doesn't work or if you need more customization or you want to learn, feel free to follow along. Some of the code here was copied from his script because I felt his code sometime was better and less prone to errors.
I'll post a tutorial doing a clean install + migrating content soon. This way you have a shiny and clean server with the same old information.
What I want?
- be able to have multiple websites and apps on the same server
- run Flarum smoothly
- try not to kill myself trying to make this work
I'll organize this tutorial in sections
- Setup the server
- Install Flarum
- Setup Nginx
- Set up Database
- Throw a huge party and invite me 😂
1. Server
For this I used a 5€ droplet from DigitalOcean. Create a simple one and access it via ssh.
These are the requirements:
This is the server I am using from digitalocean:
Using the root user lets install the Nginx and create a new sudo user (because it is not recommended to do a lot in root... it might break your droplet).
apt-get update
apt-get install nginx
adduser username
usermod -aG sudo username
su - username
(change "username" to the name you want... it will ask you for a password and then information... you can skip the information by typing the key enter in all)
Install mysql
sudo apt-get install mysql-server
Install php
...in my case php 7.4
Search it online... it depends on the version
Example:
sudo apt -y install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt -y install php7.4
Install ALL the php extensions they ask
Now here is where it starts go get tricky. Besides the one they ask you have also to install php-fpm for nginx to work properly.
sudo apt-get install php7.4-fpm php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip -y
Tokenizer is already included and the openssl should be as well but you can test it running php -i | grep -i openssl
and it should show amongst other things OpenSSL support => enabled.
I am writing this by memory or by searching online so some things might be miss typed... Correct me if I am wrong.
2. Install Flarum
First install composer
Download composer.phar
and then move it to the bin directory like this:
mv /path/to/file/composer.phar /usr/local/bin/composer
Install flarum
So now its time to install Flarum. Since I want to have multiple apps in the same server I have selected the directory /var/www
for all my apps. Each will be in its own folder inside that www/ folder. I've created a folder for flarum and install it.
sudo mkdir /var/www/flarum
sudo chmod -R 777 /var/www/flarum
cd /var/www/flarum
composer create-project flarum/flarum . --stability=beta
the last command might take a while... any question see the flarum tutorial: https://flarum.org/docs/install.html#installing
If you get a memory error you need to create some swap memory to compensate for low amount of memory of the server. On digitalocean there is a useful tutorial on how to do this: https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-18-04
Also run this to avoid permission errors
sudo chown www-data:www-data -R /var/www/
3. Setup Nginx
Now is where my knowledge goes out of the window and my desire to take my own life begun. It took me 2 days to figure this out and understand how it works.
Nginx is the software that receives the http and https requests and points them to the right directory where the app or website files are located. One of my main goals was being able to have multiple websites and apps on the same server and Nginx can do that. It is called Server Blocks and it is fairly easy to understand but confusing to implement.
You have to create a .conf file in the sites-available/
folder for every website or app (in my case located in /etc/nginx/sites-available
). When Nginx receives an http or https request it will search in all of these files for the one that it corresponds to. Each .conf file will represent one app and will have information like the domain expected for the app and location of the files in the server.
Create the new .conf file:
sudo nano /etc/nginx/sites-available/flarum.conf
this will open a text editor where you can insert the code.
My .conf file looks like this (and is named flarum.conf). This one is from @DursunCan script because his was better than mine and worked just fine. Feel free to copy and only change your url:
server {
listen 80;
listen [::]:80;
server_name www.example.com;
return 301 http://example.com$request_uri;
}
server {
listen 80;
listen [::]:80;
root /var/www/flarum/public;
index index.php index.html index.htm;
server_name example.com;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\. {
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
log_not_found off;
access_log off;
allow all;
}
include /var/www/flarum/.nginx.conf;
}
Change any necessary details like the "example.com" and "root /var/www/flarum;" and include /var/www/flarum/.nginx.conf; if it is in other folder.
To save and exit the editor just do ctrl+x and then type "y" to save and enter.
Now we need to disable the default website of nginx and make our new flarum website enabled:
sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/flarum.conf /etc/nginx/sites-enabled/
By now the domain should be working.
SSL certificate (optional)
If you want an SSL certificate do the following. This code was also copied from @DursunCan.
Install certbot:
sudo add-apt-repository ppa:certbot/certbot -y
sudo apt update
sudo apt install python-certbot-nginx -y
Generate SSL and set everything up (Don't forget to replace the "example.com" with your url and your email):
certbot --nginx --agree-tos --redirect --no-eff-email -m example@gmail.com -d example.com -d www.example.com
4. Create DataBase
Ok so it should be all working but you need a database and a user to give to flarum so it can finish the installation and start looking like a proper website.
For this we need to first create the database.
Switch to the root user and access mysql:
su - root
mysql -u root -p
you might need to type the root password
Then lets create the database, user and assign permissions
CREATE DATABASE flarumdb;
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON flarumdb.* To 'newuser'@'localhost';
exit
I named the databased flarumdb...
Replace newuser and password with whatever you want but leave the '' there!!!! This user is only for flarum so the password can be a random combination of letters and numbers
Party like there is no tomorrow
Ok by now everything should be working and you have a database and a user with privileges to that database.
Access the domain and finish the setup of Flarum.
When setting up flarum you'll be asked for the databese prefix. It is the same as the name. For example mine is called flarumdb so the prefix is flarumdb as well.