How to Install New Relic on my php application?
jer-tan
HOBBYOP

2 years ago

I am running a php web application and I would like to track error in my new relic dashboard, how do I do that? Because it seems like I need access to the php.ini

28 Replies

jer-tan
HOBBYOP

2 years ago

b01880bc-a5fa-4f41-b977-800d4b3367ef


brody
EMPLOYEE

2 years ago

for ease of use, you would want to use a Dockerfile so that it's easy to copy in your own php.ini into the container.


jer-tan
HOBBYOP

2 years ago

Can I still use nixpacks?


brody
EMPLOYEE

2 years ago

nope, nixpacks and a Dockerfile are separate things


jer-tan
HOBBYOP

2 years ago

umm sorry, but what's the difference between nixpacks and Dockerfile?


brody
EMPLOYEE

2 years ago

nixpacks tries to determine the app and what the app needs to run and builds an image automatically for you (or for railway in this case)

a Dockerfile is a text file with what is essentially just a list of commands that you write yourself, that tell the builder how to build an image and run your application.

you will get far more flexibility out of a Dockerfile than you would nixpacks. since you then control nearly everything that's run during build.


jer-tan
HOBBYOP

2 years ago

ahh, so if I have Dockerfile it will not run nixpacks anymore right?


brody
EMPLOYEE

2 years ago

that's correct, it's either one or the other


brody
EMPLOYEE

2 years ago

there would be absolutely no shortage of examples for Dockerfiles that run php apps with a modified php.ini file, and since Dockerfiles are in no way exclusive to Railway, so as long as you follow some best practices you'll have what you need up and running in no time


jer-tan
HOBBYOP

2 years ago

Because I kinda developed my application using the nixpacks way, so if I have to write Dockerfile myself then I'll have to figure a lot of things


brody
EMPLOYEE

2 years ago

like I said, it's essentially a file with a list of commands that tell the builder what to run during build and how to run your app, as long as you understand the php ecosystem you will have no difficulties


brody
EMPLOYEE

2 years ago

watch some YouTube videos, read some articles, look at some examples, you'll pick it up in no time!


jer-tan
HOBBYOP

2 years ago

Alright Ill check it out


brody
EMPLOYEE

2 years ago

sounds good!


jer-tan
HOBBYOP

2 years ago

Thanks for the encouragement, Imma spend my night playing with docker


brody
EMPLOYEE

2 years ago

happy to help!


jer-tan
HOBBYOP

2 years ago

Um a little bit more help, here's my dockerfile

# Stage 1: Build the application with Node.js and Composer
FROM node:18 AS build-stage

# Set the working directory
WORKDIR /app

# Install PHP and PostgreSQL extension
RUN apt-get update && \
    apt-get install -y php php-pgsql

# Copy package.json and pnpm-lock.yaml
COPY package.json pnpm-lock.yaml ./

# Install pnpm
RUN npm install -g pnpm

# Install Node.js dependencies using pnpm
RUN pnpm install

# Copy Composer files
COPY composer.json composer.lock ./

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install PHP dependencies
RUN composer install

# Copy the application code to the container
COPY src/ /app/src

# Stage 2: Set up the PHP and Nginx environment
FROM php:8.1-fpm

# Install Nginx
RUN apt-get update && apt-get install -y nginx

# Install PostgreSQL extension for PHP
RUN apt-get install -y libpq-dev && docker-php-ext-install pdo pdo_pgsql pgsql

# Copy Nginx configuration file
COPY nginx/default.conf /etc/nginx/conf.d/default.conf

# Copy the PHP application from the build stage
COPY --from=build-stage /app /var/www/html

# Set the working directory
WORKDIR /var/www/html

# Expose port 80
EXPOSE 80

# Start Nginx and PHP-FPM
CMD ["sh", "-c", "php-fpm -D && nginx -g 'daemon off;'"]

Now it works on my PC when I use docker run --env-file .env -p 8080:80 my-php-nginx-app, but it doesn't work when I run on railway


jer-tan
HOBBYOP

2 years ago

@Brody really need your help 😭 Now the issue is it's serving the nginx example page rather than my actual website, but whenever I try running the docker in my PC it's working perfectly. What could be the issue?


brody
EMPLOYEE

2 years ago

you need to copy your site files into the location of the serve folder


aleks
HOBBY

2 years ago

Custom php.ini in nixpacks will come hopefully soon, I'll do it when I have any free time


jer-tan
HOBBYOP

2 years ago

yay niceee


aleks
HOBBY

2 years ago

So probably in a couple weeks 😅


jer-tan
HOBBYOP

2 years ago

Copied, here's my Nginx config file:

events {
  worker_connections  4096;  # Default: 1024
}

http {
    server {
        listen 80;
        listen [::]:80;
        server_name localhost;

        root /var/www/html/src;

        location ~ /\.ht {
            deny all;
        }

        client_max_body_size 10M;

        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-Content-Type-Options "nosniff";

        index index.php;

        charset utf-8;

        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

        location / {
            try_files $uri $uri.html $uri/ @extensionless-php;
            index index.html index.php;
        }

        location @extensionless-php {
            rewrite ^(.*)$ $1.php last;
        }

        location /sso/organization/ {
            rewrite ^/sso/organization/(.+)$ /sso/organization/update.php?id=$1 last;
        }

        location /sso/event/ {
            rewrite ^/sso/event/(.+)$ /sso/event/update.php?id=$1 last;
        }

        location /portal/organization/ {
            rewrite ^/portal/organization/(.+)$ /portal/organization/?id=$1 last;
        }

        location /portal/event/ {
            rewrite ^/portal/event/(.+)$ /portal/event/update.php?id=$1 last;
        }

        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_index index.php;
            include fastcgi_params;
        }

        location ~ /\.(?!well-known).* {
            deny all;
        }

        error_page 404 /notfound.php;
    }
}

Now I have to add PORT variable into my deployment and in the docs mentioned its not recommended to do so. But whenever I try using variable like ${PORT} it doesnt work. How do I fix it?


jer-tan
HOBBYOP

2 years ago

No worries, it will helps in my future project, I already started writing DockerFile :D


brody
EMPLOYEE

2 years ago

nginx does not provide a native way to listen on the PORT environment variable, for this you will have to set a static port in your nginx config file and in the service variables


jer-tan
HOBBYOP

2 years ago

Ahh okay


jer-tan
HOBBYOP

2 years ago

I got my application up and running, how do I install New Relic?


brody
EMPLOYEE

2 years ago

have you followed their docs


Loading...