Skip to content

Instantly share code, notes, and snippets.

@tommyskott
Last active August 19, 2020 13:51
Show Gist options
  • Save tommyskott/65df32d880fdd6e061981292ed4a949e to your computer and use it in GitHub Desktop.
Save tommyskott/65df32d880fdd6e061981292ed4a949e to your computer and use it in GitHub Desktop.

Apache, Php and Certbot on Ubuntu

Apache

Install Apache
sudo apt-get update
sudo apt-get install apache2
Setup virtual host from deafult conf
cd /etc/apache2/sites-available/
sudo cp 000-default.conf 001-example.com.conf
Configure your new virtual host
sudo nano 001-example.com.conf
<VirtualHost *:80>
    ServerAdmin you@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    
    <Directory /var/www/example.com/public_html>
        AllowOverride All
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    # https force redirect
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =example.com [OR]
    RewriteCond %{SERVER_NAME} =www.example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Enable site
sudo a2ensite 001-example.com.conf
sudo service apache2 reload
sudo apachectl restart
Disable site
sudo a2dissite 001-example.com.conf
sudo service apache2 reload
sudo apachectl restart
Check for errors
sudo apachectl configtest

Php

Setup apt-repo and install php
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

sudo apt install php7.4

sudo apt install php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl -y
Enable / disable module
sudo a2dismod php5
sudo a2enmod php7.4
sudo apachectl restart

which php
php -v
php --ini
php -i
php -m

Certbot

Don't install certbot like below, it doesn't work anymore.
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-apache
If you already installed certbot like the example above, remove it with:
sudo apt-get remove certbot
Install certbot like this instead, using snap:
sudo snap install --classic certbot

If you don't have snap pre-installed, read more here: Snap

Setup certbot apache config and create certificates automatically
sudo certbot --apache
Test automatic renewal
sudo certbot renew --dry-run
Create new certificates
sudo certbot --apache -d example.com -d www.example.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment