How do I change Apache permissions?

Viewed 40

I need to web scraping (e.g. www.google.com) and show the result in another web page, but it seams apache's user does not have permission to open firefox to scrape.

The project has a docker image (php-apache), a php file (that calls the scraping), and a python file, which scrapes using selenium.

I'm able to execute a generic python script inside php, and if I access Docker container as root, I'm able to run my python scrape file successfully. However, the python scrape file can't open selenium driver when it is called by php file as www-data user.

How can I change apache user to root or how can I allow www-data user to run firefox?

Dockerfile

FROM php:7.4.15-apache

# Change apache permissions
RUN usermod -u 1000 www-data
RUN groupmod -g 1000 www-data

# Upgrade installed packages
RUN apt update

# Install python
RUN apt install -y python3.7
# Register the version in alternatives
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1
# Set python3 as the default python
RUN update-alternatives --set python /usr/bin/python3.7

# Install pip
RUN apt install -y python3-pip
# Set pip3 as the default pip
RUN update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1

# Install python modules
RUN pip install selenium

# Install Firefox
RUN apt install -y firefox-esr

# Copy geckodriver from host to docker
COPY geckodriver /usr/local/bin/

docker-compose.yml

version: '3'

services:
  php:
    build: ./docker-images/php-apache/
    container_name: test_scraping_php-container
    ports:
      - "8080:80"
    volumes:
      - ./www:/var/www/html

index.php

<html>
    <head>
        <title>Test PHP - Python - Scraping</title>
    </head>
    <body>
        <?php 
            $cmd = 'python user.py';
            $result = shell_exec($cmd);
            echo "<p>Python get user: $result </p>"; 
        ?>
        <?php 
            $cmd = 'python scraping.py';
            $result = shell_exec($cmd);
            echo "<p>Python get title (scraping): $result </p>";
        ?>
    </body>
</html>

user.py

import getpass

print(getpass.getuser())

scraping.py

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

url = 'https://www.google.com'
options = Options()
options.headless = True
try:
    driver = webdriver.Firefox(options=options)
    driver.get(url)
    print(driver.title)
except:
    print('Error!')
0 Answers
Related