How to automate deployment of ngnix configuration on Amazon AWS? - php

i have a created project using symfony 5.
For hosting website i am using amazon AWS.
Amazon AWS instance is using nginx.
Everytime i deploy code using Elastic beanstalk i have to add following lines to the etc/nginx/nginx.conf file
location / {
try_files $uri $uri/ /index.php?$args;
}
if i don't add following configuration and don't restart nginx server then only page of my website is visible . Whenever i try to open another page of website excepts homepage i get following error :
404 Not Found
nginx/1.18.0
How can i automate deployment of the ngnix configuration whenever i upload code using elasticbeanstalk ?

If you want to modify your nginx configuration, you should modify your Configuration Files by extending ElasticBeanstalk as explained here
I will assume you are using Amazon Linux 2, if not the ElasticBeanstalk is working completely differently as documented here.
Elastic Beanstalk (using Amazon Linux 2) will automatically look for a .platform directory at the root of your zip file.
By adding .platform/nginx/conf.d/elasticbeanstalk/php-custom.conf with the following content, you should be good to go (assuming you are using the default configuration). Note that you could overwrite the nginx.conf by adding this file .platform/nginx/nginx.conf to your project, but since what you want to do is fairly simple, I would only add a file that will be automatically loaded by nginx.
location / {
try_files $uri $uri/ /index.php?$args;
}
So your project tree should look something like this.
project root
├── .platform
│   └── nginx
│   └── conf.d
│   └── elasticbeanstalk
│   └── php-custom.conf
└── your project files

Related

AWS Elastic Beanstalk Apply configuration file on nginx server

I am trying to migrated my application in ebs because it was deprecated, my previous platform was:
PHP 5.6 running on 64bit Amazon Linux / 2.9.8
now i'm migrating to:
PHP 7.4 running on 64bit Amazon Linux 2 / 3.0.3
This platform uses the Nginx server instead of Apache.
I was able to deploy my application but the problem is that my .htaccess configuration file was deprecated as was my .ebextensions configuration file. so I have lost the settings I had for example to get clean urls and redirects from http to https.
I have tried to transform the apache configurations to Nginx by putting the respective configuration file but apparently they have no effect.
I have tried many test configurations and I have come to the conclusion that the platform is not reading the configurations that I put.
I have tried to try for example with this little configuration, which should allow me to list the "views" directory:
cleanurl.config
server {
location / views / {
autoindex on;
}
}
I have tried putting it in the following folders.
".ebextensions / cleanurl.config"
files:
"/etc/nginx/conf.d/cleanurl.conf":
mode: "000644"
owner: root
group: root
content: |
server {
location / views / {
autoindex on;
}
}
".platform / nginx / conf.d / cleanurl.config"
server {
location / views / {
autoindex on;
}
}
But I don't get results. I always get 403 Forbidden when I point to a folder. Please help.
Thanks in advance
After trying and trying I was able to solve it in the following way:
I put the file in this location
.platform/nginx/conf.d/elasticbeanstalk/cleanurl.conf
change my config file like this
location/views/ {
autoindex on;
}
and now everything works.
I have tried many test configurations and I have come to the conclusion that the platform is not reading the configurations that I put.
In the first case this is because the configuration files that you use are for Amazon Linux 1 (AL1). However, you environment is PHP 7.4 running on 64bit Amazon Linux 2 (AL2).
In your second attempt, you are using the config files in .platform/nginx/conf.d/ as you should. However, you are using *.config extension. This wrong extension and it should be:
cleanurl.conf
You still may have other issues, but the wrong extension could explain why the files are being ignored.

Nginx + magento 2 multisite setup with sub directory

I have Magneto 2 installed on the server and the main site is working with www.example.com and loading Magneto 2 root directory. Now have created sub-folder called "ksa" inside Magento root directory.
I need to load main site with "ksa"
For example
example.com/test.php to example.com/ksa/test.php
My nginx configuration
location ~ /ksa/ {
rewrite ^/ksa/$ http://example.com/ permanent;
}

php-fpm : disabling a php extension for a specific pool

I have configured a new FPM pool (config file www.conf in pool.d), now let's say I want to disable mysql support for that specific pool ? Or maybe there is a way not to load that extension for that pool ?
I'm using debian 9 and here is how the file structure looks like this:
/etc/php/7.0/fpm$ tree
.
├── conf.d
│ ├── 10-mysqlnd.ini -> /etc/php/7.0/mods-available/mysqlnd.ini
│ ├── 10-opcache.ini -> /etc/php/7.0/mods-available/opcache.ini
│ ├── 10-pdo.ini -> /etc/php/7.0/mods-available/pdo.ini
│ ├── 20-calendar.ini -> /etc/php/7.0/mods-available/calendar.ini
│ ├── 20-ctype.ini -> /etc/php/7.0/mods-available/ctype.ini.ini
(...)
├── php-fpm.conf
├── php.ini
└── pool.d
├── forgewww.conf
└── www.conf
extension=thing.so can be found in files conf.d/<extension>.ini. Problem is all those extensions once configured seem common to all pools...
I've also tried to disable mysql extensions globally with phpdismod then append a line in www.conf with:
php_admin_value[extension] = mysqli.so
Which doesn't seems to work (the above doesn't enable mysqli for that pool)
You can't load different extension per-pool, the extensions are defined in an .ini file, loaded by master process. php-fpm master process forks into children, meaning that they share what's been loaded by the master process. You can't have a child load a different set of extensions after forking, or unload them. It probably is possible to develop a solution, but there's really no need for that seeing that you can solve your problem via different approach.
In order to achieve what you're after, simply set up entirely different php-fpm process on a different port / unix socket and load extensions you require, set up your pools and you're done. It's actually a lot less work than it sounds, it probably won't take you more than a few minutes.

