How to setup up Laravel with Nginx on AWS linux AMI - php

I am trying to user laravel with nginx on AWS linux ami, however when I try to access my instance public ip i get file not found. Here is what i do
sudo amazon-linux-extras install nginx1
sudo service nginx start
After this step I am sure nginx is working because I can see nginx webpage. Now I am trying to install laravel
sudo amazon-linux-extras install php7.3
sudo yum install php-xml php-mbstring
sudo service php-fpm start
sudo service nginx restart
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer global require laravel/installer
export PATH=$PATH:/home/ec2-user/.config/composer/vendor/bin/
laravel new test
sudo vi /etc/nginx/nginx.conf
server {
listen 80;
server_name _;
root /home/ec2-user/test/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
sudo chown -R apache:apache storage/
sudo chown -R apache:apache bootstrap/cache
When I try to load my page i recieve file not found.

You can fix this issue in 2 way:
Providing a valid server name/ Domain
Fixing Nginx Default IP address
Provide valid server name
You need to fix the server name inside your Nginx configuration you have putten the server name as _. You need to put there a valid domain name. For example:
server_name example.com;
Then create an A record of the domain pointing to the IP adress. For example:
example.com A 123.123.123.123
Fix Nginx Default configuration
By default, if you install Nginx the server IP takes the files from /var/www/html so you can find this issue in 2 way:
Upload the files in the default path
Change the Nginx default path
Upload the files in the default path:
1st delete the html folder under /var/www/
2nd uload your laravel files inside /var/www/
3rd rename your laravel project public folder to html
Now restart nginx and visit your ip
Change Nginx Default Path
Open the default file
sudo nano /etc/nginx/sites-enabled/default
Find root /var/www/html and replace the /var/www/html with your path
Restart nginx and then visit your IP

Related

Nginx on Docker serves only welcome pages

I'm using macOS. I'm working on a Debian image created through Dockerfile. Nginx, php-fpm was installed in Debian image. Then I copied server file to /etc/nginx/sites-available/server and created its symbolic link file in /etc/nginx/sites-enabled/. It also copied srcs/info.php files to /var/www/server/info.php.
After starting nginx service, I can access my private IP address 192.168.0.46 and see NGINX's welcome page. However, when accessing 192.168.0.46/info.php, page opening fails. This is the same when other html files are inserted.
I looked it up on Google and found out that it was an INCLUDE problem of /etc/nginx/nginx.conf, but there was no problem.
The first question I suspected was the firewall, but if it was the problem, shouldn't I not be able to see the welcome page of Nginx?
I've been thinking and searching all day, but I couldn't get an answer. Please help me!
Here is my files:
Dockerfile :
FROM debian:buster
ARG DEBIAN_FRONTEND=noninteractive
ENV USER=root
COPY srcs/server.sites-available /etc/nginx/sites-available/server
COPY srcs/info.php /var/www/server/info.php
COPY srcs/login_form.html /var/www/server/login_form.html
# Installed mariadb-server instead of mysql-server
RUN apt-get update && apt-get upgrade -y && \
apt-get install -y --no-install-recommends --no-install-suggests \
nginx \
openssl \
mariadb-server \
php-fpm \
php-mysql && \
ln -s /etc/nginx/sites-available/server /etc/nginx/sites-enabled/
CMD service mysql start; \
service nginx start; \
bash;
# EXPOSE 80 for HTTP and 443 for HTTPS
EXPOSE 80 443
server.sites-available :
server {
listen 80;
listen [::]:80;
root /var/www/server;
index index.php index.html index.htm;
server_name server;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}
info.php :
<?php
phpinfo();
?>
nginx.conf :
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Here is my commands:
$ docker build -t server_image .
$ docker run -it -P --rm --name server_container server_image
The server_name server directive means that only requests for http://server will be served.
You should add 192.168.0.46 server to your hosts file.
I'd also recommend changing the server_name to something else like myserver.local (and add that to the hosts file)
use docker run -it -p 80:80 --rm --name server_container server_image to fix it.
But I don't know why...

Nginx virtual host not working

I'm hosting a PHP system on my localhost, I created index.php in the /var/www/html like this:
// /var/www/html/index.php
<?php
phpinfo();
And when I access localhost:80 it's showing up my PHP configurations info.
But when I cloned /etc/nginx/sites-available/default to /etc/nginx/sites-available/superstore and changed server_name it's not working after sudo service nginx restart, even sudo service nginx status is returning success.
My nginx config (/etc/nginx/sites-available/superstore) is like this:
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name superstore.dev;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
My PHP version:
Nginx status after restart:
I added this virtual host to my /etc/hosts like this:
127.0.0.1 superstore.dev
But when I try to access superstore.dev it's returning this error to me:
I installed PHP and Nginx by following command on Ubuntu 16:
sudo apt install nginx php7.0-fpm -y
And I'm sure that I already symbolic linked superstore config file to sites-enabled folder.
Why my virtual host is not working?
Similar to Apache config scheme, folder sites-available is supposed to contain all your config files, even the ones you don't want to run.
There is a second folder, sites-enabled, that contains symlinks to the config files you want to run.
To create the symlink run ln -s /etc/nginx/sites-available/superstore /etc/nginx/sites-enabled/ and then service nginx reload
For more info see answer https://serverfault.com/a/424456/240702

Setting docker with nginx and php5

Trying to setup docker with nginx and php5. Here's my Dockerfile
FROM nginx:1.9.9
RUN apt-get update
RUN apt-get -y install php5-fpm php5-mysql php-apc php5-imagick\
php5-imap php5-mcrypt php5-curl php5-cli php5-gd php5-pgsql\
php5-common php-pear curl php5-json
ADD index.html /usr/share/nginx/html/index.html
ADD index.php /usr/share/nginx/html/index.php
ADD default /etc/nginx/sites-available/default
RUN /etc/init.d/php5-fpm restart
I am able to build this just fine with the following command:
sudo docker build -t myuser/nginx-php5:0.1 .
And then I launch it as follows:
sudo docker run -d -P myuser/nginx-php5:0.1
The nginx deamon is running, I can indeed see my index.html, yet, index.php gets 'downloaded' rather than served. This indicates that PHP is not properly set...
My default file:
server {
listen 80;
root /var/www;
index index.php index.html;
server_name localhost;
access_log /var/log/nginx/localhost.com-access.log;
error_log /var/log/nginx/localhost.com-error.log error;
charset utf-8;
location / {
try_files $uri $uri/ /index.html /index.php?$query_string;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
include fastcgi.conf;
fastcgi_param HTTPS off;
}
location ~ /\.ht {
deny all;
}
}
Any ideas what am I doing wrong? I guess the problem is that I am not doing the proper things to keep php5-fpm running.. but how to fix this?
I think php-fpm didn't run, so nginx just returns php file instead of passing to php-fpm to process.
You should create an "entrypoint.sh" file to launch nginx and php-fpm when run the container. Besides you must correctly set user and group for php-fpm worker processes.

How to set up PHP 7 with NGINX

I installed PHP 7 Nightly and Nginx 1.9.7 (Mainline) on my development Debian Stable:
$ curl http://nginx.org/keys/nginx_signing.key | apt-key add -
$ echo -e 'deb http://nginx.org/packages/mainline/debian/ jessie nginx\ndeb-src http://nginx.org/packages/mainline/debian/ jessie nginx' > /etc/apt/sources.list.d/nginx.list
$ echo -e 'deb http://repos.zend.com/zend-server/early-access/php7/repos ubuntu/' > /etc/apt/sources.list.d/php.list
$ apt-get -y update && time apt-get -y dist-upgrade
$ apt-get -y --force-yes install --fix-missing nginx php7-nightly
$ service nginx restart
I have this configuration file in /etc/nginx/conf.d/php.conf (default.conf uncommented). It's the original default.conf, I just uncommented the PHP fastCGI lines:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
I didn't found any tutorial on the net how to set up Nginx Mainline with PHP 7 yet. :(
Thank You!
After taking a quick look at the packages offered by zend (the packages you are installing), you'll have to write a script for your init system (systemd) to start and manage the php-fpm process.
The binaries are installed in /usr/local/php7/sbin/php-fpm. You'll also have to modify the configuration files in /usr/local/php7/etc. The main change will be to ensure that fpm has a pool listening on address 127.0.0.1 at port 9000.

Trouble Installing Nginx and php5-fpm on Debian 7.3

I have a Debian 7.3 installation in a VM that I am practising installing Nginx and php5-fpm on. I got the Nginx working, by assigning it a manual port of :8080 and that points to /var/www/ for data and in that directory is an index.html and info.php file.
The config file for my Nginx is located at /etc/nginx/conf.d/default.conf and looks like this:
server {
listen 8080;
root /var/www;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.html;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
I have tried changing fastcgi_pass both ways:
fastcgi_pass 127.0.0.1:9000;
and also as:
fastcgi_pass unix:/var/run/php5-fpm.sock;
In my /etc/php5/fpm/pool.d/www.conf file I have the following configuration:
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000
;listen = /var/run/php5-fpm.sock
Here too, I have uncommented the line to match in the Nginx default.conf file.
In my php.ini file I have edited it so that it shows cgi.fix_pathinfo = 0 as required by most of the guides I have seen.
When I try to load nginx, it runs OK. When I try to run php5-fpm this is what happens:
root#debianx86:/# /etc/init.d/php5-fpm status
[FAIL] php5-fpm is not running ... failed!
root#debianx86:/# /etc/init.d/php5-fpm reload
[ ok ] Reloading PHP5 FastCGI Process Manager: php5-fpm.
root#debianx86:/# /etc/init.d/php5-fpm restart
[FAIL] Restarting PHP5 FastCGI Process Manager: php5-fpm failed!
root#debianx86:/# /etc/init.d/php5-fpm start
root#debianx86:/# /etc/init.d/php5-fpm status
[FAIL] php5-fpm is not running ... failed!
root#debianx86:/#
I then open up any of the browsers on my VM and point them to either 127.0.0.1:8080 or localhost:8080 and I get the custom index.html loading that I made and it works! So I then try to load theinfo.php file and I get presented with a 404 Not Found - nginx/1.4.4.
I don't understand what I'm doing wrong. Is there something I'm missing from all this?
I installed nginx from sudo apt-get -y install nginx and sudo apt-get -y install php5-fpm too. Any dependencies they required would have been installed along with that.
Is there a script that I can run on a fresh install of Debian 7.3 that someone has got that will install it properly for me, and make all the modifications so that nginx and php5-fpm are up and running? I've looked over many of the websites with the instructions and I seem to be doing pretty much everything they do, except for the default-sites and enabled-sites, as neither of those folders exist for me, and I don't want to run my virtual hosts like that. I will run them with their own servers listed in the default.conf file.
EDIT: I have even tried following this article at DigitalOcean and it still doesn't work.
EDIT #2: I also did chown -R www-data:www-data /var/www to ensure that the user and group match the information in the www.conf file. I also tried by changing it back to the original root:root specs too. Still nothing.
I think maybe port 9000 is already being used, so php5-fpm can't bind with that port and fails to start.
in the fpm pool settings swap the line of port 9000 with the line with the sock file, then try to start php5-fpm like you were doing, if it works then all you need is to update the nginx configuratuin to proxy pass to the sock file instead of the port.

Categories