How to create DO server to host Wordpress and Laravel both and optimize it fully?

March 21, 2017 565 views
WordPress Server Optimization Ubuntu 16.04

I am new to server management and till now I used a free service which installs server with one click for me and also installs wordpress in it.
Now I have two problems. They are retiring their free plan and even though I can still use their free plan till I disconnect my server, I want to learn How to optimize DO server for best performance.
Second Issue - I want to host Laravel apps as demo for my written tutorials which I will write in my portfolio wordpress blog and also host laravel apps as portfolio projects. But I don't know how to install laravel as Wordpress is currently installed.

After researching, I got to know that installing composer and creating laravel project in subfolder let's say homepage.com/demo/myapp should work. But I created my project in localhost and there I had to start the server everytime using "php artisan serve" to run the project. But in live servers how these works I don't know.

Any tutorial links or help will be appreciated.

Thanks

1 Answer

@parthp1808

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:

https://github.com/serveradminsh/installers

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.

cd /opt
git clone https://github.com/serveradminsh/installers.git
cd installers

Now you'll see a few directories -- for the purpose of this, we're only going to focus on nginx and php (you don't need NodeJS for PHP or Laravel).

So let's start with NGINX.

cd nginx
chmod +x installer.sh
./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.

cd php/7.1
chmod +x installer.sh
./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:

php -v

...

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

Replace yourdomain.ext with your actual domain. Now we just need to tweak that file to be specific to your domain instead of general.

nano /etc/nginx/sites/yourdomain.ext.conf

Find:

server_name _;

Replace that with

server_name yourdomain.ext www.yourdomain.ext;

Now find:

root /home/nginx/htdocs/public;

And set the path to the path where you want to store your public files, i.e. where index.php will be. For example:

root /home/yourdomain.ext/htdocs/public

Close and save the file. We just need to make one more change and that'd be to this file:

/etc/php/7.1/fpm/pool.d/www.conf

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.

nano /etc/php/7.1/fpm/pool.d/www.conf

Find:

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 :-).

  • @parthp1808

    As a side note, I just added the MariaDB 10.x installer to the repo, so much like you did above, all you need to do is:

    cd mariadb
    
    chmod +x installer.sh
    
    ./installer.sh
    

    You'll be prompted to set a root password for MariaDB. Please make sure it's secure. You'll then be prompted to answer a few questions at the end. You'll answer those like so, in order.

    • Enter the root password you just set.
    • Type n, hit enter.
    • Type y, hit enter.
    • Type `y', hit enter.
    • Type `y', hit enter.
    • Type `y', hit enter.

    ...

    And you're done. MariaDB (MySQL) is now installed. Currently, I don't have an optimized config for MariaDB included, though that's coming. It's hard to predict since configuration really depends on your RAM, though it's in the works.

  • Thanks for giving your time.
    Since long I had confusion between Apache and Nginx. Which is one better? Is there really any major performance difference between Apache and Nginx. It's like I am used to Apache things and its easy to find answers about Apache. I have no idea about Nginx so bit afraid to give it a try.
    I have seen many services claiming to optimize DO servers to maximum. Some are using Nginx, some are using Apache.

    I want to host multiple wordpress blogs and multiple Laravel projects so that means - mysql for wordpress and Postgresql and mysql for Laravel apps.

    Whats the best optimal configuration?

    1) Apache
    2) Nginx
    3) Apache + Nginx

    I am not expecting real-time more than 1000-2000 users any time soon.

    P.S - How is this setup :
    https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu-16-04-server

    Apache and Nginx are popular web servers, and you may find reasons to use both on the same server. This tutorial walks you through setting up web sites on Apache and Nginx and then proxying requests from Nginx on to Apache.
    • @parthp1808

      I much prefer NGINX over Apache due to performance and resource usage under heavier load. The NGINX configuration I've provided is already production-ready, so once it's done, it's ready to go -- you just need to configure your server blocks for your domains (and there are multiple examples included on how to do that) and restart NGINX.

      Apache on it's own isn't an issue, though when you couple mod_php with it, which is what's used to handle PHP requests typically, it can become a major resource hog due to the way that module is implemented.

      Beyond performance, NGINX syntax is much easier to work with. With Apache you have .htaccess files which are processed on each request, which can slow things down a bit as the file grows in size -- but the syntax is even more of a pain, IMO.

      The standard WordPress .htaccess file looks like this:

      # BEGIN WordPress
      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
      RewriteRule ^index\.php$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
      </IfModule>
      # END WordPress
      

      Now, to do the same thing in NGINX, inside our server block, we'll use this:

      location /
      {
          try_files $uri $uri/ /index.php?$args;
      }
      

      The above snippet is within a sever block, away from public access, unlike .htaccess, which needs to be in the web root (i.e. where index.php is).

      ...

      That being said, you can host multiple sites using either web server. The only real limit will be your resources (CPU, RAM, Disk).

      You can run MySQL/MariaDB and PostgreSQL on the same server, though I wouldn't really recommend it as both need decent resources. If you were going to run them both, I'd start with a Droplet with at least 2-4GB of RAM.

      As for using NGINX as a reverse proxy to Apache, it can work, though honestly, I would only recommend that if there's an absolute need for Apache to exist -- i.e. you're 100% sure that what you're trying to do simply can not be achieved with NGINX alone (I can't think of any specific thing off the top of my head).

      My reasoning for this is because when you add Apache behind NGINX, you're now adding to what you have to optimize, tweak, and tune. Instead of worrying about one web server, you're now worrying about two.

      As for the services that claim to optimize your server, it all depends on how you want to set up your server. The installers I've provided and am using myself already provide an NGINX configuration that is optimized. Optimizations for MariaDB/MySQL will be next. From what I have seen, most that claim to optimize your server don't actually do much as there's a wide variation between what one person will need and what the next person will need.

      There's really not a set in stone perfect configuration that just works for everyone. Anyone that tells you that there is, is lying. One person hosting a popular WordPress website may need optimizations to MariaDB/MySQL on a 4-12GB Droplet which would be too free with resources, thus said optimizations would cause a 1-2GB Droplet to crash (just one example).

Have another answer? Share your knowledge.