Running Octane on Servers0:00
Now that we know how to install and run Octane on our local machine, let's see how we may run it on servers. Similar to running it locally, we will need to ensure the Swoole PHP extension is installed and install Octane via Composer. We will also need to run the Octane server as a daemon on our VBS, and configure Nginx to serve our application through the Octane server instead of regular PHP FPM. So let's get started. We will first ssh into our VBS. For better understanding of what we are doing in this lesson, I recommend that you watch our Servers for Laravel Laracast course.
SSH and Install Octane0:37
For better understanding of what we are doing in this lesson, I recommend that you watch our Servers for Laravel Laracast course. It will help you understand how to run servers in production under Nginx and PHP FPM. All we will do in this lesson is convert the site to use Octane instead of PHP FPM. So make sure to watch the Servers for Laravel course for more information. Now we are inside the VBS, let's switch to the site system user and go pull from our git repository and install Octane via Composer. So sudo su laracast, we switch to the laracast user. Then we go into the site directory cd home/laracast/www. And we pull the latest commit.
Installing OpenSwoole Extension1:23
Then we go into the site directory cd home/laracast/www. And we pull the latest commit. So git pull origin main. Then let's install the Composer dependencies. composer install. Now we clear the terminal and install Octane by running php artisan octane install. Choose swoole from the options. And once it's done, we switch back to the root user to install the swoole PHP extension. We first add the openswoole APT repository. So apt install software-properties-common and apt-add-repository openswoole/ppa.
Creating Supervisor Daemon2:08
We first add the openswool apt repository. So apt install software-properties-common and apt repository openswool ppa. This command will take a few seconds to complete. So give it some time. And then once it's finished, we install openswool by running apt install php8.1 openswool. All done. Now let's go create the Octane daemon with supervisor. So cd /etc/supervisor/conf.d. And create a new file, sudo nano octane-laracasts.conf. Now inside the file, let's space the following block.
And create a new file, sudo nano octane laracasts.conf. Now inside the file, let's space the following block. This block defines a program called octane laracasts, which runs the octane start command. The command starts octane on port 9501. That's where we can access the octane server. The program is also configured to send logs coming from the octane server to a file named octane inside our storage/logs directory. The program runs under the laracasts system user. And the number of processes is set to one. Now let's save the file and go create the log file.
And the number of processes is set to one. Now let's save the file and go create the log file. So touch /home/laracasts/www/storage/logs/octane.log. Then we change the ownership. So chown laracasts:laracasts for the group as well. For /home/laracasts/www/storage/logs/octane.log. Now let's tell supervisor to re-read its configuration files by running supervisorctl re-read. And it says here that it has detected a new program called octane laracasts. Let's then update supervisor so it starts running this program. So supervisorctl update added process group.
Let's then update supervisor so it starts running this program. So supervisorctl update added process group. All looks good. Let's verify. So we run supervisorctl status octane laracasts. The asterisk here is for all processes under this program or under this group. We want to see the status of all the processes. We have a typo. This should be a dash. And here we go, the process group has one process running.
This should be a dash. And here we go, the process group has one process running. We have the octane server running on our VBS with no issues. And as we mentioned earlier, the swoole server will start multiple web workers and multiple task workers for handling web requests and concurrent tasks. These processes are managed by the swoole master process, which is managed by the octane process. And the octane process is now managed by supervisor. We just did that. If any of these processes crash, their manager will restart them back up. And if there are any issues with the octane process, you will see the logs in the octane.log
Configuring Nginx Proxy5:46
If any of these processes crash, their manager will restart them back up. And if there are any issues with the octane process, you will see the logs in the octane.log file that we just created. Now let's go configure nginx to serve our site through octane. Here I have the root route serve a JSON representation of the work state container binding. This binding won't be available if the application is running under php-fpm. If we check the browser, we can see an error, because the Laravel container doesn't know this binding. Let's now go configure our site to use octane. We will open the nginx configuration file, so sudo nano /etc/nginx/sites-available/laracasts.
Let's now go configure our site to use octane. We will open the nginx configuration file, so sudo nano /etc/nginx/sites-available/laracasts. And then we will go to our main server block. And update this location block here. Instead of falling back to running the index.php file, we will fall back to a named location called octane. So we change this to @octane. Next, we will hand the requests to the index.php file, which includes requests to the root route. We will handle them and point to the octane location block as well.
route. We will handle them and point to the octane location block as well. So try files. Give it a directory that doesn't exist. So not exists. And then at octane. This is a workaround to make nginx consider the octane location block when handling these kind of requests. Now let's go create this named location. So location octane.
Now let's go create this named location. So location octane. And inside the location, let's set a variable named suffix. So set suffix. And by default, this variable will be empty. Now let's check if the request is coming to the index.php file and set the value of the suffix variable to the query string. So if the URI equals index.php, we set the suffix variable to the query string. Now we will set a few headers so our octane server receives these headers as we would expect.
Now we will set a few headers so our Octane server receives these headers as we would expect. So first, we will set the host header to whatever is sent with the request. So we use proxy_set_header host and use the HTTP_HOST nginx variable. Then we set the request_scheme as sent to nginx. So proxy_set_header scheme equals $scheme. And then we set the server_port, proxy_set_header server_port equals the value of the $server_port variable. And the remote_address header as well, which is the client IP address. proxy_set_header remote_address equals $remote_address.
And the remote address header as well, which is the client IP address. proxy_set_header remote_addr equals remote_addr. And then we set the x-forwarded-for header by reading the proxy_add_x_forwarded_for nginx variable. So proxy_set_header x-forwarded-for. And we set it to this variable, proxy_add_x_forwarded_for. This variable includes the value of the visitor IP address in addition to any addresses included in the x-forwarded-for request header. This allows the Octane server to see the visitor actual IP address with all the forwarding that's happening.
This allows the octane server to see the visitor actual IP address with all the forwarding that's happening. I won't go through these steps in detail as it requires a lesson of its own. So for now the remaining part is setting the HTTP protocol version to 1.1. So proxy HTTP version 1.1. And then we pass the requests to the octane server running locally on port 9501. So proxy pass HTTP 127.0.0.1:9501 and then we append the suffix. Now let's remove this location block as it's no longer needed. It was used when we were using PHP-FPM, but we don't need it now. And then we save the file and reload nginx, sudo service nginx reload.
Reloading Nginx and Testing11:03
It was used when we were using php FPM, but we don't need it now. And then we save the file and reload nginx, sudo service nginx reload. We get an error, let's check the nginx status. Seems like we have a typo in the try_files directive. So let's cd into the /etc/nginx/sites-available and then edit the laracast file. Let's fix this typo here, try_files, save. Then we reload nginx, all looks good now. Now let's visit our website in the browser. Go with php.com. And here we go.
Go with php. And here we go. We can see the JSON dump of the WorkerState class. That means the application is running behind Octane. Perfect. I know some of these nginx configurations are quite confusing, but you don't really need to worry about them for now. You can find the recommended nginx configuration in the official Laravel documentation website. You may copy it as is and use it to run Octane. I'll add it to my list to go on a deep dive on nginx configurations in a future laracasts.
You may copy it as is and use it to run octane. I'll add it to my list to go on a deep dive on nginx configurations in a future laracasts course. But for now, that's the end of the octane course on laracasts. I hope to see you in the next course.
