django admin css on apache server - css

I need to solve the missing admin page css problem. I have tested almost all the solutions on the web with no luck! (I'm using django 1.8 on apache2.4)
Here's what I've done so far:
in setting.py I have:
INSTALLED_APPS = [
...
django.contrib.staticfiles',
...
]
STATIC_URL = '/static/'
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
I did python manage.py collectstatic. It created the static folder: 63 static files copied to '/var/www/mysite/mysite/static'.
and then in etc/apache2/sites-available/000-default.conf I added:
Alias /static /var/www/mysite/mysit/static
<Directory /var/www/mysite/mysite/static>
Require all granted
</Directory>
I also reloaded the apache.
It seems that I'm doing something wrong since the django admin doesn't show the css.
Any help would be appreciated.

"The steps presented in the question is actually correct to set the static files on apache server."
The problem was the typo I made in my apache server thread (the correct one is: Alias /static /var/www/mysite/mysite/static). Thanks to #Zagorodniy Olexiy for pointing it out.

After all you wrote I think that the problem is right of the files and directories of your project. Try customize it in this way:
1) Change the rights of all files in your project:
chmod 755 /var/www/mysite/*
2) Change rights to the folders of your project:
find /var/www/mysite/ -type f -exec chmod 644 {} \;
3) Change owner of your project:
chown -R www-data:www-data /var/www/mysite/*
Also you have typo in your apache server thread:
remove
Alias /static /var/www/mysite/mysit/static
to
Alias /static /var/www/mysite/mysite/static
Hope it helps you.

Related

How to fix 'Some files are not writable by WordPress' error, website cannot be changed by admin

I'm setting up a new website. most files are not editable by WordPress.
I am using a virtual machine on google cloud console. It's Ubuntu 18.04 LTS LAMP stack. I have tried editing permissions of the files through both Filezilla and the ssh prompt on gcloud. like it describes here. I think i have a different problem.
I am brand new and just learning so I'm sorry if the answer is obvious. I found that if I go in and manually set permissions for files to 775 in the SSH WordPress is able to edit some files. But i cannot edit permissions of some files even after i am in root
sudo su
Apart from permissions you have to change the Owner of the Files to the web server user ( most cases, especially those using apache www-data ) .
The command to do that is the below
$ sudo chown -R www-data:www-data /path/to/wordpress/
the usual path of a website on a ubuntu server is /var/www/domain.com/ but of course that depends !
Wish you the best!
change all files and folders permission as follow:
enter your public_html folder and execute this two commands
find . -type d -exec chmod 0755 {} \;
find . -type f -exec chmod 0644 {} \;
My issue was Wordpress somehow wanted to use "apache" as the user. Changing the ownership from nginx:nginx (which is what runs my web daemon) to apache:nginx worked.
I recommend trying this on ONE file and confirming with the "site health" tool to make sure it is YOUR problem before doing a global change!
chown apache:nginx ./*.php
I was getting the same error "Some files are not writable by WordPress:
wp-load.php" recently as I moved to a different host provider. The error was shown in the Site Health window from WordPress.
To fix this, all you have to do is to change the permission of the file(s). You can do this easily by using the tool called FileZilla. I use this tool for all my FTP actions.
Connect to your server and right-click on the file and select File permissions.
And finally, change the permission to 0644.

WordPress asking for my FTP credentials to install plugins

I installed a WordPress blog in my local system. But when I try to add plugins from admin it asks for FTP access. What do I need to configure for WordPress to be able to upload without FTP?
Try to add the code in wp-config.php:
define('FS_METHOD', 'direct');
If you are using Ubuntu.
sudo chown -R www-data:www-data PATH_TO_YOUR_WORDPRESS_FOLDER
"Whenever you use the WordPress control panel to automatically install, upgrade, or delete plugins, WordPress must make changes to files on the filesystem.
Before making any changes, WordPress first checks to see whether or not it has access to directly manipulate the file system.
If WordPress does not have the necessary permissions to modify the filesystem directly, you will be asked for FTP credentials so that WordPress can try to do what it needs to via FTP."
Solution:
In order to find out what user your instance of apache is running as, create a test script with the following content:
<?php echo(exec("whoami")); ?>
For me, it was daemon and not www-data. Then, fix the permission by:
sudo chown -R daemon /path/to/your/local/www/folder
If during installation of a plugin, Wordpress asks for your hostname or FTP details.
Then follow these steps:
Login to your server and navigate to /var/www/html/wordpress/.
Open wp-config.php and add this line after define(‘DB_COLLATE’)
define('FS_METHOD', 'direct');
If you get "Could not create directory" error. Give write permissions to your wordpress directory in recursive as
chmod -R go+w wordpress
NOTE. For security, revoke these permissions once you install a plugin as
chmod -R go-w wordpress
I changed the ownership of the wordpress folder to www-data recursively and restarted apache.
sudo chown -R www-data:www-data <folderpath>
It worked like a charm!
On OSX, I used the following, and it worked:
sudo chown -R _www:_www {path to wordpress folder}
_www is the user that PHP runs under on the Mac.
(You may also need to chmod some folders too. I had done that first and it didn't fix it. It wasn't until I did the chown command that it worked, so I'm not sure if it was the chown command alone, or a combination of chmod and chown.)
I did a local install of WordPress on Ubuntu 14.04 following the steps outlined here and simply running:
sudo chown -R www-data:www-data {path_to_your_project_directory}
solved my issue with downloading plugins. The only reason I'm leaving this post here is because when I googled my issue, this was one of the first results and it led me to the solution to my problem.
Hope this one helps to anyone!
There's a lot of similar responses to this question, but none of them fully touch on the root cause. Sebastian Schmid's comment on the original post touches on it but not fully. Here's my take as of 2018-11-06:
Root Cause
When you try to upload a plugin through the WordPress admin interface, WordPress will make a call over to a function called "get_filesystem_method()" (ref: /wp-admin/includes/file.php:1549). This routine will attempt to write a file to the location in question (in this case the plugin directory). It can of course fail here immediately if file permissions aren't setup right to allow the WordPress user (think the user identity executing the php) to write the file to the location in question.
If the file can be created, this function then detects the file owner of the temporary file, along with the file owner of the function's current file (ref: /wp-admin/includes/file.php:1572) and compares the two. If they match then, in WordPress's words, "WordPress is creating files as the same owner as the WordPress files, this means it's safe to modify & create new files via PHP" and your plugin is uploaded successfully without the FTP Credentials prompt. If they don't match, you get the FTP Credentials prompt.
Fixes
Ensure the plugin directory is writable by the identity running your php process.
Ensure the identity that is running your php process is the file owner for either:
a) All WordPress application files, or...
b) At the very least the /wp-admin/includes/file.php file
Final Comments
I'm not overly keen on specifically applying file ownership to the file.php to work around this issue (it feels a tad hacky to say the least!). It seems to me at this point that the WordPress code base is leaning towards having us execute the PHP process under the same user principal as the file owner for the WordPress application files. I would welcome some comments from the community on this.
From the first hit on Google:
WordPress asks for your FTP credentials when it can't access the files
directly. This is usually caused by PHP running as the apache user
(mod_php or CGI) rather than the user that owns your WordPress files.
This is rather normal in most shared hosting environments - the files are stored as the user, and Apache runs as user apache or httpd. This is actually a good security precaution so exploits and hacks cannot modify hosted files. You could circumvent this by setting all WP files to 777 security, but that means no security, so I would highly advise against that. Just use FTP, it's the automatically advised workaround with good reason.
For me the the process that solved, to be able to work on my localhost using Ubuntu, was:
(of course you must replace myUser by your user, whoami show it for you if you dont know)
Include myself on www-data group (to be able to access and edit files without sudo):
sudo usermod -aG www-data myUser
Set myself and this group as files owners:
sudo chown -R myUser:www-data /var/www/html
Set a major permission for the group (the group must write too):
sudo find . -type f -exec chmod 664 {} \;
sudo find . -type d -exec chmod 775 {} \;
Then add this line on config.php
define('FS_METHOD', 'direct');
The easiest way to solve this problem is add the following FTP information to your wp-config.php
define('FS_METHOD', 'direct');
define('FTP_BASE', '/usr/home/username/public_html/my-site.example.com/wordpress/');
define('FTP_CONTENT_DIR', '/usr/home/username/public_html/my-site.example.com/wordpress/wp-content/');
define('FTP_PLUGIN_DIR ', '/usr/home/username/public_html/my-site.example.com/wordpress/wp-content/plugins/');
FTP_BASE is the full path to the "base"(ABSPATH) folder of the WordPress installation
FTP_CONTENT_DIR is the full path to the wp-content folder of the WordPress installation.
FTP_PLUGIN_DIR is the full path to the plugins folder of the WordPress installation.
We had the same problem as part of a bigger problem. The suggested solution of
define('FS_METHOD', 'direct');
hides that window but then we still had problems with loading themes and upgrades etc. It is related to permissions however in our case we fixed the problem by moving from php OS vendor mod_php to the more secure php OS vendor FastCGI application.
First move to your installation folder (for example)
cd /Applications/XAMPP/xamppfiles/
Now we’re going to modify your htdocs directory:
sudo chown -R daemon htdocs
Enter your root password when prompted, then finish it out with a chmod call:
sudo chmod -R g+w htdocs
I was facing the same problem!
I've added the code below in wp-config.php file (in any line) and it's working now!
define('FS_METHOD', 'direct');
define('FS_METHOD', 'direct');
Add this to wp-config.php
If the issue still persist , you can try setting permission of plugin folder to 755
Or in linux you can set it by this command
Chmod -R 755
As mentioned by Niels, this happens because the server process user can't write to the Wordpress folder.
But here's the thing a lot of articles don't explain. It's the owner of the php process, not the nginx process. If you try to change the nginx owner, it won't solve this.
To solve it, try running ps aux to see which user owns the php-fpm process. Then check that user is the same user as the owner of the wordpress folder, or can at least write to it. If the user can't write to it, you'll need to change permissions and/or ownership of the folder; or put the two users (server owner and wordpress folder owner) in a common group which can write to the folder; or change php.ini "user" property to a user that can write to the folder.
Changing the ownership of the files worked but only after I logged out of my wordpress website and logged in again. I also restarted the Apache server, but that may not be necessary.

Wordpress Index.php not loading

I'm a newbie here. I installed a wordpress in a server run by Apache2. But the wordpress doesn't load index.php page. I can access deshboard and all other categories are displaying well. but the home url is not loading the index.php. Eventhough i try to added /index.php at the end of my url, still it's not working. It just display 404 error. When i look at the theme directory and wordpress directory, there are (index.php) files.
so I googled for solution. some suggested to add
directoryindex index.php in .htaccess file. I did that and that result in 500 internal server error.
Some post in google suggest to look at dir.conf and httpd.conf under Apache2. In my httpd.conf file, I have this line of code
SSLcertificateChainfile /etc/ssl/certs/secure_yosalasd_org.ca-bundle
In my dir.conf file, I have this lines of code
<IfModule mod_dir.c>
DirectoryIndex index.php index.html
</IfModule>
I don't seem to find answer to this problem.I would appreciate if anyone can figure out what is the problem here.
thank you so much in advance
regards
Khun
This may be a long shot, but once saw a simillar problem which in the end the problem was that there was an index.html file as well in the folder.
Make sure such file does not exist.
And did you try re-installing?
may be the premits of htaccess disallow it from working, that will result not found pages
if you uploaded the script though root access
try this command
chown -hR user:user folder_path;chmod -R 644 folder_path/*.php
replace user by your user name, replace folder path by the full path of wordpress
Edit:
The problem is your theme not include index.php
try to explore your theme directory, and check if there is index.php file, or switch to another theme
if the file is exists, Try to change file permissions use the command that i provided above again, i've edited it
You can change main diretory and sub directory all php files permission with this SSH Command
find /top/level/directory -type f -name "*.php" -print | xargs chmod 644

Symfony2 what to do when deploying manually?

Since i can't find any useful information on Symfony2 website i'm asking here. What should be done when manually deploying a Symfony2 project? As far i understand:
Edit app/config/paramenters.ini to meet server paramenters
Update vendors to the latest version: php bin/vendors update
Install database with php app/console doctrine:database:create then tables with php app/console doctrine:schema:update --force and eventually load fixtures: php app/console doctrine:fixtures:load
Dump and install assets/assetics: php app/console assets:install --symlink web as long as php app/console assetic:dump --env=prod --no-debug
Symlink index.php to web/app.php, assuming that all files from Symfony2 distribution are in the root of the web server
Remove web/app_dev.php (is this really necessary?)
should app/config/paramenters.ini left untouched? What about security concerns?
Am i right? Thanks for helping. I think that Symfony2 documentation should cover this too.
EDIT: virtual host example:
<VirtualHost *:80>
ServerName symfony.local
DocumentRoot "C:/www/Symfony2/web"
DirectoryIndex app.php
<Directory "C:/www/Symfony2/web">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
Only two things I wouldn't do is:
Symlink index.php to web/app.php, assuming that all files from
Symfony2 distribution are in the root of the web server
Remove web/app_dev.php (is this really necessary?)
The thing with the symlink might be ok, but I prefer to link the webroot directory with the web directory of symfony2.
Second thing is, you don't have to remove app_dev.php, because it can only be run from localhost (it checks the IP). So no need to remove it, and I personally sometimes want to see debug info on the webpage (maybe not best way), and than I just add my own IP to app_dev (and remove it afterwards).
Yes. That is all. And also you don't need web/app_dev.php in prod environment.
Moreover, web folder should be document root and run app.php
I think you should put DirectoryIndex app.php inside <Directory></Directory>

Why does my Django admin site not have styles / CSS loading?

I made a Django admin site using Django development version but it isn't being styled:
After setting up your STATIC_ROOT and STATIC_URL, you may have to run
python manage.py collectstatic
ADMIN_MEDIA_PREFIX is deprecated now, use STATIC_URL instead. Setting STATIC_URL = '/static/' in settings.py should do the job. Try:
import os.path
import sys
PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
and then:
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
Works on Django 1.4 pre-alpha SVN-16920.
I broke my head over it for two days trying whatnot!
Finally, changed DEBUG in the settings.py file to:
DEBUG = True
and it worked.
P.S:
SECURITY WARNING: don't run with debug turned on in production!
Django does not serve static files on it's own. You have to tell it where the files are.
The ADMIN_MEDIA_PREFIX in the settings.py will point Django in the right location.
Since you're using the development version, you'll want the dev-specific document for static files how-to. Adam's link will lead you to the 1.2 version.
I read several other threads trying to fix this...resorted to an alias as in other threads.
This assumes that your own custom app is serving static files correctly, which would indicate that your STATIC_ROOT and STATIC_URL have proper settings.
STATIC_ROOT = ''
STATIC_URL = '/static/'
Then (from your static directory):
ubuntu#ip-1-2-3-4:/srv/www/mysite.com/app_folder/static$ sudo ln -s /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/ admin
Hope this helps someone...there are a lot of threads on this topic.
I ran into this issue as well following the Django Book Tutorial.
In Chapter 5|Installing the model, the book states when referring to the default INSTALLED_APPS-
"Temporarily comment out all six of those strings by putting a hash character (#) in front of them."
http://www.djangobook.com/en/2.0/chapter05.html
Then, in Chapter 6, the Book tells the reader to uncomment 4 of those 6 lines-
"note that we commented out these four INSTALLED_APPS entries in Chapter 5. Uncomment them now."
But the statcifiles line is what is needed to restore CSS to the admin page, so uncomment that
'django.contrib.staticfiles',
In /project_name/project_name/settings.py you need to set STATIC_URL to tell your site what url to use for static files.
Then set STATIC_ROOT to be some folder on your filesystem that is not the same as any of your directories listed in STATICFILES_DIRS list.
Once STATICFILES_ROOT is set, you would run python manage.py collectstatic from the project directory.
This will copy all the admin static files and all files in any other folders listed in the STATICFILES_DIRS list. Basically this puts all your static files in one place so you you can move them to your CDN when deploying your site. If you are like me and don't have a CDN, then you have two options:
Add the folder you set as STATIC_ROOT to the STATICFILES_DIRS list. This will allow the staticfiles finders in django to locate all the static files.
Move the entire folder of static files somewhere else on your file system and direct STATICFILES_DIRS to include that new location.
I make no comments about security with this answer, it is just the way I have been able to develop with my web server for small projects. I expect that you will want a CDN as django suggest if you are doing anything larger scale.
UPDATE:
I just ran into this issue and this method didn't quite do what I think you want. What ended up working for me was after I ran collectstatic I just copied the admin static files that it put into STATICFILES_ROOT into the directory that I had used for my own static files. That solved the issue for me.
In addition to many of the other answers being useful, I had a problem that hasn't yet been noted. After upgrading from Django 1.3 to 1.6, my static files directory had a broken symbolic link to the django admin static files.
My settings.py was configured with:
STATICFILES_DIRS = (
'/var/www/static/my-dev',
)
According to this answer,
Django will now expect to find the admin static files under the URL
/admin/.
I had a symbolic link /var/www/static/my-dev/admin which was set to:
admin -> /usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/
That location no longer exists in django 1.6, so I updated the link to:
admin -> /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/
And now my admin site is working properly.
run: python manage.py collectstatic
Add this line to Vhost which located at : /etc/apache2/sites-available/000-default.conf
Alias /static/admin/ /var/www/html/example.com/static/admin
Here is entire Vhost setting for django setup
<VirtualHost *:80>
ServerName gautam.tech
ServerAlias www.gautam.tech
WSGIDaemonProcess gautam.tech python-path=/var/www/html/gautam.tech python-home=/var/www/html/gautam.tech/venv
WSGIProcessGroup gautam.tech
#Your static files location
Alias /static /var/www/html/gautam.tech/static
Alias /media/ /var/www/html/gautam.tech/media
Alias /static/admin/ /var/www/html/gautam.tech/static/admin
<Directory /var/www/html/gautam.tech/static>
Require all granted
</Directory>
<Directory /var/www/html/gautam.tech/media>
Require all granted
</Directory>
WSGIScriptAlias / /var/www/html/gautam.tech/myproject/wsgi.py
DocumentRoot /var/www/html/gautam.tech
<Directory /var/www/html/gautam.tech>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
CustomLog /var/www/html/gautam.tech/access.log combined
ErrorLog /var/www/html/gautam.tech/error.log
</VirtualHost>
This will work for sure!
I see there are many answers but none of them worked for me, so I'm posting my own.
What solved it for me was adding a static files URL to the root URLs of the app. I needed to add this URL to my URLs list:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
You will also need these two imports:
from django.conf import settings
from django.conf.urls.static import stati
More can be viewed in this article.
If you are using Apache server to host your django site, you need to make sure the static alias point to your /directory to site/site_media/static/. If your static files are in /directory to site/site/site_media/static/, the previous Apache alias configuration will not work.
While following the Django tutorial, I had a similar problem and in my case the issue was the mimetype used by the development server when serving css files.
The mimetype served was 'application/x-css' which led to following warning message in Chrome (in the 'Network' tab of the Developer tools):
Resource interpreted as Stylesheet but transferred with MIME type
application/x-css: "http://127.0.0.1:8000/static/admin/css/base.css"
The workaround that I've found: changing the mimetype to be served by adding following lines to the django webapp's manage.py file:
import mimetypes
mimetypes.init()
mimetypes.types_map['.css'] = 'text/css'
Note: worked for me with Django 1.7.4 on Python 2.7 and Chrome 40.0
Same sort of issue i encountered while developing a site in django-1.10.5 and python-2.7.13. But in my firefox-51 and chrome, the login page was able to get the css but still there was no styling. But weirdly it was working on IE-8..
I tried do every possible thing mentioned here and suitable to my set of sw versions. None worked.
But when i tried the same site on other system which had the python-2.7.8, it worked..
Just posted if it may help someone...
edited: later I found that in python-2.7.13, writing the following two lines in settings.py (plus clearing the cache of the browser) had done the trick
import mimetypes
mimetypes.add_type("text/css", ".css", True)
My issue was resolved by creating new Virtual Environment for the project, before that I was using general system level python interpreter.
$ mkvirtualenv myproject
Reference: https://docs.djangoproject.com/en/2.1/howto/windows/
In the issue is in a dev/test/prod server and using Nginx, please follow the below steps.
set the configs in settings.py as something below
STATIC_URL = '/static/'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Run the below command to create css and js files in static folder
$ python manage.py collectstatic
config in /etc/nginx/sites-enabled/example (Nginx) to serve static files
location /static/ {
alias /project/root/folder/static/;
}
this works fine and easily. I moved (manually) the folder. just you have to copy your static/admin from the directory of the main Project and paste it into public_html static/ if there is no static folder you have to run following command in terminal
python manage.py collectstatic
here you go with css working of Django admin
Ensure that 'django.contrib.staticfiles' is in your INSTALLED_APPS in your settings.py
Admin panel was working fine except css wasn't loaded. This worked for Lightsail Django with Apache
1.Define STATIC_ROOT and STATIC_URL in settings.py
STATIC_ROOT = '/opt/bitnami/projects/decisions/decision/'
STATIC_URL = '/static/'
2.Eject(copy) admin assets files to the project
run python manage.py collectstatic
this command creates /opt/bitnami/projects/decisions/decision/admin folder with css/ fonts/ img/ js/ subfolders
3.Make /static url accessible from apache
Paste this snippet in /opt/bitnami/apache2/conf/bitnami/bitnami.conf (If you have set up ssl then the file location will be /opt/bitnami/apache2/conf/bitnami/bitnami-ssl.conf)
Alias /static/ "/opt/bitnami/projects/decisions/decision/"
<Directory "/opt/bitnami/projects/decisions/decision/">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
4. Don't forget to restart apache
sudo /opt/bitnami/ctlscript.sh restart apache
Failing after trying 1000s of suggestions, I finally found a solution that helped. Here is what I tried and what I was using.
I am using django-1.11 and nginx web server.
Firstly, I made sure that my CSS/js files are not getting 404 in browser's console. After that, I could see a warning
Resource interpreted as Stylesheet but transferred with mime type text/plain
I found the base.html in admin templates and removed
type="text/css"
and now the lines looks like this:
<link rel="stylesheet" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}" />
This fixed the issue for me.
Check your settings.py file
STATIC_URL = '/static/'
there should be backslash ' / ' in both opening and closing side ..
If you have a value set in settings.py for STATICFILES_DIRS and the declared folder doesn't exist or is in the wrong location, it will cause the Admin to have no styling e.g. by defining:
STATICFILES_DIRS = ( os.path.join(BASE_DIR,"static"))
And the static folder doesn't exist .
Configuring static files
Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.
In your settings.py file, define STATIC_URL, for example:
STATIC_URL = '/static/'
For more details see
static files [django-docs]

Resources