Skip to content

Instantly share code, notes, and snippets.

@mtvbrianking
Last active June 24, 2023 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mtvbrianking/666d8dad01fff32db5d67e359f47806a to your computer and use it in GitHub Desktop.
Save mtvbrianking/666d8dad01fff32db5d67e359f47806a to your computer and use it in GitHub Desktop.
Setup digital ocean apache web server php mysql

Prerequisties

Login

bmatovu@home-pc:~# ssh root@xxx.xxx.xxx.xxx

root@ubuntu-512mb-nyc3-01:~$ sudo su

root@ubuntu-512mb-nyc3-01:~#

Update package manager

root@ubuntu-512mb-nyc3-01:~# apt-get update

root@ubuntu-512mb-nyc3-01:~# apt-get upgrade

Apache

Installation

root@ubuntu-512mb-nyc3-01:~# apt-get install apache2

MySQL

Installation

root@ubuntu-512mb-nyc3-01:~# apt-get install mysql-server

Initialization

root@ubuntu-512mb-nyc3-01:~# mysqld --initialize

Verification

root@ubuntu-512mb-nyc3-01:~# mysql --version

Set default mysql user password.

Note: Default mysql user is root with no password.

root@ubuntu-512mb-nyc3-01:~# mysql -u root
...
mysql> USE mysql;
mysql> SELECT user, plugin, host FROM mysql.user WHERE user = 'root';
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'set_root_user_mysql_pswd_here';
mysql> FLUSH PRIVILEGES;
mysql> \q
root@ubuntu-512mb-nyc3-01:~# service mysql restart
root@ubuntu-512mb-nyc3-01:~# mysql -u root -p
...
mysql> 

PHP

Installation

root@ubuntu-512mb-nyc3-01:~# apt-add-repository ppa:ondrej/php

root@ubuntu-512mb-nyc3-01:~# apt-get update

root@ubuntu-512mb-nyc3-01:~# apt-get install php7.4

Install PHP extensions (Optional)

root@ubuntu-512mb-nyc3-01:~# apt-cache pkgnames | grep php7.4

root@ubuntu-512mb-nyc3-01:~# apt-get install -y php7.4 libapache2-mod-php7.4 php7.4-cli php7.4-common php7.4-mbstring php7.4-gd php7.4-intl php7.4-xml php7.4-mysql php7.4-zip

Verify LAMP setup

root@ubuntu-512mb-nyc3-01:~# cd /var/www/html

root@ubuntu-512mb-nyc3-01:/var/www/html# touch info.php

root@ubuntu-512mb-nyc3-01:/var/www/html# echo "<?php phpinfo(); ?>" > info.php

Visit: http://xxx.xxx.xxx.xxx/info.php

root@ubuntu-512mb-nyc3-01:/var/www/html# rm info.php

phpmyadmin

root@ubuntu-512mb-nyc3-01:~# apt-get install phpmyadmin

root@ubuntu-512mb-nyc3-01:~# a2enmod rewrite

root@ubuntu-512mb-nyc3-01:~# service apache2 restart

Note: phpmyadmin default user is phpmyadmin, not root with the password is what you set during phpmyadmin installation.

Confirm phpmyadmin user credentials

root@ubuntu-512mb-nyc3-01:~# cat /etc/phpmyadmin/config-db.php

Browser access

Visit: http://xxx.xxx.xxx.xxx/phpmyadmin

Application database.

Create new user and database.

root@ubuntu-512mb-nyc3-01:~# mysql -u root -p
...
mysql> SELECT DISTINCT user, host, authentication_string, password_expired FROM mysql.user;

mysql> CREATE USER augur@localhost IDENTIFIED BY 'T@urs.Augur';

mysql> CREATE DATABASE augur_tours;

mysql> GRANT ALL PRIVILEGES ON augur_tours.* TO augur@localhost;

mysql> FLUSH PRIVILEGES;

mysql> \q
...
root@ubuntu-512mb-nyc3-01:~# mysql -u augur -p

Composer - PHP Package Manager

root@ubuntu-512mb-nyc3-01:/var/www/html# php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

root@ubuntu-512mb-nyc3-01:/var/www/html# php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

root@ubuntu-512mb-nyc3-01:/var/www/html# php composer-setup.php

root@ubuntu-512mb-nyc3-01:/var/www/html# php -r "unlink('composer-setup.php');"

Test composer

root@ubuntu-512mb-nyc3-01:/var/www/html# php composer.phar --version

Setup composer globally

root@ubuntu-512mb-nyc3-01:/var/www/html# mv composer.phar /usr/local/bin/composer

root@ubuntu-512mb-nyc3-01:/var/www/html# composer --version

Verify you have git installed

root@ubuntu-512mb-nyc3-01:/var/www/html# git --version

Apache virtual hosts

vim /etc/apache2/sites-available/example.com.conf

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin admin@example.com

    DocumentRoot "/var/www/html/example"
    <Directory "/var/www/html/example">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/example.error.log
    CustomLog ${APACHE_LOG_DIR}/example.access.log combined
</VirtualHost>

APACHE_LOG_DIR ==> /var/log/apache2

Activate new configuration

user@server:/# service apache2 reload

Enable site

user@server:/# a2ensite example.com.conf

To activate the new configuration, you need to run:

user@server:/# systemctl reload apache2

Restart apache web server

user@server:/# service apache2 restart

List enabled sites

user@server:/# apache2ctl -S

Test domain

user@server:/# curl -I example.com

Check logs

user@server:/# cat /var/log/apache2/example.error.log

Rollback

user@server:/# a2dissite example.com.conf

user@server:/# rm /etc/apache2/sites-available/example.com.conf

Useful

