问题
I'm using symfony4. Everything works in dev. But there is less tutorial for prod. How to deploy tutorial provides only less info (No info about shared hosting)
Index.php prevents .env from reading the environment variables. Thats fine. But in Symfony2.x we can switch between env by app.php and app_dev.php front controllers. But in Symfony4 There is no such files only one index.php and an .env file
According to the deploy link .
How you set environment variables, depends on your setup: they can be set at the command line, in your Nginx configuration, or via other methods provided by your hosting service.
I'm using shared hosting, so the provider does not allow to change environment variables. If it is in my file, I can use it good.
Currently to overcome this problem, what i'm doing is,
$_SERVER['APP_ENV'] = "prod";
$_SERVER['APP_SECRET'] = "79cfa09223f91b1a195134019e0b17ac";
I added these two line at the beginning of index.php and bin/console file. I think this is not the correct way to do.
Suggest me hot to deploy symfony4 (Even testing prod environment)
回答1:
You need the .env
file that is on the server to have APP_ENV=prod
The .env
file is for the specific machine, it is ignored by git, while .env.dist
is tracked. So edit the .env.dist and commit it.
Put your project in the same dir as public_html as a sibling. Then remove the public_html dir and recreate it as a symlink pointing to your project public dir.
Rename .env.dist
to .env
.
Run composer require symfony/apache-pack
it will create the .htaccess file and you should be good.
Worked for me on A2hosted server.
Update 2019-06-28
Just deployed to Bluehost, they finally upgraded to PHP 7. I deployed into a sub-domain using the same process as above except one change. I had to add the following line to the public/.htaccess file generated by apache-pack in order to enable PHP 7.1
AddHandler application/x-httpd-ea-php71 .php
回答2:
I'm using apache server for production (Shared hosting). The easy thing is edit .htaccess file.
Add this at the bottom of .htaccess file
# ENVIRONMENT VARIABLES
SetEnv APP_ENV prod
SetEnv APP_SECRET 79cfa09223f91b1a195134019e0b17ac
This will solve the question.
These are the things I faced personaly, so to help someone,
And sometimes symfony4 throws error on php7.2 servers. To solve this, in .htaccess file add the following code,
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php72” package as the default “PHP” programming language.
<IfModule mime_module>
AddType application/x-httpd-ea-php72 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
This code automatically generated by my server. I think someone needs this.
At first I tried to deploy my app without .htaccess file. But this throws error, no output in production server. I tried to switch php and finally i saw the above code generated by server itself and later I added the setenv to work with symfony4.
来源:https://stackoverflow.com/questions/47949952/symfony4-deploy-to-shared-hosting