# Magento 2 Setup on Ubuntu 18.04 (Debian)
## User Permissions
In `<magento_rootdir>`
`sudo usermod -a -G www-data <your Linux username>`
`sudo chown -Rf :www-data .`
Log off then back on
## Install PHP and Mods
`sudo apt-get install php7.4-common php7.4-cli php7.4-fpm php7.4-opcache php7.4-gd`
`sudo apt-get install php7.4-mysql php7.4-curl php7.4-xml php7.4-mbstring php7.4-zip`
`sudo apt-get install php7.4-bcmath php7.4-soap php-pear php-dev php-xmlrpc php7.4-intl`
## Install Apache and Mods
`sudo apt-get install apache2`
- test by going to http://localhost in browser
`sudo apt-get install libapache2-mod-php7.3`
- Edit php.ini file (`/etc/php/7.3/apache/php.ini`)
- Turn `display_errors=off` to `on`
`sudo a2enmod rewrite`
`sudo systemctl restart apache2`
## Install MySQL and Mods
`sudo apt-get install mysql-server`
## Install Dev Tools
### Git
`sudo apt-get install git`
### XDebug
`sudo apt-get install php-xdebug`
- run `php -m` to check module is installed
Edit the config file for XDebug
`sudo nano /etc/php/7.3/mods-available/xdebug.ini`
`zend_extension=xdebug.so`
`xdebug.remote_autostart = 1`
`xdebug.remote_enable = 1`
`xdebug.remote_handler = dbgp`
`xdebug.remote_host = 127.0.0.1`
`xdebug.remote_log = /tmp/xdebug_remote.log`
`xdebug.remote_mode = req`
`xdebug.remote_port = 9000`
`sudo systemctl restart apache2`
### Composer
`php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"`
`php -r "if (hash_file('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"`
`https://getcomposer.org/download/` (get hash, second line)
`php composer-setup.php`
`sudo mv composer.phar /usr/local/bin/composer`
### Redis
`sudo apt-get install redis-server`
`sudo systemctl enable redis-server.service`
### Memcached
sudo apt-get install memcached
### Varnish
sudo apt-get install varnish
### Postman
sudo snap install postman
### PHPUnit
`sudo apt-get phpunit`
### PHPStorm
<instructions forthcoming>
## Apache Site Setup
You must set up a virtual host for each website you are working on.
### Create Virtual Host (VHost)
`sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/<your-magentosite-name>.conf`
`sudo nano /etc/apache2/sites-available/<your-magentosite-name>.conf`
#### Change file
You may either replace the text in the file with the following text or edit it to match, but it must look like this.
```
<VirtualHost *:80>
ServerName <your-magentosite-name>.<whatever> #this is the name you type in the browser
ServerAlias <your-magentosite-name>.<whatever>
ServerAdmin webmaster@example.com
DocumentRoot /var/www/html/<your-magentosite-name>/pub
<Directory /var/www/html/<your-magentosite-name>
Options -Indexes +FollowSymLinks
Require all granted
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
```
#### Host File
`sudo nano /etc/hosts`
Add the name of the website you want to this line. This is what you will enter into your browser to access your local installation. It should be the same as `ServerAlias` in the previous step.
`#You can make a comment line like this`
`127.0.1.1 <your-magentosite-name>.<whatever>`
As a reference, I always use `.local` on my local environments so that I can easily tell that I'm on my local environment.
- Example `mysite.local` becomes `http://mysite.local`
## MySQL Setup
### Create MySQL user
`sudo mysql -u root`
`CREATE USER ‘magento’@’localhost’ IDENTIFIED BY ‘TestMe!’;`
- `‘magento’@’localhost’` is arbitrary, can pick your own name and password
### Create Site DB
`CREATE DATABASE sitedb;`
- name your database after your project for ease of use
- Example: `CREATE DATABASE kroger`
#### Give access to MySQL user to DB
`GRANT ALL PRIVILEGES ON sitedb.* TO ‘magento’@’localhost’;`
`FLUSH PRIVILEGES;`
### Exit MySQL
`Exit;`
## Magento Pull and Install
### Composer Code Pull
From inside `<magento_rootdir>`
`composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition . (or <installdirname>)`
## Magento Install
Fill in appropriate variables for your website
```
php bin/magento setup:install --admin-user=admin --admin-firstname=Magento --admin-lastname=Admin --admin-email=admin@admin.com --admin-password=admin123 --backend-frontname=admin --db-user=magento --db-host=localhost --db-password=Testing0! --db-name=sitename --base-url=http://sitename.local --currency=USD --timezone=America/Chicago --language=en_US --use-rewrites=1 --use-sample-data
```
```
php bin/magento setup:upgrade
```
```
php bin/magento setup:di:compile
```
```
php bin/magento cache:clean
```
```
php bin/magento cache:flush
```
## Post-install permissions
In `<magento_rootdir>`
```
sudo find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
sudo find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
sudo find . -type f -name “*.php” -exec chmod 600 {} +
sudo chown -R :www-data .
sudo chmod u+x bin/magento
```