├── nginx_config_files_php_framework ├── nginx_hosts ├── nginx_conf.d_yoursite.com.conf ├── fastcgi_params.conf ├── nginx_sitesavailable_yoursite.com └── nginx.conf └── README.md /nginx_config_files_php_framework/nginx_hosts: -------------------------------------------------------------------------------- 1 | 27.0.0.1 localhost 2 | 127.0.0.1 your_site.com 3 | 4 | # The following lines are desirable for IPv6 capable hosts 5 | ::1 ip6-localhost ip6-loopback 6 | fe00::0 ip6-localnet 7 | ff00::0 ip6-mcastprefix 8 | ff02::1 ip6-allnodes 9 | ff02::2 ip6-allrouters 10 | -------------------------------------------------------------------------------- /nginx_config_files_php_framework/nginx_conf.d_yoursite.com.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80 default_server; 3 | listen [::]:80 default_server; 4 | 5 | root /var/www/yoursite.com/html/public; 6 | 7 | index index.html index.htm index.php; 8 | 9 | server_name yoursite.com www.yoursite.com; 10 | 11 | location / { 12 | try_files $uri $uri/ /index.php$is_args$args; 13 | } 14 | 15 | location /yoursite.com { 16 | alias /var/www/yoursite.com/public; 17 | try_files $uri $uri/ @yoursite.com; 18 | 19 | location ~ \.php$ { 20 | include snippets/fastcgi-php.conf; 21 | fastcgi_param SCRIPT_FILENAME $request_filename; 22 | fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; 23 | } 24 | } 25 | 26 | location @yoursite { 27 | rewrite /yoursite.com/(.*)$ /yoursite.com/index.php?/$1 las$ 28 | } 29 | 30 | location ~ \.php$ { 31 | include snippets/fastcgi-php.conf; 32 | fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /nginx_config_files_php_framework/fastcgi_params.conf: -------------------------------------------------------------------------------- 1 | fastcgi_param QUERY_STRING $query_string; 2 | fastcgi_param REQUEST_METHOD $request_method; 3 | fastcgi_param CONTENT_TYPE $content_type; 4 | fastcgi_param CONTENT_LENGTH $content_length; 5 | 6 | fastcgi_param SCRIPT_NAME $fastcgi_script_name; 7 | fastcgi_param REQUEST_URI $request_uri; 8 | fastcgi_param DOCUMENT_URI $document_uri; 9 | fastcgi_param DOCUMENT_ROOT $document_root; 10 | fastcgi_param SERVER_PROTOCOL $server_protocol; 11 | fastcgi_param REQUEST_SCHEME $scheme; 12 | fastcgi_param HTTPS $https if_not_empty; 13 | 14 | fastcgi_param GATEWAY_INTERFACE CGI/1.1; 15 | fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; 16 | 17 | fastcgi_param REMOTE_ADDR $remote_addr; 18 | fastcgi_param REMOTE_PORT $remote_port; 19 | fastcgi_param SERVER_ADDR $server_addr; 20 | fastcgi_param SERVER_PORT $server_port; 21 | fastcgi_param SERVER_NAME $server_name; 22 | 23 | # PHP only, required if PHP was built with --enable-force-cgi-redirect 24 | fastcgi_param REDIRECT_STATUS 200; 25 | -------------------------------------------------------------------------------- /nginx_config_files_php_framework/nginx_sitesavailable_yoursite.com: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | listen [::]:80; 4 | 5 | # SSL configuration 6 | # 7 | listen 443 ssl; 8 | listen [::]:443 ssl; 9 | include snippets/self-signed.conf; 10 | include snippets/ssl-params.conf; 11 | 12 | root /var/www/yoursite.com/html/public/; 13 | 14 | # Add index.php to the list if you are using PHP 15 | index index.php index.html index.htm index.nginx-debian.html; 16 | 17 | server_name grandstrandmvc.com www.grandstrandmvc.com; 18 | 19 | return 302 https://$server_name$request_uri; 20 | 21 | 22 | location / { 23 | # First attempt to serve request as file, then 24 | # as directory, then fall back to displaying a 404. 25 | try_files $uri $uri/ =404; 26 | } 27 | 28 | # pass PHP scripts to FastCGI server 29 | # 30 | location ~ \.php$ { 31 | include snippets/fastcgi-php.conf; 32 | 33 | # # With php-fpm (or other unix sockets): 34 | fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; 35 | # # With php-cgi (or other tcp sockets): 36 | # fastcgi_pass 127.0.0.1:9000; 37 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam$ 38 | } 39 | 40 | # deny access to .htaccess files, if Apache's document root 41 | # concurs with nginx's one 42 | # 43 | location ~ /\.ht { 44 | deny all; 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /nginx_config_files_php_framework/nginx.conf: -------------------------------------------------------------------------------- 1 | user www-data; 2 | worker_processes auto; 3 | pid /run/nginx.pid; 4 | include /etc/nginx/modules-enabled/*.conf; 5 | 6 | events { 7 | worker_connections 768; 8 | # multi_accept on; 9 | } 10 | 11 | http { 12 | 13 | ## 14 | # Basic Settings 15 | ## 16 | 17 | sendfile on; 18 | tcp_nopush on; 19 | tcp_nodelay on; 20 | keepalive_timeout 65; 21 | types_hash_max_size 2048; 22 | # server_tokens off; 23 | 24 | server_names_hash_bucket_size 128; 25 | # server_name_in_redirect off; 26 | 27 | include /etc/nginx/mime.types; 28 | default_type application/octet-stream; 29 | 30 | ## 31 | # SSL Settings 32 | ## 33 | 34 | ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE 35 | ssl_prefer_server_ciphers on; 36 | 37 | ## 38 | # Logging Settings 39 | ## 40 | 41 | access_log /var/log/nginx/access.log; 42 | error_log /var/log/nginx/error.log; 43 | 44 | ## 45 | # Gzip Settings 46 | ## 47 | 48 | gzip on; 49 | # gzip_vary on; 50 | # gzip_proxied any; 51 | # gzip_comp_level 6; 52 | # gzip_buffers 16 8k; 53 | # gzip_http_version 1.1; 54 | # gzip_types text/plain text/css application/json application/javascrip$ 55 | 56 | ## 57 | # Virtual Host Configs 58 | ## 59 | 60 | include /etc/nginx/conf.d/*.conf; 61 | include /etc/nginx/sites-enabled/*; 62 | } 63 | 64 | 65 | #mail { 66 | # # See sample authentication script at: 67 | # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript 68 | # 69 | # # auth_http localhost/auth.php; 70 | # # pop3_capabilities "TOP" "USER"; 71 | # # imap_capabilities "IMAP4rev1" "UIDPLUS"; 72 | # 73 | # server { 74 | # listen localhost:110; 75 | # protocol pop3; 76 | # proxy on; 77 | # } 78 | # 79 | # server { 80 | # listen localhost:143; 81 | # protocol imap; 82 | # proxy on; 83 | # } 84 | #} 85 | 86 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nginx_Config_PHP_Framework 2 | Config files for Nginx Server on Ubuntu 18.04 to serve CodeIgniter, Custom PHP Frameworks, Laravel, and other LEMP stack frameworks. 3 | 4 | Note: This is an ongoing project for me as I learn how to use Nginx in place of Apache, this repository is intended to help beginner to intermediate PHP developers up and running for educational videos. I will work on security and better ways to improve the speed and performance of the setup as I learn more. 5 | 6 | ## Pre-requisites For Using This Source Code To Setup Nginx Server on Ubuntu 18.04 7 | 8 | * Must be using PHP-FPM 5.0 or newer, however this code is set up for using PHP-FPM 7.2 with Ubuntu 18.04. Please make the necessary changes to the code to whatever version of PHP-FPM version you are using. 9 | 10 | * Already have Nginx, PHP, and MySQL installed on your Ubuntu 18.04 server. The code should work with Ubuntu 16.04 or newer, however our code was built for 18.04. If you do not have the LEMP stack or Nginx setup on your machine, then please check out this great guide from Digital Ocean on how to setup the LEMP stack on Ubuntu at: https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-ubuntu-18-04 11 | You may download Nginx at: https://nginx.org/en/linux_packages.html#stable 12 | 13 | * Have Nginx downnloaded and enabled, if you do not, below is the command line order to enable nginx as your localhost service. 14 | 15 | 16 | > sudo systemctrl stop apache2.service 17 | 18 | > sudo systemctrl enable nginx.service 19 | 20 | > sudo systemctrol start nginx. service 21 | 22 | * You have at minimum a index.php file for testing to see if nginx server is working properly. 23 | 24 | * Have fireware software permissions set to allow access to the service. 25 | 26 | * Have PHP-FPM installed and properly running. 27 | 28 | ## Instructions for Using Setting Up Nginx to Serve Index.php files on Ubuntu 18.04 29 | 30 | 1. Copy the files from /etc/sites-available/default to a new folder named /etc/sites-available/your_site.com, substituting your domain for your_site.com. 31 | 32 | > sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/your_site.com 33 | 34 | 2. Edit the newly created file with the code in the repository labeled nginx_sitesavailable_yoursite.com using the nano text editor in the command line. 35 | 36 | > sudo nano /etc/nginx/sites-available/your_site.com 37 | 38 | 3. Make sure the default server is in the /etc/nginx/sites-enabled/ directory. 39 | 40 | > grep -R default_server /etc/nginx/sites-enabled/ 41 | 42 | 4. Create a symbolic link the new directory to the sites-enabled directory. 43 | 44 | > sudo ln -s /etc/nginx/sites-available/your_site.com /etc/nginx/sites-enabled/ 45 | 46 | 5. The /etc/nginx/conf.d/default.conf file is used to configure the default virtual host. Copy the default.conf to create a new file names yoursite.com.conf. 47 | 48 | > sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/yoursite.com.conf 49 | 50 | 6. Edit the nginx.conf file. Copy and paste the code in the repository into the newly created .conf file, changing yoursite.com to your own custom URL. 51 | 52 | > sudo nano /etc/nginx/nginx.conf.d/yoursite.com.conf 53 | 54 | 7. Restart the Nginx service 55 | 56 | > sudo systemctl restart nginx.service 57 | 58 | 8. Test the configuration file using this command. You should get a response stating that sytax is ok and configuration was successful 59 | 60 | > sudo nginx -t 61 | 62 | 9. Edit the nginx hosts file using the Nano text editor. Add your domain to the list under localhost. If you are unsure how to do this, please cut and paste the code in the repository with the file name nginx_hosts. 63 | 64 | > sudo nano /etc/hosts 65 | 66 | 8. It's a good idea to make sure file permissions are set to be able to write to your files, if you haven't already done so, the following commands will make sure all permissions are set for writing. Adjust setting based to your security needs. 67 | 68 | > sudo chmod 755 /var/www 69 | 70 | > sudo chmod 755 /var/www/your_site.com 71 | 72 | > sudo chmod 755 /var/www/your_site.com/html 73 | 74 | 9. Restart the nginx service 75 | 76 | > sudo systemctl restart nginx.service 77 | 78 | Your project should now be viewable at http://localhost/your_site.com/index.php 79 | 80 | If you do not see your application please check to make sure you have a index.php file first, then check the status of your nginx service. Also check to be sure you replaced all locations with your_site to your domain or project name. 81 | 82 | ## Troubleshooting Links and Other Helpful Nginx and LEMP Stack Resources 83 | 84 | * Nginx Download - https://nginx.org/en/linux_packages.html#stable 85 | * Official Nginx website: https://nginx.com 86 | 87 | * Nginx Documentation - Official Nginx documentation link: https://docs.nginx.com/?_ga=2.71462611.194525721.1536891075-1736526540.1536770284 88 | 89 | * 404 error help topic on Digital Ocean - https://www.digitalocean.com/community/questions/nginx-and-php-getting-error-404-with-scripts 90 | 91 | * Nginx.Io - Website to help with config files for Nginx - https://nginxconfig.io 92 | 93 | * Setup for PHP-FPM via Linode - https://linode.com/docs/web-servers/nginx/serve-php-php-fpm-and-nginx/ 94 | 95 | * www-data and file permissions help link on StackExchange - https://superuser.com/questions/646062/granting-write-permissions-to-www-data-group 96 | 97 | Spent a little time trying to figure this out so I thought I would share it. It is mainly for my re-use efforts, however feel free to make changes or make it better as needed. Hope it helps someone else out. Most of the tutorials on development are for the LAMP stack or use XAMPP or WAMPP, so I wanted to create some files for Nginx as I think it is a far better performing server then Apache. --------------------------------------------------------------------------------