Wednesday, August 1, 2012

Optimized LEMP Installer for Debian/Ubuntu with Virtual Hosts Automator.

LAMP stacks are used widely but they are notorious for being less resource friendly and in the same time, incredibly attracting to script kiddies and part time crackers.

There's a tradition to play with most popular toys rather than one which was designed to be small and less expensive. As a kid, what would you like to steal in a kindergarten: a Spider-Man toy or a small spider miniature?
Leaving aside these arguments, let's move on to the real-valued part. I wrote this script which I think, has the following featured jobs:

  • Configures Nginx for performance and static file caching as well as gzipping the output.
  • Installs and PHP-FPM as well as PHP-APC for Opcode Caching
  • Optimizes MySQL for caching queries, while being light on resources.
  • Facilitates interactive virtual hosts creation with dedicated PHP-FPM pool for each user so that each PHP process runs as the owner. This eliminates the need to install FTP server for updating Wordpress or installing plugins. Since, PHP runs as the user owning the script, "wp-config" can be set to very strict permissions like 700.

There's even more...

  • Virtual hosts generation script also has the ability to create MySQL user and database as well as allowing user to import SQL file onto the newly created database for easy migration.
  • PHPMyAdmin installer is also built-in which allows PHPMyAdmin to be installed on specified domain or sub-domain. 
  • This script uses unix sockets instead of TCP which promises some more improvement and lowers TCP overhead within the system. 
  • I have implemented some security practices like "cgi.fixpath_info=0". 
Download the script here and run it as root:
wget https://raw.github.com/aatishnn/lempstack/master/lemp-debian.sh
chmod +x lemp-debian.sh
./lemp-debian.sh
This script works for me but I cannot guarantee that it will work for you too. I am not a Bash expert. Please add some regexp to your brain to ignore my scripting laziness. This is designed to work with Debian and Ubuntu. You may freely use it to suit your needs under the condition that you will thank me. I would be glad if you can share something about this script, test it, debug it or just scold, rebuke or criticize.


8 comments:

  1. Thank you for your nice script!

    I have one problem. Only the first vhost I made trough setup-vhost has a corresponding php5-fpm-hostname.sock. The following vhosts give me a 502 response.

    In /var/log/nginx/hostname.tld-error.log:

    2012/08/09 15:29:40 [crit] 1285#0: *12 connect() to unix:/var/run/php5-fpm-hostname.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.1.33, server: hostname.tld, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm-hostname.sock:", host: "www.hostname.tld"

    ReplyDelete
    Replies
    1. Head over to your issue at Github. I fixed a typo. It should work now.

      Delete
  2. Yes. It should. You should try it on a test server and share the results.

    ReplyDelete
  3. I've tried but is stops before installing mysql...
    "Use setup-vhost to configure virtual hosts.
    Running mysql_secure_installation. Use root password if set during install time.
    Press [Enter] key to continue after reading the above line ...
    ./lemp-debian.sh: line 215: mysql_secure_installation: command not found
    "

    ReplyDelete
    Replies
    1. Was MySQL installed correctly? Run it like this:
      ./lemp-debian.sh|tee log.txt

      and post the output.

      Delete
  4. Looks like an awesome script - I've also built one for CentOS / RHEL that runs MariaDB as compared to MySQL. Check it out if you're curious: http://drewsymo.com/web-servers/baller-nginx-php-fpm-apc-mysql-install-script/

    ReplyDelete
  5. 06:18:24 p.m. - flesoft: Installation done.
    06:18:24 p.m. - flesoft: Use setup-vhost to configure virtual hosts.
    06:18:24 p.m. - flesoft: Running mysql_secure_installation. Use root password if set during install time.
    06:18:24 p.m. - flesoft: Press [Enter] key to continue after reading the above line ...
    06:18:24 p.m. - flesoft: ./lemp-debian.sh: line 225: mysql_secure_installation: command not found
    06:20:12 p.m. - flesoft: ja falle

    ReplyDelete