Skip to content

Instantly share code, notes, and snippets.

@bobdesaunois
Forked from bartjakobs/server.markdown
Last active December 20, 2018 10:59
Show Gist options
  • Save bobdesaunois/23b883c6688e8fc0491dbc9e074056c6 to your computer and use it in GitHub Desktop.
Save bobdesaunois/23b883c6688e8fc0491dbc9e074056c6 to your computer and use it in GitHub Desktop.
Setup a server

Installing Apache, PHP7, MariaDB etc etc and install a Laravel project

Server

For this installation, I'm using Ubuntu 18.04 on DigitalOcean, with my ssh key already added for easy first-time login.

Login into the new server using ssh: ssh root@serverip

Updating the system

Before doing anything, update Ubuntu using the following commands: Because we're logged in as root, there's no need to use sudo.

# apt update
# apt dist-upgrade

Both commands can take a minute.

Create a user and disable root login

Use the following command to create a user

# adduser username

It'll ask you to choose a password and some personal account information. Choose a super safe password!

Use the usermod command to add the user to the sudo group.

# usermod -aG sudo username

After this, you can use su - (username) to test logging in as the user. If logging in works, you can test sudo rights for example by using sudo ls /root.

Add your SSH keys for the new user.

Now for the new user, you can add your SSH keys for easier and safer login. On the host computer, read your ssh keys by typing:

$ cat ~/.ssh/id_rsa.pub 

The output probably starts with with ssh-rsa. Copy the contents of the file.

On the server, type

$ nano ~/.ssh/authorized_keys

(or use any other text editor) And paste the contents of the public key. Save by typing control-o (enter) and quit by typing control-x.

Disable root login via SSH

Open the ssh configuration file:

$ sudo nano /etc/ssh/sshd_config

And add the following line:

PermitRootLogin no

Save by typing control-o (enter) and quit by typing control-x. After that, restart the SSH servers:

$ sudo service ssh restart

Install Apache, PHP and MariaDB

Install the following packages:

  • Apache2
  • php7.2
  • php7.2-mysql for connecting php to mysql
  • php7.2-xml for html parsing (laravel needs it)
  • php7.2-zip for reading zips - makes composer faster
  • php7.2-mbstring Multibyte strings 😎
  • mod-php php module for apache
  • composer Install your laravel project
  • MariaDB MySQL compatible database server
$ sudo apt install apache2 php7.2 php7.2-mysql php7.2-xml php7.2-zip php7.2-mbstring libapache2-mod-php7.2 php-curl composer mariadb-server zip unzip

First, secure the database and make database and user:

$ sudo mysql_secure_installation 

Choose a super safe root password and disable remote root login!

Now, login with that new password. (replace newuser, password and dbname)

$ mysql -u root -p
mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> CREATE DATABASE dbname;

Install a web application

If your git repository needs it, create a new ssh keypair and read the public key:

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.
The key fingerprint is: (...)
The key's randomart image is: (...)

$ cat ~/.ssh/id_rsa.pub

And set it up on the git server.

Go to the web server directory and clone the project:

$ cd /var/www
$ sudo chmod 777 .
$ git clone (git address)

$ cd (project)
$ composer install
$ cp .env.example .env
$ php artisan migrate

Now add the virtualhost

$ sudo nano /etc/apache2/sites-available/sitename.conf

And add something like

<VirtualHost *:80>
    ServerAdmin (email)
    ServerName (hostname)
    DocumentRoot /var/www/(cloned git dir)/public
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<Directory /var/www/(cloned git dir)/public>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
</Directory>

Save and activate it using

$ sudo a2ensite sitename

Also activate mod_rewrite

$ sudo a2enmod rewrite
$ sudo systemctl reload apache2

Have a nice weekend.

Sources:

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