Lecture
Laravel is a modern, open source PHP framework for web developers. It is designed to provide a simple, elegant way for developers to get a full-featured web application that works quickly.
In this guide, we will discuss how to install Laravel on Ubuntu 16. We will use Nginx as our web server and will work with the latest version of Laravel at the time of this writing, version 5.3.
The first thing we need to do to get started with Laravel is to set up a stack that will support it. We can do this through the default Ubuntu repositories.
First, we need to update our local package index to make sure that we have a fresh list of available packages. Then we can install the necessary components:
sudo apt-get update sudo apt-get install nginx php7-fpm php7-cli php7-mcrypt git
This will allow Nginx to be installed as our web server along with the PHP tools needed to run the Laravel code itself. We also install git
since the composer
tool, PHP dependency manager, which we will use to install Laravel, will use it to download packages.
Now that we have installed our components, we can begin to customize them. We will start with PHP.
The first thing we need to do is open the main PHP configuration file for a PHP-FPM processor that uses Nginx.
View this with sudo privileges in a text editor:
sudo nano /etc/php/7.0/fpm/php.ini
We only need to change one value in this file.
Search by cgi.fix_pathinfo
parameter. it will be commented out the set to "1".
We need to uncomment it and set it to "0":
cgi.fix_pathinfo=0
This tells PHP not to attempt to execute such a script with a name if the name of the requested file cannot be found. This is very important, because allowing this type of behavior can allow an attacker to make a specially designed request to try to trick PHP into executing code that he shouldn’t.
When you're done, save and close the file.
The last part of the PHP administration that we need to do is explicitly enable the Mcrypt extension, which Laravel depends on. We can do this using the php5enmod
command, which allows us to easily include additional modules:
sudo php5enmod mcrypt
Now we can restart php5-fpm
service to implement the changes we made:
sudo service php5-fpm restart
Our PHP is now fully configured and we can move on.
The next item we have to solve is a web server. This will actually include the next two steps.
The first step is to configure the document root and directory structure that we will use to store the Laravel files. We will place our files in a directory called /var/www/laravel
.
At this time, only the top level of this path ( /var
) is created. We can create all the way in one step by passing the -p
flag to our mkdir
command. This instructs the utility to create any necessary parent path elements necessary to build a specific path:
sudo mkdir -p /var/www/laravel
Now that we have space allocated for the Laravel components, we can proceed to editing the blocks of the Nginx server.
Open the default server block configuration file with sudo privileges:
sudo nano /etc/nginx/sites-available/default
After installation, this file will have quite a few explanatory comments, but the basic structure will look like this:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.htm; server_name localhost; location / { try_files $uri $uri/ =404; } }
The first thing we need to change is the location of the document root.
Laravel will be installed in the /var/www/laravel
directory we created.
However , the base files that are used to trigger the application are stored in a subdirectory inside this called public
.
Here we set our document root. In addition, we will tell Nginx to process any index.php
files before looking for their HTML counterparts when requesting the directory location:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/laravel/public; index index.php index.html index.htm; server_name localhost; location / { try_files $uri $uri/ =404; } }
Next, we need to set the server_name
directive to refer to the actual domain name of our server. If you do not have a domain name, feel free to use the IP address of your server.
We also need to change the way Nginx will handle requests. This is done using try_files
directives. We want to try to process the request as a file first. If he cannot find the file with the correct name, he should try to process the default index file for the directory that matches the request. Otherwise, it should send a request to the index.php
file as a request parameter.
The changes described above can be implemented as follows:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/laravel/public; index index.php index.html index.htm; server_name server_domain_or_IP; location / { try_files $uri $uri/ /index.php?$query_string; } }
And finally, we need to create a block that handles the actual execution of any PHP files. This will apply to all files that end with .php
. It will try to execute the file itself, and then try to pass it as a parameter in the index.php
file.
We will install the fastcgi_*
directives so that the request path correctly fastcgi_*
up the execution, and make sure that Nginx uses a socket that php7-fpm
uses for communication and that the index.php
file is used as an index for these operations.
Then we set the SCRIPT_FILENAME
parameter so that PHP can find the required files correctly. When we are done, the completed file should look like this :
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/laravel/public; index index.php index.html index.htm; server_name server_domain_or_IP; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { try_files $uri /index.php =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Save and close the file when you're done.
Since we have changed the default
file of the server block, which is already enabled, we just need to restart Nginx for our configuration changes to be matched:
sudo service nginx restart
Before you go about installing the composer and Laravel, it may be a good idea to enable some paging on your server, so that the build is completed correctly. This is usually necessary only if you are working on a server without special memory (like 512mb of a droplet).
The swap space will allow the operating system to temporarily move data from memory to disk when the amount of information in memory exceeds the physical memory space. This will prevent your applications or systems from failing with an out-of-memory (OOM) exception when performing resource-intensive memory tasks.
We can very easily set up some swap space to allow our operating system to shuffle some of this to disk if necessary. As mentioned above, this is probably necessary only if you have less than 1 GB of RAM available.
First, we can create an empty 1 GB file by typing:
sudo fallocate -l 1G /swapfile
We can format it as a swap partition by typing:
sudo mkswap /swapfile
And finally, we can enable this space so that the kernel starts using it by typing:
sudo swapon /swapfile
The system will only use this space until the next reboot, but the only time that the server is likely to exceed its free memory during the build process, so this should not be a problem.
Now we are finally ready to install Composer and Laravel. We will set up Composer first. After that we will use this tool to process the Laravel installation.
Move to the folder where you have write access (for example, your home directory), and then download and run the installation script from the Composer project:
cd ~ curl -sS https://getcomposer.org/installer | php
This will create a file named composer.phar
in your home directory. This is a PHP archive and can be run from the command line.
We want to install it in a globally accessible place, though. In addition , we want to change the name of the composer
(without the file extension). We can do it in one step by typing:
sudo mv composer.phar /usr/local/bin/composer
Now that you have installed the composer, we can use it to install Laravel.
Remember that we want to install Laravel in the /var/www/laravel
directory. To install the latest version of Laravel, you can enter:
sudo composer create-project laravel/laravel /var/www/laravel
At the time of this writing, the latest version is 4.2. In the event that future project changes prevent this installation procedure from completing correctly, you can force the version we use in this guide instead of typing:
sudo composer create-project laravel/laravel /var/www/laravel 4.2
Now the files are all installed in our /var/www/laravel
directory, but they are fully owned by our root
account. The web user needs a partial owner and permission in order to properly serve the content.
We can give group ownership of our Laravel directory structure to a web group by typing:
sudo chown -R :www-data /var/www/laravel
Next, we can change the permissions to the /var/www/laravel/app/storage
directory to allow permissions to write to the web group. This is necessary for the application to function correctly:
sudo chmod -R 775 /var/www/laravel/app/storage
You now have Laravel fully installed and ready to go. You can see the default landing page by visiting your server’s domain or IP address in a web browser:
http://server_domain_or_IP
Now you have everything you need to start building apps with a Laravel framework.
You should now have Laravel and running on your server. Laravel is quite flexible in structure and includes many tools that can help you create an application in a structured way.
To learn how to use Laravel to create an application, check the Laravel documentation.
https://laravel.com/docs/5.3
Stumble upon the pitfalls in the configuration and operation of the web server is very easy. But it is difficult to understand the reason for incorrect or not always correct / erroneous work, if all the rules are followed.
There is nothing wrong with placing a root directory inside a location . But if the location does not match, then it will not have access to the root directory. Correct to do so:
server { server_name www.somesite.com; root /var/www/nginx-default/; location / { # [...] } location /foo { # [...] } location /bar { # [...] } }
# Specifying root inside the server section
No need to produce a large number of index directives. Register it once in the http block:
http { index index.php index.htm index.html; server { server_name www.somesite.com; location / { # [...] } } server { server_name somesite.com; location / { # [...] } location /foo { # [...] } } }
# Index will automatically be inherited in all sections.
You know that if = evil? When using the directive, you need to be careful, it is easy to make a mistake. So if possible, avoid using if.
Suppose your site lies on the domain somesite.com and you redirect to it the users who go to www.somesite.com:
server { server_name somesite.com *.somesite.com; if ($host ~* ^www\.(.+)) { set $raw_domain $1; rewrite ^/(.*)$ $raw_domain/$1 permanent; } # [...] } }
# Checks and redirects host
Here are a few problems. Home - if. Regardless of the host request (with or without www), Nginx still checks if. For each request. Instead, you can do this:
server { server_name www.somesite.com; return 301 $scheme://somesite.com$request_uri; } server { server_name somesite.com; # [...] }
# $ Scheme is used, which is suitable for http and https
Do not use if to check for a file:
server { root /var/www/somesite.com; location / { if (!-f $request_filename) { break; } } }
# Approach at least not effective
In return, Nginx has a try_files directive:
server { root /var/www/somesite.com; location / { try_files $uri $uri/ /index.html; } }
# Check the sequence for the presence of the file, if it does not exist, then sends it to index.html
It is noteworthy that the directive can also be used to protect the web server from unauthorized access.
If Nginx redirects all requests ending in .php directly to the PHP interpreter, then the attackers are able to execute third-party code. PHP by default tries to guess where the wrong query should lead. So first of all it is necessary to correct php.ini , having specified:
cgi.fix_pathinfo=0
# The interpreter will process only valid requests.
Pay attention to the correct configuration of Nginx:
# Перенаправляет запросы только для указанных файлов location ~* (file_a|file_b|file_c)\.php$ { fastcgi_pass backend; # [...] } # Отключить выполнение скриптов в пользовательских загрузках location /uploaddir { location ~ \.php$ {return 403;} # [...] } # Фильтрация при помощи try_files location ~* \.php$ { try_files $uri =404; fastcgi_pass backend; # [...] } # Использует вложенное расположение для фильтрации location ~* \.php$ { location ~ \..*/.*\.php$ {return 404;} fastcgi_pass backend; # [...] }
# Parameters can be combined
Incorrect indication of the paths for placing scripts in the FastCGI part leads to the error "Primary script unknown" , which is easily solved.
Use the $ request_uri variable to change the request URI:
rewrite ^ http://somesite.com $request_uri? permanent; # Или так return 301 http://somesite.com$request_uri;
# Redirects to page 301
Add a missing http: // is very simple:
rewrite ^ http://somesite.com permanent;
# Automatically complements the request
Do not redirect all requests for PHP in this form:
server { server_name _; root /var/www/site; location / { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/tmp/phpcgi.socket; } }
# Transfers everything to phpcgi.socket
Use the same try_files directive:
server { server_name _; root /var/www/site; location / { try_files $uri $uri/ @proxy; } location @proxy { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/tmp/phpcgi.socket; } }
# Sends only the necessary requests to the proxy
Or so:
server { server_name _; root /var/www/site; location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/tmp/phpcgi.socket; } }
# If Nginx cannot process the requested URI itself, then check directories for availability, then transfer to proxy
The main reason for the erroneous operation of the system (not only Nginx) is thoughtless copy-paste. Check configs, test the application before rolling out, read the documentation.
Here we are with you and got to the most interesting.
Although this is the most interesting fact, it has already ended, because we have almost everything set up and ready. It remains only to enter a couple of commands in the console and that's it.
So, we start the console: Press the Win + R keys, enter the cmd command and click OK.
For further manipulations with the command line, it will be nice to remember a few small tips.
> C: > cd Web \ www \
Now we need to install the composer.phar file in the project directory. Install the command line as described above and enter the command:
php -r "eval ('?>' .file_get_contents ('http://getcomposer.org/installer'));"
Checking that the composer.phar file should appear in our www directory. If it is not, then at some of the stages you made a mistake. If the file is present, then continue.
Now enter the command:
composer create-project laravel / laravel --prefer-dist
We are waiting for Composer to download and install all the Laravel components.
This completes the installation. We only need to check its performance.
If you did everything according to these instructions, then simply go to the browser at the address: http: // localhost / laravel / public /
Comments
To leave a comment
Running server side scripts using PHP as an example (LAMP)
Terms: Running server side scripts using PHP as an example (LAMP)