R
Railway•6mo ago
Croissant

How to Install New Relic on my php application?

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
Solution:
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.
Jump to solution
24 Replies
Percy
Percy•6mo ago
Project ID: b01880bc-a5fa-4f41-b977-800d4b3367ef
Croissant
CroissantOP•6mo ago
b01880bc-a5fa-4f41-b977-800d4b3367ef
Solution
Brody
Brody•6mo 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.
Croissant
CroissantOP•6mo ago
Can I still use nixpacks?
Brody
Brody•6mo ago
nope, nixpacks and a Dockerfile are separate things
Croissant
CroissantOP•6mo ago
umm sorry, but what's the difference between nixpacks and Dockerfile?
Brody
Brody•6mo 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.
Croissant
CroissantOP•6mo ago
ahh, so if I have Dockerfile it will not run nixpacks anymore right?
Brody
Brody•6mo ago
that's correct, it's either one or the other 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
Croissant
CroissantOP•6mo 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
Brody•6mo 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 watch some YouTube videos, read some articles, look at some examples, you'll pick it up in no time!
Croissant
CroissantOP•6mo ago
Alright Ill check it out
Brody
Brody•6mo ago
sounds good!
Croissant
CroissantOP•6mo ago
Thanks for the encouragement, Imma spend my night playing with docker
Brody
Brody•6mo ago
happy to help!
Croissant
CroissantOP•6mo 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;'"]
# 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 @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
Brody•6mo ago
you need to copy your site files into the location of the serve folder
root
root•6mo ago
Custom php.ini in nixpacks will come hopefully soon, I'll do it when I have any free time
Croissant
CroissantOP•6mo ago
yay niceee
root
root•6mo ago
So probably in a couple weeks 😅
Croissant
CroissantOP•6mo 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;
}
}
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? No worries, it will helps in my future project, I already started writing DockerFile :D
Brody
Brody•6mo 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
Croissant
CroissantOP•6mo ago
Ahh okay I got my application up and running, how do I install New Relic?
Brody
Brody•6mo ago
have you followed their docs
Want results from more Discord servers?
Add your server