Setup_laravel_server.md 4.73 KB
Newer Older
Shengjie Yang's avatar
Shengjie Yang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Setup Laravel App on Server

* Laravel app was developed on local and pushed to Git. We were going to deploy it on an Ubuntu 18.04 server where PHP, Apache, MySQL are already setup.
---

### 1. Clone Laravel Repo

Login the server and clone a repository to ~/public_html (It's not nesessary to store into /var/www/html)


```
git clone https://git.biohpc.swmed.edu/QBRC/web-development/data-manager.git datamanager

cd datamanager/
cp .env.example .env
Shengjie Yang's avatar
Shengjie Yang committed
16
// Create a new database in MySQL and configure the mysql parameters in .env
Shengjie Yang's avatar
Shengjie Yang committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
```

### 2. Install Composer

Laravel gitignore the vendor directory by default and we need composert to install the packages in vendor. You can skip this step if composer is already installed.

Run `curl -sS https://getcomposer.org/installer -o composer-setup.php` to pull the composer installer. If it returns error like below,

>  curl: (35) error:1408F10B:SSL routines:ssl3_get_record:wrong version number

**Solution:**

```
vi /etc/environment`
// Change https_proxy="https://xxxxx" to https_proxy="http://xxxxx" and then reboot server

sudo su
sudo reboot
```

Login back to server and locate at where the composer installer stored. Then run following script, (check https://getcomposer.org/download/ for latest script)

```
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

sudo mv composer.phar /usr/local/bin/composer
```

### 3. Install packages in vendor of Laravel

Go to the directory of Laravel app and run

```
composer install

// if it alerts to execute composer update, just do it
composer update
```

In my case, it returns errors in prompt

> Problem 1
>
>    \- Root composer.json requires php ^7.3|^8.0 but your php version (7.2.24) does not satisfy that requirement.
>
> Problem 2
>
>    \- laravel/framework[v8.40.0, ..., 8.x-dev] require php ^7.3|^8.0 -> your php version (7.2.24) does not satisfy that requirement.
>
>    \- Root composer.json requires laravel/framework ^8.40 -> satisfiable by laravel/framework[v8.40.0, ..., 8.x-dev].

Shengjie Yang's avatar
Shengjie Yang committed
70
**Solution:** Upgrade PHP from 7.2 to 7.4 (Whatever version php>=7.3)
Shengjie Yang's avatar
Shengjie Yang committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

```
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

# Install PHP 7.4 core
sudo apt install php7.4 php7.4-common php7.4-cli

# PHP 7.4 extensions
sudo apt install php7.4-bcmath php7.4-bz2 php7.4-curl php7.4-intl php7.4-mbstring php7.4-mysql php7.4-readline php7.4-xml php7.4-zip

# Web server integration
sudo apt install php7.4-fpm
sudo a2enconf php7.4-fpm
sudo apt install libapache2-mod-php7.4

# Restart apache to reload
sudo systemctl reload apache2

# php -v shows PHP 7.4 and phpinfo() shows PHP 7.2. To enable php 7.4 in phpinfo()
sudo a2dismod php7.2
sudo a2enmod php7.4

sudo service apache2 restart
```

Then rerun `composer update` / `composer install` under Laravel app directory.

### 4. Setup Virtual Host in Apache2

Shengjie Yang's avatar
Shengjie Yang committed
101
102
Since DocumentRoot doesn't point to /var/www/html and port 80 is occupied, I setup another directory and specify port 8080 for deploying my sites.

Shengjie Yang's avatar
Shengjie Yang committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
```
sudo mv /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/laravel.com.conf

vi laravel.com.conf
// Insert following content
----------------------------
<VirtualHost *:8080>
    DocumentRoot /home/shengjie/public_html/

    Alias /datamanager /home/shengjie/public_html/datamanager/public

    <Directory "/home/shengjie/public_html">
       Options All
       AllowOverride All
       Allow from all
    </Directory>

    <Directory /home/shengjie/public_html/datamanager>
       Options Indexes FollowSymLinks
       AllowOverride All
       Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/shengjie_error.log
    CustomLog ${APACHE_LOG_DIR}/shengjie_access.log combined
</VirtualHost>
----------------------------

sudo a2ensite laravel.com.conf

vi /etc/apache2/ports.conf
// Add a line: Listen   8080

Shengjie Yang's avatar
Shengjie Yang committed
136
137
sudo service apache2 restart

Shengjie Yang's avatar
Shengjie Yang committed
138
139
140
141
142
143
144
145
146
147
```

### 5. Initial Laravel App

```
vi LARAVE_PATH/public/.htaccess
// Add a line: RewriteBase /datamanager/

php artisan key:generate

Shengjie Yang's avatar
Shengjie Yang committed
148
149
150
151
152
153
154
155
156
/**
* For some reasons, like image viewer, we would 
* like to store those large files in storage/ to 
* avoid deployment (by Git) problem. So we create 
* a symbolic link to access it.
**/

php artisan storage:link

Shengjie Yang's avatar
Shengjie Yang committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
php artisan cache:clear
php artisan view:clear
php artisan config:clear
php artisan route:clear

php artisan migrate:refresh --seed

chmod -R 775 storage
```

Access `http://129.112.152.121:8080/datamanager/` to visit and find only root route "/" works and other routes return NOT FOUND

**Solution:**

```
sudo a2enmod rewrite
sudo service apache2 restart
```