When you’re running
php artisan serve, Laravel is using PHP’s built-in web server to serve your site. It’s not a production web server and only meant for local development, which would be why you need to start it each time you close out your terminal.
It’s not meant to be persistent like, for example, NGINX or Apache, and you definitely don’t want rely on that on a production server.
What I would recommend is setting up a LEMP Stack. That’d consist of Linux (Ubuntu), NGINX, PHP, and MariaDB (or MySQL). Ubuntu has existing repositories for each, though there are also multiple alternative repositories, including those maintained by the developers themselves (which is why I use when/where possible).
That said, I actually just released an initial release of some of the installers I use to handle setting up NGINX and PHP (PHP-FPM) which ease the process. The NGINX installer I use compiles NGINX from source and includes multiple configuration examples (including one for PHP) which can be used as a basis for your overall configuration.
You can find that here:
To use the installers, all you need to do is clone the repository to your server using
git. Since most don’t have it installed by default, you’ll probably need to run:
apt-get -y install git
Then change over to a directory of your choice for the clone. I generally use
/opt, so that’s what I’ll use for this example.
git clone https://github.com/serveradminsh/installers.git
Now you’ll see a few directories – for the purpose of this, we’re only going to focus on
php (you don’t need NodeJS for PHP or Laravel).
So let’s start with NGINX.
chmod +x installer.sh
That’s all you need to do to install NGINX. That auto-installer will handle a source compile for you and you won’t have to configure anything until it completes (it’ll take a little while – between 15-30 minutes). Even then, the only thing you need to configure is your server blocks for your website. The configuration is already optimized and ready to rock.
Now to install PHP and PHP-FPM (used by NGINX), you can change over to the
php directory and choose your preferred PHP version. I normally stick with 7.1, so that’s what I’ll use here.
chmod +x installer.sh
The correct repository will be added and the most common PHP packages will be installed, which should be all you need for your application unless you need very specific packages.
You can check your PHP version by running:
Inside of the
nginx directory, there’s an
examples directory and within that, a
php directory. That’s the server block configuration file you want to work with, so let’s copy that over and rename it to your domain.
cp examples/php/php.conf /etc/nginx/sites/yourdomain.ext.conf
yourdomain.ext with your actual domain. Now we just need to tweak that file to be specific to your domain instead of general.
Replace that with
server_name yourdomain.ext www.yourdomain.ext;
And set the path to the path where you want to store your public files, i.e. where
index.php will be. For example:
Close and save the file. We just need to make one more change and that’d be to this file:
We need to modify the listen directive so that it matches our NGINX configuration. By default, PHP uses sockets but our NGINX configuration uses TCP (preferred), so we need to fix that.
listen = /run/php/php7.1-fpm.sock
Change that to:
listen = 127.0.0.1:9000
Close and save. Now we need to restart both NGINX and PHP-FPM.
service php7.1-fpm restart
systemctl restart nginx
From there, the only other thing you need to do is install MariaDB/MySQL if needed for your app and then start creating databases, database users, etc so that you can connect. We can do that next (and I’ll have an installer for it uploaded soon to simplify things), but let’s make sure we get the above up and running for you first :-).