Image

linux

LAMP: Apache

How to install Apache2 on Ubuntu

2 min read
Last update: November 28, 2021

Digital Ocean: LAMP

1. Apache and firewall

sudo apt update
sudo apt install -y apache2
sudo ufw app list
sudo ufw app info "Apache Full"
sudo ufw allow in "Apache Full"
sudo apt install -y curl

2. MySQL

sudo apt install -y mysql-server
sudo mysql_secure_installation
Choose **STRONG level** password for public server and define it.
sudo mysql
If you have set password, you can use: `mysql -u root -p`
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;
exit

3. PHP

sudo apt install php libapache2-mod-php php-mysql
sed -i 's/DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm/DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm/g' /etc/apache2/mods-enabled/dir.conf
sudo systemctl restart apache2
sudo apt install php-cli

4. Virtual host

sudo mkdir /var/www/my-domain
sudo chown -R $USER:www-data /var/www/my-domain
sudo chmod -R 755 /var/www/my-domain
vim /var/www/my-domain/index.html
<html>
    <head>
        <title>Welcome to my-domain.localhost!</title>
    </head>
    <body>
        <h1>Success!  The my-domain.localhost server block is working!</h1>
    </body>
</html>
sudo vim /etc/apache2/sites-available/my-domain.conf
<VirtualHost *:80>
  ServerName my-domain.localhost
  DocumentRoot /var/www/my-domain
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
sudo a2ensite my-domain.conf
sudo apache2ctl configtest
sudo systemctl restart apache2

Configurations

Laravel

<VirtualHost *:80>
    ServerAdmin webmaster@my-domain.com
    ServerName laravel-app.localhost

    # Optional
    ServerAlias www.laravel-app.localhost
    # Path to the project
    DocumentRoot /var/www/laravel-app/public

    # On linux only
    ErrorLog ${APACHE_LOG_DIR}/laravel-app-error.log
    CustomLog ${APACHE_LOG_DIR}/laravel-app-access.log combined

    <Directory /var/www/laravel-app>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    <Directory /var/www/laravel-app/public>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>