Skip to content

Instantly share code, notes, and snippets.

@HakamRaza
Last active May 19, 2022 06:44
Show Gist options
  • Save HakamRaza/c41a401eb5b90f448076f8c2c8355c44 to your computer and use it in GitHub Desktop.
Save HakamRaza/c41a401eb5b90f448076f8c2c8355c44 to your computer and use it in GitHub Desktop.
Basic - LAMP Stack Setup (ec2, php 8.1, Laravel 9)
/*
* Download .pem, connect SSH tunnel after set up Ubuntu instance.
* Open terminal and use following command
* This setup should more or less the same for any provider like DigitalOcean too.
$ ssh -i "your_pem.pem" ubuntu@<your public dns>.compute.amazonaws.com
/*
* the following command may need root privilage
* better create new user with root privilage to do commands
* enter "sudo su" or add "sudo" for individually to each command
*/
### Setup Apache, Setup Uncomplicated Firewall (UFW) ###
$ sudo apt update
$ sudo apt install apache2
$ sudo ufw allow "Apache Full"
/*
* Modify instance security group Inbound Rules to allow HTTP - Anywhere 0.0.0.0/0
* Check for server apache success setup by opening your aws server DNS/ip , should get Apache2 Welcome page
* also can get public ip set by use command below
*/
$ ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
(browser) http://<aws public DNS or IP>
########## Set Up MySQL ##########
$ sudo apt install mysql-server
/*
* setup mysql credentials
* setup mysql connection to use password (will be used to login mysql)
* if got stuck loop setting root password, skip this first by Ctrl+C at confirm password question.
* like this "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : ^C"
* DON'T miss the ";" at the end of every sql command
*/
$ sudo mysql_secure_installation
setup VALIDATE PASSWORD plugin? - y
three levels of password validation policy: 0 (since for test deploy)
New password: <follow the policy, if 0 then 8 char>
Re-enter new password: <follow the policy, if 0 then 8 char>
next others - y
$ sudo mysql
mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '<your mysql password previously >';
mysql> FLUSH PRIVILEGES;
/*
* repeat "sudo mysql_secure_installation" if skip previously
* Check root is using 'mysql_native_password plugin
*/
mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
mysql> exit
/*
* next time, to enter mysql, different command is use since using native plugin
*/
$ mysql -u root -p
########## Setup PHP (using PHP 8.1) ##########
$ sudo apt install php libapache2-mod-php php-mysql
$ sudo apt install php-mbstring php-xml php-bcmath
$ sudo apt-get install php8.1 libapache2-mod-php8.1 php8.1-common php8.1-gd php8.1-mysql php8.1-curl php8.1-intl php8.1-xsl php8.1-mbstring php8.1-zip php8.1-bcmath php8.1-soap php-xdebug php-imagick
/*
* Modify apache default serve files
*/
$ sudo nano /etc/apache2/mods-enabled/dir.conf
- move index.php to first position:
=================== dir.conf
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
===================
$ sudo systemctl restart apache2
/*
* each project will be serve inside folder in /var/www
* eg: in the example "myApp" laravel project will be use
*/
########## Install Composer ##########
$ sudo apt update
$ sudo apt install php-cli unzip
$ cd ~
$ curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
$ HASH=`curl -sS https://composer.github.io/installer.sig`
$ php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
$ composer
- should get composer help page
########## Create New Laravel Project ##########
$ cd /var/www/
$ sudo composer create-project --prefer-dist laravel/laravel myApp
$ sudo chown -R www-data.www-data /var/www/myApp/storage
$ sudo chown -R www-data.www-data /var/www/myApp/bootstrap/cache
$ sudo chmod -R 755 /var/www/myApp
########## OR git Clone Existing Project ##########
$ cd /var/www/
$ sudo git clone <github repo link>
- username: your username
- paswword: key in profile Auth token generated
$ composer install
########## Setup Schema Database to Use for Laravel ##########
$ mysql -u root -p
- key in password root
mysql> CREATE DATABASE myApp_db;
mysql> SHOW DATABASES;
mysql> exit
- you can also set up different login user instead of root for login mysql
########## Setup Database ENV Laravel ##########
$ sudo nano /var/www/myApp/.env
APP_URL=http://localhostcls
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myApp_db
DB_USERNAME=root
DB_PASSWORD=<root user password>
########## Deploy to Apache (Virtual Host) ##########
$ sudo nano /etc/apache2/sites-available/myApp.conf
- change to: (refer to Sites-Available Sample Config File below)
- make sure root is'/var/www/myApp/public;'
$ sudo a2ensite myApp.conf
$ sudo a2dissite 000-default.conf
- unlink default server block
$ sudo apache2ctl configtest
$ sudo systemctl restart apache2
- (browser) open http://<aws public DNS or IP>
- should get Laravel Start Page
########################################################################################## END
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName myApp
ServerAlias www.example.com
DocumentRoot /var/www/myApp/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
######### Ubuntu Console ##########
// up one level
$ cd ..
// go to home directory
$ cd ~
// go to root
$ cd /
// check ubuntu version
$ lsb_release -a
// list php module
$ apt search php- | less
// check module details
$ apt show <package_name>
// install module
$ sudo apt install <package_name> <package_name2> ...
// reload apache
$ sudo systemctl restart apache2
// check apache status
$ sudo systemctl status apache2
// check apache error log
$ sudo tail -100 /var/log/apache2/error.log
// load file read-only
$ cat fileName
// remove file
$ rm fileName
// remove directory
$ rm -rf dirName
// update packages
$ sudo apt update
// mysql login root (using native)
$ mysql -u root -p
// mysql login root (using socket)
$ mysql
#######// Laravel (inside project folder) ##########
// clear config cache
$ php artisan config:clear
// clear route cache, if change route files
$ php artisan route:clear
// clear cache
$ php artisan cache:clear
######### MySQL Console ##########
// exit
mysql> exit
// create new database
mysql> CREATE DATABASE new_database_name;
// list all database
mysql> SHOW DATABASES;