├── .dockerignore ├── Dockerfile ├── README.md ├── conf └── nginx │ └── nginx-site.conf └── scripts └── 00-laravel-deploy.sh /.dockerignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /public/hot 3 | /public/storage 4 | /storage/*.key 5 | /vendor 6 | .env 7 | .phpunit.result.cache 8 | Homestead.json 9 | Homestead.yaml 10 | npm-debug.log 11 | yarn-error.log -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM richarvey/nginx-php-fpm:latest 2 | 3 | COPY . . 4 | 5 | # Image config 6 | ENV SKIP_COMPOSER 1 7 | ENV WEBROOT /var/www/html/public 8 | ENV PHP_ERRORS_STDERR 1 9 | ENV RUN_SCRIPTS 1 10 | ENV REAL_IP_HEADER 1 11 | 12 | # Laravel config 13 | ENV APP_ENV production 14 | ENV APP_DEBUG false 15 | ENV LOG_CHANNEL stderr 16 | 17 | # Allow composer to run as root 18 | ENV COMPOSER_ALLOW_SUPERUSER 1 19 | 20 | CMD ["/start.sh"] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-render-template 2 | 3 | Do you want to host your Laravel application for FREE and stress free? This guide will take you step by step until your application is hosted on render.com. 4 | 5 | Now, let's get started. 6 | 7 | ## 1. Set up an account on render.com 8 | 9 | You need to [click to sign up](https://dashboard.render.com/login) on Render. It is preferable to sign up with GitHub since you will be using GitHub in the hosting process. 10 | 11 | ## 2. Force Laravel to accept only https 12 | 13 | To force Laravel to allow https by default, make sure you `App/Provider/AppServiceProvider.php` contains the line and block of code below: 14 | 15 | ```php 16 | public function boot() 17 | { 18 | if (env('APP_ENV') == 'production') { 19 | $this->app['request']->server->set('HTTPS', true); 20 | } 21 | } 22 | ``` 23 | 24 | ## 3. Create a remote repository with laravel-render-template. 25 | 26 | Here, you will use `Laravel-render-template`. So [click to create](https://github.com/codingnninja/Laravel-render-template-host) a remote repo. 27 | 28 | ![Laravel render template](https://res.cloudinary.com/nyscapp/image/upload/v1686174427/create_laravel-render_template_hpcqts.png) 29 | 30 | You need to click on `use this template` and then `create a new repository`. Fill your repository name and you're done. 31 | 32 | ## 4. Link local and remote repository. 33 | 34 | It is time to merge the local and remote repo and push everything to the remote repo. In this case, you need to choose the option that fit your need. 35 | 36 | ### 4.1 Using a new Laravel project. 37 | 38 | Do you still remember the remote repository you created? Well, you will know what to do about it soon. 39 | 40 | Run the following commands line by line: 41 | 42 | ```sh 43 | git init 44 | git add . 45 | git commit -m "first commit" 46 | git branch -M main 47 | ``` 48 | 49 | ![Github url](https://res.cloudinary.com/nyscapp/image/upload/v1686174426/laravel_render_repo_url_sztour.png) 50 | 51 | Yeah, it is time to use the url of the repository you created and you can copy it like in the image above. Use it as a replacement for the `url` in the first command below: 52 | 53 | ```sh 54 | git remote add origin https://github.com/OWNER/REPOSITORY.git 55 | git pull && git push -u origin main 56 | ``` 57 | ### 4.2 Using an existing project with no remote repo. 58 | 59 | If you're working on an existing project you want to host, you need the commands below: 60 | 61 | ```sh 62 | git remote add origin https://github.com/OWNER/REPOSITORY.git 63 | git branch -M main 64 | git pull && git push -u origin main 65 | ``` 66 | You need to replace the `url` above with the one you copied from your remote repository. 67 | 68 | ### 4.3 Using an existing project that is already connected to a remote repo. 69 | 70 | To do this, you need to reset the remote `url` in your local repository by running the command below: 71 | 72 | ```sh 73 | git remote set-url origin https://github.com/OWNER/REPOSITORY.git 74 | git remote -v 75 | git add . 76 | git commit -m "first commit" 77 | git pull && git push 78 | ``` 79 | 80 | ## 5. Setting up host web service on render 81 | [Click to create](https://dashboard.render.com/select-repo?type=web) a new web service on Render. Then you will be prompted on what to do next. You see Github and Gitlab logos, and `connect account` by the right-hand side of your screen. 82 | 83 | Click `connect account` under the Github logo to link your Github account. Choose the repository you want to host after setting up the web service. The image below gives you an insight. 84 | 85 | ![Render web service section](https://res.cloudinary.com/nyscapp/image/upload/v1686211207/render_create_webserive_w4lh78.png) 86 | ## 6. Setting up Enviroment variables 87 | 88 | > Make sure your Laravel application is already working. 89 | 90 | Here, we will add `environment` variables to the render web service you created the other time. Navigate to the `dashboard` and you will see all the services you have created just like in the image below: 91 | 92 | ![Dashboard of a render account](https://res.cloudinary.com/nyscapp/image/upload/v1686211207/dashboard_seriveces_cclxzh.png) 93 | 94 | Then, click on the web service you want to host on and navigate to `environment`. You should see a view that looks like below: 95 | 96 | ![Render enviroment section](https://res.cloudinary.com/nyscapp/image/upload/v1686211206/laravel_render_environment_variable_chirww.png) 97 | 98 | Now, add all the `environment` variables your application depends on one by one. Let's start with `APP_KEY`. 99 | 100 | ### Generate application key. 101 | 102 | Run the code below in the root directory of your Laravel project and copy its output. 103 | 104 | ```sh 105 | php artisan key:generate --show 106 | ``` 107 | 108 | Add `APP_KEY` as the key and the generated string as its value. 109 | ## 7. Setup a database (PostgreSQL & MySQL) 110 | 111 | To use MySQL on render, you need to be a paid user so we're using PostgreSQL in this write up. [Create](https://dashboard.render.com/new/database) PostgreSQL database service on render. 112 | 113 | ![Create new PostgreSQL](https://res.cloudinary.com/nyscapp/image/upload/v1686174426/Laravel_render_postgresql_h5ynsn.png) 114 | 115 | Make sure you enter your preferred `name`, `database name`, `user name` and `region`. The rest are optional. So look for `create database` button at the bottom of the page to complete the setup. 116 | 117 | Remenber, you can use an external MySQL or ProgreSQL services. You need to follow similar process to set it up. 118 | 119 | Note: Make sure the name of your database include something like `db` to differentiate the services on your dashboard but that is up to you. 120 | 121 | ### Adding database environment variables. 122 | 123 | When you finished setting up PostreSQL above, you should have copied `Hostname`, `Port`, `Database`, `Username`, `Password` because you need to add them to the `enviroment` as variables. 124 | 125 | ![Dashboard of a render account](https://res.cloudinary.com/nyscapp/image/upload/v1686211207/dashboard_seriveces_cclxzh.png) 126 | 127 | If you forgot to copy them, you can go back to your dashboard and select your database to copy the information. 128 | 129 | ![Database connection information](https://res.cloudinary.com/nyscapp/image/upload/v1686212167/database_render_info_hclqgy.png) 130 | 131 | 132 | | Key | Value | 133 | | -------- | ------- | 134 | | DB_CONNECTION| pgsql | 135 | | Port | 5432 | 136 | | Hostname | your hostname| 137 | | Database | your dbname| 138 | | Username | your username| 139 | | Password | your password| 140 | 141 | The keys and values above show how to add `environment` variables on render.com and the image below also gives some insight. 142 | 143 | ![Render enviroment section](https://res.cloudinary.com/nyscapp/image/upload/v1686211206/laravel_render_environment_variable_chirww.png) 144 | 145 | Now, your application should be working correctly. And don't forget, it is important to keep this your application repository private for security and other reasons. 146 | 147 | ## Content of Laravel-render-template 148 | 149 | It contains the following files and folders: 150 | 151 | 1. `Dockerfile` contains the information to setup our a serve environment. 152 | 2. `scripts/00-laravel-deploy.sh` contains all necessary information to install Laravel and its dependencies. Below is its content: 153 | 154 | ```sh 155 | #!/usr/bin/env bash 156 | echo "Running composer" 157 | composer install --no-dev --working-dir=/var/www/html 158 | 159 | echo "Caching config..." 160 | php artisan config:cache 161 | 162 | echo "Caching routes..." 163 | php artisan route:cache 164 | 165 | echo "Running migrations..." 166 | php artisan migrate --force 167 | ``` 168 | 169 | We add the above commands because they're the default to install Laravel, dependencies, and run its operations. If you run a certain command after installing a package before it works on your machine, you also need to run the same here. 170 | 171 | For example, Laravel-cloudinary package requires it users to publish it to work on their machine. So, if you use Laravel-cloudinary, you also need the command for the serve too. Then, you need to add the command below to the above commands: 172 | 173 | ```sh 174 | echo "Publishing cloudinary provider..." 175 | php artisan vendor:publish --provider="CloudinaryLabs\CloudinaryLaravel\CloudinaryServiceProvider" --tag="cloudinary-laravel-config" 176 | ``` 177 | 178 | 3. `conf/nginx-site.conf` contains nginx (server) configuration. 179 | 4. `.dockerignore` contains contains the files you don't want to ship with Docker. 180 | ## Troubleshooting 181 | 182 | > 1. Always check the logs on the server. 183 | 184 | > 2. Sometimes, you might be running packages that need update which may affect the application to work properly. So, make it an habit to run `composer update` once in a while. 185 | 186 | > 3. If your database can't connect despite adding all necessary details correctly, then you can hardcode the database connection information into your laravel project. 187 | 188 | Navigate to `config/database.php` to change your default database connection to pgsql. 189 | 190 | ```sh 191 | 'default' => env('DB_CONNECTION', 'pgsql') 192 | ``` 193 | 194 | And add database connection details below: 195 | 196 | ```php 197 | 'pgsql' => [ 198 | 'driver' => 'pgsql', 199 | 'url' => env('DATABASE_URL'), 200 | 'host' => env('DB_HOST', 'Hostname'), 201 | 'port' => env('DB_PORT', '5432'), 202 | 'database' => env('DB_DATABASE', 'dbname'), 203 | 'username' => env('DB_USERNAME', 'username'), 204 | 'password' => env('DB_PASSWORD', '********'), 205 | 'charset' => 'utf8', 206 | 'prefix' => '', 207 | 'prefix_indexes' => true, 208 | 'schema' => 'public', 209 | 'sslmode' => 'prefer', 210 | ] 211 | ``` 212 | 213 | > 4. Click to visit [render docs for Laravel]() 214 | ## Need help? 215 | 216 | Chat me up on Twitter via [Ayobami Ogundiran](https://twitter.com/codingnninja) 217 | 218 | 219 | 220 | -------------------------------------------------------------------------------- /conf/nginx/nginx-site.conf: -------------------------------------------------------------------------------- 1 | server { 2 | # Render provisions and terminates SSL 3 | listen 80; 4 | 5 | # Make site accessible from http://localhost/ 6 | server_name _; 7 | 8 | root /var/www/html/public; 9 | index index.html index.htm index.php; 10 | 11 | # Disable sendfile as per https://docs.vagrantup.com/v2/synced-folders/virtualbox.html 12 | sendfile off; 13 | 14 | # Add stdout logging 15 | error_log /dev/stdout info; 16 | access_log /dev/stdout; 17 | 18 | # block access to sensitive information about git 19 | location /.git { 20 | deny all; 21 | return 403; 22 | } 23 | 24 | add_header X-Frame-Options "SAMEORIGIN"; 25 | add_header X-XSS-Protection "1; mode=block"; 26 | add_header X-Content-Type-Options "nosniff"; 27 | 28 | charset utf-8; 29 | 30 | location / { 31 | try_files $uri $uri/ /index.php?$query_string; 32 | } 33 | 34 | location = /favicon.ico { access_log off; log_not_found off; } 35 | location = /robots.txt { access_log off; log_not_found off; } 36 | 37 | error_page 404 /index.php; 38 | 39 | location ~* \.(jpg|jpeg|gif|png|css|js|ico|webp|tiff|ttf|svg)$ { 40 | expires 5d; 41 | } 42 | 43 | location ~ \.php$ { 44 | fastcgi_split_path_info ^(.+\.php)(/.+)$; 45 | fastcgi_pass unix:/var/run/php-fpm.sock; 46 | fastcgi_index index.php; 47 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 48 | fastcgi_param SCRIPT_NAME $fastcgi_script_name; 49 | include fastcgi_params; 50 | } 51 | 52 | # deny access to . files 53 | location ~ /\. { 54 | log_not_found off; 55 | deny all; 56 | } 57 | 58 | location ~ /\.(?!well-known).* { 59 | deny all; 60 | } 61 | } -------------------------------------------------------------------------------- /scripts/00-laravel-deploy.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | echo "Running composer" 3 | composer install --no-dev --working-dir=/var/www/html 4 | 5 | echo "Caching config..." 6 | php artisan config:cache 7 | 8 | echo "Caching routes..." 9 | php artisan route:cache 10 | 11 | echo "Running migrations..." 12 | php artisan migrate --force 13 | 14 | echo "Publishing cloudinary provider..." 15 | php artisan vendor:publish --provider="CloudinaryLabs\CloudinaryLaravel\CloudinaryServiceProvider" --tag="cloudinary-laravel-config" 16 | --------------------------------------------------------------------------------