How to change root directory for PHP azure website?

I have created a PHP website on azure using app services. I use continuous deployment through bitbucket. I need to point the website to public folder in my code to run the app as it is built with zend framework.
After some search, was not able to find how to change the folder where the server points for default directory.
Go to Azure Web apps settings -> Application Settings -> Virtual Applications and directories and setup the physical path of the new folder. Also check the Application checkbox.
Restart the web app once.
There are a few scenarios possible:
You run a Windows App Service
You run a Linux App Service with PHP 7.4 or less
You run a Linux App Service with PHP 8
In the first scenario (Windows App Service) you can go to the App Service > Settings > Configuration blade you need to select the tab "Path Mappings" where you can set the Virtual Applications paths as follows: "/" maps to "site\wwwroot\public".
In the second scenario you can use the .htaccess solution described by #Ed Greenberg, even though for Zend Framework I suggest to use the following settings:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
For the third scenario you have a bit more of a challenge since Apache was replaced by Nginx and the rewrite rules no longer apply. Please see my detailed blog article "PHP 8 on Azure App Service" on how to solve this and other challenges with the new Azure App Service for PHP 8.
Good luck and let me know if it solved your problem.
For PHP 8.0 with nginx I use startup.sh script placed in the root directory of the project. startup.sh contains the following line:
sed -i 's/\/home\/site\/wwwroot/\/home\/site\/wwwroot\/public/g' /etc/nginx/sites-available/default && service nginx reload
You need to add "startup.sh" as Startup Command in General Settings. Now "public" dir is your root directory.
The correct answer in 2021 (for Laravel, and probably other frameworks with a /public directory) is to put an extra .htaccess in the webroot directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Credit to Azure Web App - Linux/Laravel : Point domain to folder
Finally I've found Laravel documentation how to make it work with Azure. To be more precise - PHP8 + NGINX. Here is the article link - https://azureossd.github.io/2022/04/22/PHP-Laravel-deploy-on-App-Service-Linux-copy/index.html
Hope it will be useful :-)
PHP 8 (NGINX)
PHP 8 on Azure App Service Linux use NGINX as the Web Server. To have NGINX route requests to /public we’ll have to configure a custom startup script. We can grab the existing default.conf under /etc/nginx/sites-available/default.conf and run cp /etc/nginx/sites-available/default.conf /home. This will copy the default.conf we need into /home so we can download it with an FTP client or any other tool that allows this.
This default.conf has the following line:
root /home/site/wwwroot;
We need to change it to the following:
root /home/site/wwwroot/public;
Next, under the location block we need to change it from:
location / {
index index.php index.html index.htm hostingstart.html;
}
to the following:
location / {
index index.php index.html index.htm hostingstart.html;
try_files $uri $uri/ /index.php?$args;
}
Now configure your actual startup.sh bash script. Note, the file name is arbitrary as long as it is a Bash (.sh) script. Configure the file along the lines of the below:
#!/bin/bash
echo "Copying custom default.conf over to /etc/nginx/sites-available/default.conf"
NGINX_CONF=/home/default.conf
if [ -f "$NGINX_CONF" ]; then
cp /home/default.conf /etc/nginx/sites-available/default
service nginx reload
else
echo "File does not exist, skipping cp."
fi
NOTE: $query_string can be used as well. See the official documentation here.
Our custom default.conf should look like the below:
server {
#proxy_cache cache;
#proxy_cache_valid 200 1s;
listen 8080;
listen [::]:8080;
root /home/site/wwwroot/public;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
index index.php index.html index.htm hostingstart.html;
try_files $uri $uri/ /index.php?$args;
}
........
.....
...all the other default directives that were in this file originally...
}
Use an FTP client to upload both your startup.sh script and your custom default.sh to the /home directory for your PHP App Service.
Next, under ‘Configuration’ in the portal target /home/startup.sh (or whatever the startup script file name is).
Laravel App
Lastly, restart the App Service. This should now be using our custom startup script. Use LogStream or the Diagnose and Solve -> Application Logs detector, or other methods, to see the stdout from the script.

Fresh Laravel install displays 403 Forbidden in Nginx

I have created a fresh Laravel application by using composer create-project command. Then I put all the folders and files in /usr/share/nginx/html/, which is the default document root for my nginx server. However everytime I runs the http://localhost, it keeps displaying 403 Forbiden. I tried creating a testing index.php (<? php_info();) and it worked fine.
I've read somewhere that I need to set the containing folder (/html), as well as the app/storage folder permission to 777 but still no luck.
Please help me. Thank you in advance.
Here is the nginx default.conf
Laravel projects serve from the <projectName>/public directory. Make sure your nginx config is set up to look there for your index file and NOT in your <projectName> folder only.
Would you edit your question and paste your server config there?
I think it may because index.php is not in the index file list. check these lines:
index index.html index.php;
or
try_files $uri $uri/ =404;
The accepted answer is correct - Laravel services from the public folder and you need to tell nginx to look there - but also a little vague if you're bumping up against this problem. When you create new sites on a Homestead installation by adding them to your .yaml file and using vagrant provision or vagrant up --provision, the created nginx conf file will need editing before Laravel will serve files correctly.
Go to your CLI, and enter Homestead using vagrant ssh or homestead ssh. It will ask for your password, which by default is "vagrant".
Once you're in the virtual machine, type the following commands:
sudo nano /etc/nginx/sites-enabled/your-site-name-here
Then add /public to the end of the existing root near the top of the file (it'll be something like /home/vagrant/projects/your-site to begin with and save in Nano (ctrl+s), then exit nano (ctrl+x).
Once you've exited Nano, restart nginx using sudo nginx -s reload. Your routing will now work!

Categories