Kumaran
Kumaran

Reputation: 3650

How to access ubuntu files as root in php

I have a PHP script which runs in first server and it curls the PHP file which is on the second server.

$service_url = 'http://example.com/version_check.php?f_path='.$path;

On the second server, that PHP file (version_check.php) reads the files in /var/www directory and processes it. Now I want to know how to access /var/www directory with root privileges. I need root privileges because I am doing some fwrites in that directory.

Upvotes: 1

Views: 1889

Answers (4)

kakoma
kakoma

Reputation: 1203

Kumaran, The issue isn't PHP; the issue is the user accessing the files. Your PHP scripts are run by the apache user (www-data or apache, you'd need to check the username your apache's running under). The apache user, by default, can access certain files. To be able to access files using root privileges, the apache user needs to acquire the rights to do so.

Allowing this to happen is a big security risk. You need to re-think the architecture altogether; why is PHP accessing root files in the first place? One option is to make the files in question writeable by the apache user

Upvotes: -1

nonchip
nonchip

Reputation: 1142

you don't. just make the particular files or folders writable by www-data. running your PHP script as root would be a serious security problem. even more so, because I don't expect you to follow any common security guidelines (if you did, you wouldn't want to run your script as root).

Upvotes: 2

icktoofay
icktoofay

Reputation: 129001

If you’re using the default configuration on Ubuntu, you’ll have a user and group named www-data that your web server runs as. If you want to be able to write to a particular file, chown it to www-data:

$ chown www-data:www-data /var/www/my-file

Alternatively, keep the current ownership, change the group, and make it group-writable:

$ chgrp www-data /var/www/my-file
$ chmod g+w /var/www/my-file

As a last resort if you don’t want to change the ownership or group, make it world-writable:

$ chmod a+w /var/www/my-file

But this is a bad idea—any service on your system, good or bad, can modify the file, rather than just your web server. You should avoid it if you can.

Upvotes: 2

Techiegal
Techiegal

Reputation: 1

The /root is only readable to root user. Hence you need to first move that Move file to /var/www and change it's permissions so www-data users can read it.

Upvotes: -2

Related Questions