user@server:/# service apache2 start

user@server:/# service apache2 stop

user@server:/# service apache2 restart

Site Address

http://127.0.0.1/example => http://example.com

* WordPress

Update project url

http://xxx.xxx.xxx.xxx/example/wp-admin/options-general.php

From: http://xxx.xxx.xxx.xxx/example

To: http://example.com

SSL

https://letsencrypt.org

user@server:/# apt-get install letsencrypt

Documetation

user@server:/# apt-get update
user@server:/# apt-get install software-properties-common
user@server:/# add-apt-repository universe
user@server:/# add-apt-repository ppa:certbot/certbot
user@server:/# apt-get update
user@server:/# apt-get install certbot python-certbot-apache

user@server:/# letsencrypt certonly -a webroot --webroot-path=/var/www/html/example -d example.com -d www.example.com

user@server:/# cd /etc/apache2/sites-available

https://stackoverflow.com/a/26717793/2732184

user@server:/etc/apache2/sites-available# vim example.com.conf

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin admin@example.com

    DocumentRoot "/var/www/html/example"
    <Directory "/var/www/html/example">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
    
    # Don't serve http. Redirect all traffic to https
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    
    ErrorLog ${APACHE_LOG_DIR}/example.error.log
    CustomLog ${APACHE_LOG_DIR}/example.access.log combined
</VirtualHost>
<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin admin@example.com

    DocumentRoot "/var/www/html/example"
    <Directory "/var/www/html/example">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
    
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    
    ErrorLog ${APACHE_LOG_DIR}/example.error.log
    CustomLog ${APACHE_LOG_DIR}/example.access.log combined
</VirtualHost>
/etc/apache2/sites-available# a2enmod rewrite
/etc/apache2/sites-available# a2ensite default-ssl
/etc/apache2/sites-available# a2enmod ssl
/etc/apache2/sites-available# systemctl reload apache2

* Wordpress

http://example.com/wp-admin/options-general.php

Change to https

From: http://example.com

To: https://example.com

RainLoop mail server

https://www.youtube.com/watch?v=pBmHkgnsP6Q

https://www.howtoforge.com/how-to-install-rainloop-webmail-on-ubuntu-1804/

https://www.linuxbabe.com/mail-server/install-rainloop-webmail-ubuntu-16-04

# apt-get install -y php7.2-curl

# cd /var/www
var/www# mkdir rainloop
var/www# cd rainloop
var/www/rainloop# curl -s http://repository.rainloop.net/installer.php | php
var/www/rainloop# chown -R www-data:www-data /var/www/rainloop
var/www/rainloop# chmod 755 -R /var/www/rainloop
var/www/rainloop# letsencrypt certonly -a webroot --webroot-path=/var/www/rainloop -d mail.example.com -d www.mail.example.com

var/www/rainloop# vim /etc/apache2/sites-available/rainloop.conf

<VirtualHost *:80>
    ServerName mail.example.com
    ServerAlias www.mail.example.com
    ServerAdmin admin@example.com

    DocumentRoot "/var/www/rainloop"
    
    <Directory "/var/www/rainloop">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
    
    <Directory  "/var/www/rainloop/data">
        Order allow,deny
        Deny from all
    </Directory>
    
    # # Don't serve http. Redirect all traffic to https
    # RewriteEngine On
    # RewriteCond %{HTTPS} off
    # RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    
    ErrorLog ${APACHE_LOG_DIR}/rainloop.error.log
    CustomLog ${APACHE_LOG_DIR}/rainloop.access.log combined
</VirtualHost>

<VirtualHost *:443>
    ServerName mail.example.com
    ServerAlias www.mail.example.com
    ServerAdmin admin@example.com

    DocumentRoot "/var/www/rainloop"
    
    <Directory "/var/www/rainloop">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
    
    <Directory  "/var/www/rainloop/data">
        Order allow,deny
        Deny from all
    </Directory>
    
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/mail.example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/mail.example.com/privkey.pem
    
    ErrorLog ${APACHE_LOG_DIR}/rainloop.error.log
    CustomLog ${APACHE_LOG_DIR}/rainloop.access.log combined
</VirtualHost>
var/www/rainloop# 

Enable site

# user@server:/# a2ensite /etc/apache2/sites-available/rainloop.conf

Restart apache web server

# user@server:/# service apache2 restart

Confirm enabled sites

# apache2ctl -S

Test domain

# user@server:/# curl -I mail.example.com

Troubleshooting...

Find current user

root@ubuntu-512mb-nyc3-01:/var/www/html# id

Add your user, usually root to www-data group

root@ubuntu-512mb-nyc3-01:/var/www/html# gpasswd -a "$USER" www-data

Confirm changes

root@ubuntu-512mb-nyc3-01:/var/www/html# id root

root@ubuntu-512mb-nyc3-01:/var/www/html# groups root

Update existing file ownership

root@ubuntu-512mb-nyc3-01:/var/www/html# chown -R "$USER":www-data /var/www

root@ubuntu-512mb-nyc3-01:/var/www/html# chown -R www-data:www-data /var/www

Update file permissions

root@ubuntu-512mb-nyc3-01:/var/www/html# chmod 764 -R /var/www

  • User (root): 7 rwx 111
  • Group (www-data): 6 rwx 110
  • Other: 4 rwx 100
@mtvbrianking
Copy link
Author

Troubleshooting MySQL

Error

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Solution

Try to restart mysql service.

service mysql restart

Source: https://stackoverflow.com/q/11657829/2732184

@mtvbrianking
Copy link
Author

@mtvbrianking
Copy link
Author

MYSQL default user (root)

Default user is root with no password.

https://stackoverflow.com/a/52335791

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