no way to compare when less than two revisions
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Vorhergehende Überarbeitung Nächste Überarbeitung | |||
— | content:apache_phpfpm [2021/06/24 11:35] – [Howto setup a LAMP-Server in 2018-2020] Daniel | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | ====== Small Apache PHP- FPM- Serverguide ====== | ||
+ | |||
+ | |||
+ | ===== Howto setup a LAMP-Server in 2018-2020 ===== | ||
+ | |||
+ | This documentation is about how to set up a LAMP- Server (Linux, Apache, MySql, PhP) in the current, most stable way. The Reason for me writing this is, that default Installations of common Distruibutions are often based on an old way in Server- Configuration, | ||
+ | |||
+ | To get the Differences, | ||
+ | |||
+ | * Apache-Prefork. That way, Apache is one Application with many threads - which is slower, consumes more memory and doesn' | ||
+ | * PHP-Module integrated in the Apche- Server which is not released as stable and may crash the whole Apache- Server on Errors | ||
+ | * Using Network Connection to connect to | ||
+ | * MariaDB | ||
+ | |||
+ | And here is what this guide will set up: | ||
+ | |||
+ | * Apache- Event. That way, Apache is one small Apache- Manager- Application which will spawn as many Apache- Servers as needed to handle the incoming connections dynamically. This is stable, as one Error may only crash the one Instance, which will be respawned by the Apache- Manager dynamically | ||
+ | * Connection to php is done by Proxy- Handling in Apache | ||
+ | * PHP-FPM will also Spawn PHP-Instances dynamically for each script beeing run | ||
+ | * Using Socket Connections to connect to | ||
+ | * MariaDB | ||
+ | |||
+ | |||
+ | ==== Choosing Installation-Media & Install Basic System ==== | ||
+ | |||
+ | First, starting from Windows, you should make sure to have enough harddisk- space free (i recommend at least 60 GB). Then get the installation-medium of the Linux your choice. I will stick to OpenSuSE as to get from [[https:// | ||
+ | |||
+ | - Download the DVD-Image from the Webpage as described there | ||
+ | - Make the Installation- Media | ||
+ | - Install the System with standard Desktop- Packages (KDE) | ||
+ | - Boot into new Linux and Set Up Desktop as you like | ||
+ | |||
+ | |||
+ | ==== Basic System- Scaling thoughts ==== | ||
+ | |||
+ | The most important thing to consider when making performant LAMP is to **not overextend memory-usage** | ||
+ | |||
+ | The remaining Memory should be Split around this Values: | ||
+ | |||
+ | * 1/2 to Mysql | ||
+ | * 1/4 to PhP | ||
+ | * 1/4 to Apache | ||
+ | |||
+ | Those values are only for initial setup. After watching your System some time, you can adjust them to your needs. Mostly, when the system is growing, the Database will need even much more Memory than the Webserver, but that depens on your needs. | ||
+ | |||
+ | This is a very tight setup - having no more space for other Applications. So maybe if possible, spend some more GB and leave them free or dedicated to other things (e.g. In- Memory-DB like redis for special jobs). | ||
+ | |||
+ | Again in short: **Don' | ||
+ | |||
+ | ==== How to tell that your System is set up right ==== | ||
+ | |||
+ | As the System is still usable also with a bad configuration, | ||
+ | |||
+ | Open a terminal and type in " | ||
+ | |||
+ | If those values are both high, you can go and set swappiness to 0, which means that the system will tray to stay in memory as long as it will be possible. | ||
+ | |||
+ | Use " | ||
+ | |||
+ | ==== Install MAriaDB (MySql) ==== | ||
+ | |||
+ | In OpenSuSE MariaDB is available and working out of the Box (maybe change the Admin- Password at first Start). MySQL is tuneable in / | ||
+ | |||
+ | You should check the Parameters align with the memory of your machine (see above): Settings for 4 GB Memory (at all), are: | ||
+ | |||
+ | * innodb_buffer_pool_size = 768M | ||
+ | * innodb_log_file_size = 96M | ||
+ | * #Hint: innodb_log_file_size * 2/ | ||
+ | * max_connections = 24 | ||
+ | * join_buffer_size = 12M | ||
+ | * sort_buffer_size = 1M | ||
+ | * readn_rnd_buffer_size = 1M | ||
+ | |||
+ | Memory-Usage will be: innodb_buffer_pool_size + ( join_buffer_size + sort_buffer_size + readn_rnd_buffer_size ) * max_connections. If your System has more memory, use some tuning script (like MySQLTuner-perl) to see what makes most sense to put the memory to. | ||
+ | |||
+ | For a local setup, you should use Sockets and disable networking. To do this, set | ||
+ | |||
+ | < | ||
+ | socket = / | ||
+ | |||
+ | </ | ||
+ | |||
+ | You should than deactivate TCP/IP with " | ||
+ | |||
+ | Start Mysql with " | ||
+ | |||
+ | After that had worked you should secure your Database by giving a password as it was explained by the step above when starting it. If not shure just run "< | ||
+ | |||
+ | |||
+ | ==== Install Apache ==== | ||
+ | |||
+ | In SuSE 15.X the apache-prefork is installed by default as MPM, which means having one single Apache- Programm in memory. This is not very well scaleable (not multithreaded) and not very stable, as one hangig Request can stop the Server. | ||
+ | |||
+ | In modern setups, apache-event (which is the successor of the apache-worker MPM) is used. This is the most stable and best multithreaded webserver commonly used. If you experience problems with it, you can switch back to apache-worker, | ||
+ | |||
+ | To switch to that MPM: | ||
+ | |||
+ | * Open the Software Store | ||
+ | * Install apache-event | ||
+ | * Remove apache-prefork | ||
+ | * Commit the Changes | ||
+ | * in / | ||
+ | < | ||
+ | |||
+ | #This Config is for event or worker MPMs. | ||
+ | # | ||
+ | ServerLimit 31 | ||
+ | # http:// | ||
+ | StartServers | ||
+ | #This should be set to threadsperchild * serverlimit | ||
+ | MaxRequestWorkers | ||
+ | # http:// | ||
+ | MinSpareThreads | ||
+ | MaxSpareThreads | ||
+ | # number of worker threads created by each child process | ||
+ | # http:// | ||
+ | ThreadsPerChild | ||
+ | # maximum number of requests a server process serves | ||
+ | # http:// | ||
+ | MaxRequestsPerChild | ||
+ | #unsued Parameters, while not needed or obsolete | ||
+ | # | ||
+ | # | ||
+ | |||
+ | </ | ||
+ | |||
+ | I would suggest to remove any mpm-specific configurations and use only those settings. You can leave the other settings as defined by initial setup. | ||
+ | |||
+ | ==== Install PhP-FPM ==== | ||
+ | |||
+ | If installed, remove mod_php (see beneath)! The Module for apache is known to make it slow and instable - here we will set up PhP-FPM, which is much more stable and much faster. [[https:// | ||
+ | |||
+ | PhP-FPM is a Server for running the PhP-Instances in a controlled way. It will manage the maximum amount of running instances and take care of errors like hanging scripts. To get it: | ||
+ | |||
+ | * Uninstall mod_php and remove it from apache- starting parameters: | ||
+ | < | ||
+ | |||
+ | sudo zypper remove apache2-mod_php7 | ||
+ | sudo a2dismod php7 | ||
+ | |||
+ | </ | ||
+ | |||
+ | * I would suggest using a newer Version of php-fpm than in the default Repositories. E.g. using the Version of Repository " | ||
+ | * Either install the new Version with 1-Click-Install there **or** | ||
+ | < | ||
+ | |||
+ | sudo zypper install php7-fpm | ||
+ | |||
+ | </ | ||
+ | |||
+ | * Copy the configuration-files for php-fpm: | ||
+ | |||
+ | < | ||
+ | sudo cp / | ||
+ | sudo cp / | ||
+ | |||
+ | </ | ||
+ | |||
+ | * Than go to / | ||
+ | * Explanation: | ||
+ | |||
+ | The " | ||
+ | |||
+ | * pm = dynamic | ||
+ | * pm.max_children = 120 | ||
+ | * pm.start_servers = 12 | ||
+ | * pm.min_spare_servers = 6 | ||
+ | * pm.max_spare_servers = 18 | ||
+ | |||
+ | ==== Using Sockets ==== | ||
+ | |||
+ | Whenever you can - you should use unix sockets instead of TCP/IP, because of less overhead. If you are on the same machine (apache and php-fpm), than you can. | ||
+ | |||
+ | So this here is new for the setup: in "/ | ||
+ | |||
+ | < | ||
+ | listen = / | ||
+ | listen.owner = wwwrun | ||
+ | listen.group = www | ||
+ | listen.mode = 0660 | ||
+ | |||
+ | </ | ||
+ | |||
+ | You need to make the file be created by systemd, so create a file / | ||
+ | |||
+ | < | ||
+ | d / | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ==== PHP Configuration ==== | ||
+ | |||
+ | I do not recommend using php.ini in / | ||
+ | |||
+ | After that, start php-fpm: | ||
+ | |||
+ | < | ||
+ | sudo systemctl start php-fpm | ||
+ | sudo systemctl enable php-fpm | ||
+ | |||
+ | </ | ||
+ | |||
+ | and check, if the socket-file has been created. | ||
+ | |||
+ | ==== About PHP- Modules ==== | ||
+ | |||
+ | many modules for PHP are offered in the Distrubution. I **would not recommend using those** | ||
+ | |||
+ | Better use pearl / pecl and install modules with it! Here, i have found no other way, than to search for php-pear and php-pecl in the distribution and use them. | ||
+ | |||
+ | For me, i needed: php-pear, php-pecl, php7-devel (for command phpize) | ||
+ | |||
+ | Which can be found in the Repo: [[https:// | ||
+ | |||
+ | After that, modules can be installed by e.g. "pecl install imagick" | ||
+ | |||
+ | Restart php-fpm for the changes and check the log of php-fpm (usually in / | ||
+ | |||
+ | ==== Tell Apache to use php-fpm ==== | ||
+ | |||
+ | For making Apache use php-fpm as php-server, you use the module " | ||
+ | |||
+ | Caution: this has noting to do with " | ||
+ | |||
+ | * To enable this and all its dependencies, | ||
+ | < | ||
+ | |||
+ | sudo a2enmod setenvif rewrite proxy proxy_fcgi | ||
+ | |||
+ | </ | ||
+ | |||
+ | Now, tell proxy_fcgi to use php: | ||
+ | |||
+ | * Create / | ||
+ | |||
+ | < | ||
+ | # Don't use " | ||
+ | # This is to forward all PHP to php-fpm | ||
+ | < | ||
+ | | ||
+ | </ | ||
+ | |||
+ | # Don't use " | ||
+ | # <Proxy fcgi:// | ||
+ | < | ||
+ | #6 Hours = 21600 | ||
+ | #Make this high, as upload will stop after that amount of time | ||
+ | ProxySet connectiontimeout=30 timeout=21600 | ||
+ | </ | ||
+ | |||
+ | # If the php file doesn' | ||
+ | # This will allow .htaccess rewrite rules to work and | ||
+ | # the client will see the default 404 page of Apache | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Start and check Apache ==== | ||
+ | |||
+ | Now you can start and enable apache2 | ||
+ | < | ||
+ | |||
+ | sudo systemctl start apache2 | ||
+ | sudo systemctl enable apache2 | ||
+ | |||
+ | </ | ||
+ | |||
+ | check if the modules have beend loaded: | ||
+ | |||
+ | < | ||
+ | apache2ctl -M | ||
+ | |||
+ | </ | ||
+ | |||
+ | This should include proxy_fcgi_module now. | ||
+ | |||
+ | ==== Create a PHP- Test- File ==== | ||
+ | |||
+ | Create the File / | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | |||
+ | // Show all information, | ||
+ | phpinfo(); | ||
+ | |||
+ | ?> | ||
+ | |||
+ | </ | ||
+ | |||
+ | Now open your Web- Browser and go to: [[http:// | ||
+ | |||
+ | This should give you the complete Info of your php-configuration. If something fails, check if the above services are started an/or the logfiles. | ||
+ | |||
+ | In Production, you should not run a plain http-server, | ||
+ | |||
+ | Well: You are done. Now its up to you to fill Apache with content. Have fun! | ||
+ | |||
+ | |||
+ | ==== Manage Database with phpMyAdmin ==== | ||
+ | |||
+ | To manage your local Database, it would be nice to have phpMyAdmin installed first (via Package-manager). After that, copy the config.sample.inc.php to config.inc.php under / | ||
+ | |||
+ | You can finish the setup of your phpMyAdmin by visiting [[http:// | ||
+ | |||
+ | |||
+ | ===== Installing Eclipse ===== | ||
+ | |||
+ | Get Eclipse with PDT here: [[https:// | ||
+ | |||
+ | You may download the file, extract the contents (e.g. to ~/eclipse) and run the installer there in userspace (no superuser is required). | ||
+ | |||
+ | |||
+ | ===== Filling Content to your Server ===== | ||
+ | |||
+ | This is a demo to install some small Software to your Server. I will use the github- Project [[https:// | ||
+ | |||
+ | |||
+ | ==== Install git-web ==== | ||
+ | |||
+ | This is a nice tool, to view your git-repositorys. Install it, and create a directory named /srv/git and make it writeable to users. Restart your webserver. | ||
+ | |||
+ | You should already be able to go to [[http:// | ||
+ | |||
+ | ==== Download some Project ==== | ||
+ | |||
+ | To get the git-project, | ||
+ | |||
+ | You should already be set to open the project in eclipse. If you rightclick on the Project, you can add Composer-support to automate the installation of composer-modules while setup of ep3. | ||
+ | |||
+ | Than create the Apache configuration to point to that directory: | ||
+ | < | ||
+ | |||
+ | Alias /ep3 "/ | ||
+ | < | ||
+ | require local | ||
+ | Options FollowSymLinks | ||
+ | AllowOverride All | ||
+ | DirectoryIndex index.php | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | After that, follow the instructions in [[https:// | ||
+ | |||
+ | As Database you can create a new user (e.g. named ep3) with corresponid database and fill the configuration to fit. | ||
+ | |||
+ | After that, your testinstallation should work already. | ||
+ | |||