Skip to content

Instantly share code, notes, and snippets.

@ManuelTS
Last active October 16, 2020 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ManuelTS/7d96cfd29fe8e623fb110556a6bda1cd to your computer and use it in GitHub Desktop.
Save ManuelTS/7d96cfd29fe8e623fb110556a6bda1cd to your computer and use it in GitHub Desktop.
Install Apache2, MySQL, and Wordpress on Ubuntu 20.04

Install Apache2, MySQL, and Wordpress on Ubuntu 20.04

Install Apache2 and MySQL

sudo apt update -y && \ # Update dependencies
sudo apt upgrade -y && \
sudo apt install -y php7.4 php7.4-curl php7.4-gd php7.4-json php7.4-mbstring php7.4-xml libapache2-mod-php7.4 php7.4-mysql apache2 apache2-bin apache2-data apache2-utils mysql-server phpmyadmin && \ # Install dependencies
sudo mysql_secure_installation

The password for phpmyadmin can be phpmyadmin, but chose one for yourself.

You will be asked for a MySQL Root Password, choose your own, I chose: kjdndnakdnejuef. Leave the anonymus users on the DataBase (DB), only allow root access from localhost, remove the test DB,

Re-, Start and Stop

To restart Apache and MySQL services, type:

sudo systemctl restart apache2
sudo systemctl restart mysql

To start Apache and MySQL services, type:

sudo systemctl start apache2
sudo systemctl start mysql

To stop Apache and MySQL services, type:

sudo systemctl stop apache2
sudo systemctl stop mysql

Quick first test

sudo rm /var/www/html/* && \ # Clear public folder
sudo nano /var/www/html/index.php

and paste

<?php 
foreach (scandir('.') as $file)
    echo "<a href=\"" . $file . "\">" . $file . "</a><br/>";

phpinfo();
?>

Then go to http://localhost/info.php to see the php info page.

Install Wordpress

  1. Download Wordpress
  2. Unpack the .zip
  3. Move the contained wordpress folder with the contents to the location you want and rename it to <your_folder_name>
  4. I chose homepage, which will be used from now on.
  5. In sudo nano /etc/apache2/sites-available/<your_folder_name>.conf paste:
    <Directory absolute/path/2/<your_folder_name>>
        Options FollowSymLinks
        AllowOverride Limit Options FileInfo All
        DirectoryIndex index.php
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    <Directory absolute/path/2/<your_folder_name>/wp-content>
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    
  6. Run:
    sudo a2ensite <your_folder_name> && \
    sudo systemctl reload apache2 && \
    sudo systemctl restart apache2 && \
    sudo mysql -u root -p<your_previously_set_pwd> -e "CREATE DATABASE <databaseName>;"
    
  7. Create another wordpress user than root and grant him all priviliges:
    CREATE USER <userName>@localhost IDENTIFIED BY '<user_password>';
    GRANT ALL ON <databaseName>.* TO <userName>@localhost;
    FLUSH PRIVILEGES;
    
  8. Place in your/path2/homepage/wp-config.php this content:
      <?php
        /** The base configurations of the WordPress. */
        $files = array( //any constant which is defined in this files, wont be overwritten by the default wp-config.php
            '/wp-config_staging.php',
            '/wp-config_local.php'
        );
    
        foreach($files as $file) {
            if (file_exists(__DIR__ . $file)) {
                include_once __DIR__ . $file;
            }
        }
    
        $baseURL = $_SERVER['REMOTE_ADDR']=='127.0.0.1' || $_SERVER['REMOTE_ADDR']=='::1' ? 'http://localhost/<your_project_name>' : $_SERVER['REMOTE_ADDR'];
    
        $configs = array( // Production config
            // DB
            'DB_HOST' => $_SERVER['REMOTE_ADDR'] == '127.0.0.1' || $_SERVER['REMOTE_ADDR'] == '::1' ? '127.0.0.1:3307' : 'localhost',
            'DB_NAME' => '<your_db_name>',
            'DB_USER' => '<your_wp_user_name>',
            'DB_PASSWORD' => '<your_wp_password>',
            'DB_CHARSET' => 'utf8',
            'DB_COLLATE' => '',
            // WP
            'FS_METHOD' =>'direct',
            'WP_SITEURL' => $baseURL,
            'WP_HOME' => $baseURL,
            'WP_DEBUG' => false,
            'WP_DEBUG_LOG', false,
            'WP_DEBUG_DISPLAY', false,
            'SCRIPT_DEBUG', false,
            'ABSPATH' => dirname(__FILE__) . '/',
            'AUTH_KEY' => '<apply_from_old_wp-config.php>',
            'SECURE_AUTH_KEY' => '<apply_from_old_wp-config.php>',
            'LOGGED_IN_KEY' => '<apply_from_old_wp-config.php>',
            'NONCE_KEY' => '<apply_from_old_wp-config.php>',
            'AUTH_SALT' => '<apply_from_old_wp-config.php>',
            'SECURE_AUTH_SALT' => '<apply_from_old_wp-config.php>',
            'LOGGED_IN_SALT' => '<apply_from_old_wp-config.php>',
            'NONCE_SALT' => '<apply_from_old_wp-config.php>',
        );
    
        foreach($configs as $key => $value) {
            if (!defined($key)) {
                define($key, $value);
            }
        }
    
        $table_prefix  = 'wp_';
        require_once(ABSPATH . 'wp-settings.php');
    
  9. (Re)Start best both services, apache2 + mysql with:
    sudo systemctl start apache2 mysql
    
  10. Create in the /var/www/html directory a symlink to your homepage wordpress project directory with:
    sudo ln -s /home/your/project/dir/homepage /var/www/html/homepage # 1. is source dir, 2. is target dir
    
  11. Go to http://localhost/homepage and complete the wordpress setup
  12. Now develop!

If subsites are not working

  1. Watch the apache2 error logs with tail -F /var/log/apache2/error.log
  2. In the /etc/apache2/apache2.conf, inform yourself about security risks beforehand, but make changes to:
    <Directory /var/www/html/<your_wp_folder_name>>
        Options FollowSymLinks
        AllowOverride All
        DirectoryIndex index.php
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    <Directory /var/www/html/<your_wp_folder_name>/wp-content>
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    
    
  3. Run:
    sudo a2enmod rewrite &&\
    sudo service apache2 restart
    
  4. You may need to execute:
    sudo find wp-content/ -type d -exec chmod g+rx {} +
    sudo find wp-content/ -type f -exec chmod g+r {} +
    

Additional Ubuntu Service Commands

To enable apache2 service on boot up run

sudo systemctl enable apache2

To disable apache2 service on boot up run

sudo systemctl disable apache2

To check whether the service is currently configured to start on the next boot up

systemctl is-enabled apache2

To see the service status

systemctl status service-name

To start the service

sudo systemctl start service-name

To restart the service

systemctl restart apache2

To stop the service

sudo systemctl stop service-name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment