NGINX Setup with WordPress Multisite - php

I wanted to know the configuration for WordPress Multisite when the main site is in the root and the others in /site-name/
For example:
Main website: http://www.example.com/ Second website: http://www.example.com/second-site/ Third site: http://www.example.com/thrid-site/
I looked for the setting, but I only found it with subdomains.

Let's say your folder structure is as follows:
/root_folder/*
/root_folder2/second-site/*
/root_folder3/third-site/*
Then you need:
server {
server_name www.example.com;
root /root_folder/;
location / {
# code
}
location /second-site {
root /root_folder2/;
# code
}
location /third-site {
root /root_folder3/;
# code
}
}
It's important not to add the /second-site and /third-site in the root directive as Nginx will automatically add requested subpaths to the root path at request.
If your folder structure is as follows:
/root_folder/*
/root_folder/second-site
/root_folder/third-site
You only need
server {
server_name www.example.com;
root /root_folder/;
location / {
# code
}
}
And Nginx will do the rest for you.

Related

index.php not loading from ubuntu server VM (nginx & sftp)

I'll try to be short as it can get bloated.
I set up an ubuntu server 20.04 VM on an Oracle vbox.
I installed nginx, php7.4, Xdebug 3.
I configured everything properly for remote debugging and sftp ( I use PHPstorm), I forwarded the ports and customized my local hosts file.Everything works except...
when loading HTTP://127.0.0.2 it loads the default nginx landing page.My index.php file is uploaded in the root /var/www/my_domain folder through sftp. I created the server block for my_domain.
Still won't work. I am new to this so I am missing something.
If you need more info please request.
Any help appreciated!
Vhost config ( server block in nginx) - php-projects being my current domain and containing index.php:
server {
listen 80;
listen [::]:80 ipv6only=on;
root /var/www/php-projects;
server_name php-projects www.php-projects;
location / {
try_files $uri $uri/ =404;
}
error_log /var/log/nginx/debug.log debug;
}
Network settings from Vbox:
PS. I have tried :
How to set index.html as root file in Nginx?
and
Rewrite rule for the root file in nginx?
with no success.
"I configured everything properly for remote debugging and sftp"
Please post your xdebug configuration.
"when loading HTTP://127.0.0.2"
From where ? Host machine or VM?
"My index.php file is uploaded in the root /var/www/my_domain folder
through sftp."
Please post your nginx vhost configuration
"I created the server block for my_domain"
Where? Post the path to the file where you have done this

How can I index files only from specific folder of my public/ - Nginx?

I have 2 txt files that I placed at /home/forge/laravel58/public/files;
I want to index those 2 txt files when I goto my site/files
I've tried
location /files {
#auth_basic "Restricted";
#auth_basic_user_file /home/forge/laravel58/.htpasswd;
alias /home/forge/laravel58/public/files;
autoindex on;
}
Go to : site/files, and see
403 Forbidden Nginx
The trailing slash is essential for autoindex to work, it should be:
location /files/ {
alias /home/forge/laravel58/public/files/;
autoindex on;
}
Next, check that nginx has execute permissions (+x) on every folder in the path.
After that remove any index file from this folder, by default it's index.html.
And finally, check that your location / directive has attempt to try directories:
location / {
...
try_files $uri $uri/ ...;
^^^^^
}
why nginx if you want you can use symbolic link
usage : ln -s /path/to/file /path/to/symlink
ln -s /home/forge/laravel58/public/files site/files with full path
The other answer about trailing slash being "essential for autoindex to work" is 100% incorrect: trailing slash is not required here, although, it is, in actuality, the preferred paradigm, because otherwise you make it possible to access both regular files and directories like /filesSECRET, one level up from /files/, opening yourself to potential security issues.
In your situation, where /files is the suffix of both the location, as well as alias, it is preferred to use root instead of alias. See http://nginx.org/r/alias and http://nginx.org/r/root.
In order for http://nginx.org/r/autoindex to work, the UNIX user under which the nginx process is running must have "read" permission on the final directory of the path, as well as "execute" permissions for every part of the path.
You can use stat(1), or ls -l, to examine permissions, and chmod(1) to change the permissions. You'd probably want o+rx on /home/forge/laravel58/public/files, as well as o+x on every single directory that's leading to the above one.
server {
listen 80;
listen 443 ssl;
server_name www.old-name.com;
return 301 $scheme://www.new-name.com$request_uri;
}
SOURCE

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;
}

Magento 2 can't find css and js files

I moved my site based on Magento 2 from hosting to my localhost.
I cleared cache, adjusted(secure and unsecure) URLs in core_config, run static content deploy() using CLI. Checked all permissions for "folder".
Magento runs but with no CSS and js files.
In console I can see the following:
What should I do to remove this issue?
P.S
Win 10
Open Sever (PHP7x64, MySQL5,7x64, Apache-PHP7-x64+Nginx1.10)
No external caching
P.P.S Before I copied the site from the host I tried to setup Magento with sample data using CLI and I received the same issue! So I believe it's not the only issue about moving Magento 2 from host to local.
I can see that M2 tries to load all files from the version1485628564 folder which doesn't exist in the pub/static
http://magehost.two/pub/static/**version1485628564**/frontend/Magento/luma/en_US/mage/calendar.css
You need to update the .htaccess file under /pub/static folder. Open MAGENTO_DIR/pub/static/.htaccess and add the following code:
...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /pub/static/ # <- Add This
...
Alternatively, you can disable static file signing by adding this record into the core_config_data table with this query:
INSERT INTO `core_config_data` VALUES (NULL, 'default', 0, 'dev/static/sign', 0);
In this case, keep in mind that this will disable the browser's cache refreshing mechanism.
After the execution, you have to flush the Magento cache.
UPDATE 2018
In 2018 I've made a Pull Request to the Magento 2 team that includes this fix. Latest versions of branch 2.3 and 2.4 include the above row in the .htaccess file:
## you can put here your pub/static folder path relative to webroot
#RewriteBase /magento/pub/static/
You have to uncomment the row and set it accordingly to your Magento installation.
You can find the same row under the /pub/media/.htaccess file.
As you are using nginx, the htaccess comment above wont help you. You need to add this to your nginx domain config;
location /static/ {
# Remove version control string
location ~ ^/static/version {
rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;
}
It means your deployed_version.txt is removed. Add it again and deploy your Magento 2. Then it will work fine.
deployed_version.txt has to exist in pub/static/.
You need to run below command on CLI
path to Magento root folder : php bin/magento setup:static-content:deploy
path to Magento root folder : php bin/magento cache:flush
Add one more answer that might be helpful here. Firstly, if the website is set to production mode, make sure you run the command to deploy the static assets as below:
php bin/magento setup:static-content:deploy
Second, if your site is hosting with Nginx, make sure you include the nginx.conf.sample file located at the Magento 2 root folder. More specifically, following is the snippet (Magento 2.3.0) which handle the static assets requests:
location /static/ {
# Uncomment the following line in production mode
# expires max;
# Remove signature of the static files that is used to overcome the browser cache
location ~ ^/static/version {
rewrite ^/static/(version[^/]+/)?(.*)$ /static/$2 last;
}
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2|json)$ {
add_header Cache-Control "public";
add_header X-Frame-Options "SAMEORIGIN";
expires +1y;
if (!-f $request_filename) {
rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
}
}
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
add_header Cache-Control "no-store";
add_header X-Frame-Options "SAMEORIGIN";
expires off;
if (!-f $request_filename) {
rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
}
}
if (!-f $request_filename) {
rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
}
add_header X-Frame-Options "SAMEORIGIN";
}
You might want to check your Nginx configuration to ensure that it is allowing includes - that is what happened in my case. Without this setting, it will not look at your site nginx.conf file and the server will not be able to find your css, img or js files.
This link has instructions: https://www.inmotionhosting.com/support/edu/wordpress/advanced-nginx-vps-and-dedicated/

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.

Categories