How to install Ghost blog on Ubuntu
Recently, I migrated This Dev Brain and Brain Archives to new VPSs. In both cases, I had to set up the operating systems and install Ghost from scratch. As a result, I've prepared this concise set of instructions in case I need to do this again.
Configure Ubuntu
Add user
Create a new user:
adduser ghostuser
Add user to superuser group to unlock admin privileges:
usermod -aG sudo ghostuser
Log in as the new user:
su - ghostuser
Update packages
Update package lists:
sudo apt-get update
Update installed packages:
sudo apt-get upgrade
Install Nginx
Install Nginx package:
sudo apt-get install nginx
Enable Nginx in firewall:
sudo ufw allow 'Nginx Full'
Install Node.js
Download and import the Nodesource GPG key:
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
Create deb repository:
NODE_MAJOR=18 # Set latest version that is compatible with Ghost
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
Update and install packages:
sudo apt-get update
sudo apt-get install nodejs -y
Install MySQL
Install MySQL server package:
sudo apt-get install mysql-server
Secure MySQL:
sudo mysql_secure_installation
Configure it like this:
- Set root password? [Y/n] Y
- Remove anonymous users? [Y/n] Y
- Disallow root login remotely? [Y/n] Y
- Remove test database and access to it? [Y/n] Y
- Reload privilege tables now? [Y/n] Y
In some cases themysql_secure_installation
might fail because of authorization error. In that case runsudo mysql
and then, in MySQL console, run this command:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'Here_Is_Your_MySQL_Root_User_Password';
Once database is updated, themysql_secure_installation
script should work properly.
Once the MySQL server is configured, start and enable the MySQL service:
sudo systemctl start mysql
sudo systemctl enable mysql
Setup Database
Login to MySQL server:
sudo mysql -u root -p
Create database:
CREATE DATABASE ghostdb;
Create user:
CREATE USER 'ghostuser'@'localhost' IDENTIFIED BY 'Here_is_Ghost_User_Password';
Set privileges:
GRANT ALL PRIVILEGES ON ghostdb. * TO 'ghostuser'@'localhost';
Flush privileges:
FLUSH PRIVILEGES;
Close MySQL terminal:
exit;
Setup Ghost
Ghost CLI
Install Ghost CLI:
sudo npm install ghost-cli@latest -g
Setup folder with the site
Create directory. Change sitename
to name of your website:
sudo mkdir -p /var/www/<sitename>
Set directory owner. Replace ghostuser
with the name of the user created at the very beginning of this article:
sudo chown ghostuser:ghostuser /var/www/<sitename>
Set the correct permissions
sudo chmod 775 /var/www/<sitename>
Then navigate into it:
cd /var/www/<sitename>
Ghost Blog
Install Ghost Blog:
ghost install
Setup Ghost:
Install questions
? Enter your blog URL: https://sitename.com
? Enter your MySQL hostname: 127.0.0.1
? Enter your MySQL username: ghostuser
? Enter your MySQL password: [hidden]
? Enter your Ghost database name: ghostdb
Once Ghost Blog is configured, it should start and be ready for further setup at https://sitename.com/ghost
.