Giunsa ang Pag-install sa Nginx, MariaDB ug PHP (FEMP) Stack sa FreeBSD


Kini nga panudlo maggiya kanimo kung giunsa ang pag-install ug pag-configure sa FBEMP sa FreeBSD 11.x nga labing bag-ong pagpagawas. Ang FBEMP usa ka acronym nga naghulagway sa mosunod nga koleksyon sa software:

FreeBSD 11.1 Unix-like distribution, Nginx web server, MariaDB relational database management system (usa ka community fork sa MySQL) ug PHP dynamic programming language nga nagdagan sa server-side.

  1. Pag-instalar sa FreeBSD 11.x
  2. 10 ka Butang nga Buhaton Human sa FreeBSD Installation

Lakang 1: I-install ang Nginx Web Server sa FreeBSD

1. Ang unang serbisyo nga among i-install para sa among FBEMP stack sa FreeBSD mao ang web server, nga girepresentahan sa Nginx software.

Ang Nginx web server adunay mas daghang pre-complied nga mga pakete nga anaa sa FreeBSD 11.x PORTS. Aron makakuha og lista sa Nginx binaries gikan sa Ports repository, i-isyu ang mosunod nga mga command sa imong server terminal.

# ls /usr/ports/www/ | grep nginx
# pkg search -o nginx

2. Niini nga partikular nga configuration, atong i-install ang main package nga bersyon sa Nginx pinaagi sa pag-isyu sa ubos nga sugo. Ang pagdumala sa pkg package mangutana kanimo kung gusto nimo ipadayon ang pag-install sa nginx package. Tubag og oo (y sa command line) aron masugdan ang proseso sa pag-instalar.

# pkg install nginx

3. Human ma-install ang Nginx web server package sa imong system, ipatuman ang mosunod nga mga commands aron mahimo ang daemon system-wide ug magsugod sa serbisyo sa imong system.

# sysrc nginx_enable="yes"
# service nginx start

4. Sunod, gamit ang sockstat command, pamatud-i ang Nginx service network sockets, kung kini nagbugkos sa 80/TCP port, pinaagi sa pag-isyu sa ubos nga sugo. Ang output sa sockstat command ipa-pipe pinaagi sa grep utility aron makunhuran ang gibalik nga mga resulta lamang sa nginx string.

# sockstat -4 | grep nginx

5. Sa katapusan, ablihi ang browser sa desktop computer sa imong network ug bisitaha ang Nginx default web page pinaagi sa HTTP protocol. Isulat ang FQDN sa imong makina o imong domain name o ang IP address sa imong server sa URL nga gisang-at sa browser aron hangyoon ang Nginx web server default web page. Ang mensahe nga \Welcome sa nginx! kinahanglan nga ipakita sa imong browser, sama sa gihulagway sa ubos nga screenshot.

http://yourdomain.com
http://your_server_IP
http://your_machine_FQDN

6. Ang default nga webot nga direktoryo alang sa Nginx web content nga nahimutang sa /usr/local/www/nginx/ absolute system path. Niini nga lokasyon kinahanglan ka maghimo, kopyahon o i-install ang mga file sa sulud sa web, sama sa .html o .php nga mga file, para sa imong website.

Aron usbon kini nga lokasyon, i-edit ang nginx main configuration file ug usba ang root directive aron mapakita ang imong bag-ong webroot path.

# nano /usr/local/etc/nginx/nginx.conf

Dinhi, pangitaa ug i-update ang mosunod nga linya aron ipakita ang imong bag-ong webroot nga agianan:

root	/path/to/new/webroot;

Lakang 2: I-install ang PHP sa FreeBSD

7. Dili sama sa Apache HTTP server, ang Nginx walay kapabilidad sa natively process PHP code. Agig balos, ang Nginx web server nagpasa sa mga hangyo sa PHP sa usa ka tighubad sa PHP, sama sa php-fpm FastCGI daemon, nga nagsusi ug nagpatuman sa code. Ang resulta nga code ibalik dayon sa Nginx, nga nag-assemble pag-usab sa code balik sa gihangyo nga html format ug ipadala ang code ngadto sa bisita nga web browser.

Ang FreeBSD 11.x Ports repository nagtanyag og daghang binary nga mga bersyon para sa PHP programming language, sama sa PHP 5.6, PHP 7.0 ug PHP 7.1 nga pagpagawas. Aron mapakita ang tanang available nga pre-compiled PHP versions sa FreeBSD 11.x, padagana ang mga command sa ubos.

# pkg search -o php
# ls /usr/ports/lang/ | grep php

8. Mahimo nimong pilion nga i-install ang bisan unsang bersyon sa PHP nga imong makita nga labing angay alang sa aplikasyon sa web nga imong gipadagan sa imong sistema. Bisan pa, sa kini nga giya i-install namon ang labing bag-ong bersyon sa PHP.

Para ma-install ang PHP 7.1 release ug pipila ka PHP importanteng modules nga gikinahanglan para sa lain-laing web applications, padagana ang mosunod nga command.

# pkg install php71 php71-mysqli php71-mcrypt php71-zlib php71-gd php71-json mod_php71 php71-mbstring php71-curl

9. Human nimo ma-install ang PHP packages sa imong system, ablihi ang PHP-FPM configuration file para sa Nginx ug i-adjust ang user ug group values aron mohaum sa value sa Nginx runtime user, nga mao ang www. Una, paghimo og backup sa file gamit ang ubos nga sugo.

# cp /usr/local/etc/php-fpm.d/www.conf{,.backup}

Dayon, ablihi ang file ug i-update ang mosunod nga mga linya sama sa gipresentar sa ubos nga sample.

user = www
group = www

10. Usab, paghimo ug PHP configuration file nga gigamit alang sa produksyon pinaagi sa pag-isyu sa ubos nga sugo. Niini nga payl makahimo ka ug custom nga mga kausaban nga magamit sa PHP interpreter sa runtime.

# cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

Pananglitan, usba ang date.timezone setting para sa PHP interpreter aron ma-update ang pisikal nga lokasyon sa imong makina sama sa gipakita sa ubos nga pananglitan. Ang lista sa timezone sa PHP makita dinhi: http://php.net/manual/en/timezones.php.

# vi /usr/local/etc/php.ini

Idugang ang mosunod nga timezone (itakda ang timezone sumala sa imong nasud).

date.timezone = Europe/London

Mahimo usab nimong i-adjust ang ubang mga variable sa PHP, sama sa maximum nga gidak-on sa file sa gi-upload nga file, nga mahimong madugangan pinaagi sa pag-usab sa ubos nga mga bili:

upload_max_filesize = 10M
post_max_size = 10M

11. Pagkahuman, nahimo nimo ang naandan nga mga setting para sa PHP, pagpagana ug pagsugod sa PHP-FPM nga daemon aron magamit ang bag-ong mga pag-configure pinaagi sa pag-isyu sa mga mando sa ubos.

# sysrc php_fpm_enable=yes
# service php-fpm start

12. Sa kasagaran, ang PHP-FPM nga daemon sa FreeBSD mogapos sa lokal nga network socket sa port 9000/TCP. Aron ipakita ang PHP-FPM network sockets ipatuman ang mosunod nga sugo.

# sockstat -4 -6| grep php-fpm

13. Aron mapasa sa Nginx web server ang PHP scripts sa FastCGI gateway server, nga namati sa 127.0.0.1:9000 socket, ablihi ang Nginx main configuration file ug idugang ang mosunod nga block sa code sama sa gihulagway sa ubos nga sample.

# vi /usr/local/etc/nginx/nginx.conf

FastCGI code block alang sa nginx:

 location ~ \.php$ {
        root	/usr/local/www/nginx;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;    
        include        fastcgi_params;
        	}

14. Aron matan-aw ang kasamtangang impormasyon sa PHP para sa imong server, paghimo ug info.php file sa Nginx webot path pinaagi sa pag-isyu sa mosunod nga sugo.

# echo "<?php phpinfo(); ?>" | tee /usr/local/www/nginx/info.php

15. Dayon, sulayi ug i-restart ang Nginx daemon aron magamit ang PHP FastCGI settings ug bisitaha ang info.php page sa usa ka browser.

# nginx -t # Test nginx configuration file for syntax errors
# service nginx restart

Ilisan ang IP address o domain name sa ubos nga mga link sumala niana. Ang pahina sa impormasyon sa PHP kinahanglan magpakita sa impormasyon sama sa gihulagway sa ubos nga screenshot.

http://yourdomain.com/info.php
http://server_IP-or-FQDN/info.php

Lakang 3: I-install ang MariaDB sa FreeBSD

16. Ang katapusang component nga nawala sa imong FEMP stack sa database. Ang MariaDB/MySQL maoy usa sa labing kalambigitan nga open source nga RDBMS software nga adunay Nginx web server nga gigamit sa pag-deploy sa mga dinamikong website.

Sa tinuud, ang MariaDB/MySQL usa sa labing gigamit nga relational database sa kalibutan. Ang pagpangita pinaagi sa FreeBSD Ports, makit-an nimo ang daghang mga pagpagawas sa MariaDB/MySQL.

Niini nga giya atong i-install ang MariaDB database, nga usa ka komunidad nga tinidor sa MySQL database. Aron makapangita alang sa magamit nga mga bersyon sa MariaDB, i-isyu ang mosunod nga mga mando sa terminal.

# ls -al /usr/ports/databases/ | grep mariadb
# pkg search mariadb

17. Aron i-install ang pinakabag-o nga bersyon sa MariaDB database server ipatuman ang mosunod nga sugo. Kinahanglan mo usab nga i-install ang PHP relational database driver module nga gigamit sa PHP scripts alang sa pagkonektar sa MySQL.

# pkg install mariadb102-server php71-mysqli

18. Human ma-install ang database, pagana ang MySQL daemon ug sugdi ang serbisyo sa database pinaagi sa pagpadagan sa mosunod nga mga sugo.

# sysrc mysql_enable="YES" 
# service mysql-server start

19. Usab, siguroha nga imong i-restart ang PHP-FPM daemon aron ma-load ang extension sa driver sa MySQL.

# service php-fpm restart
20. On the next step, secure MariaDB database by launching mysql_secure_installation script. Use the below sample of the installation script in order to answer the questions. Basically, say yes (y) for all asked questions to secure the database and type a strong password for MySQL root user.
# /usr/local/bin/mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
 
Enter current password for root (enter for none):
OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
 ... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

21. Aron masulayan ang koneksyon sa database sa MariaDB gikan sa console, ipatuman ang sugo sa ubos.

# mysql -u root -p -e "show status like ‘Connections’"

22. Aron mas masiguro ang MariaDB, nga sa kasagaran maminaw alang sa umaabot nga mga koneksyon sa network sa 0.0.0.0:3306/TCP socket, i-isyu ang ubos nga sugo aron pugson ang serbisyo sa pagbugkos sa loopback interface ug hingpit nga dili tugotan ang remote access. Pagkahuman, i-restart ang serbisyo sa MySQL aron magamit ang bag-ong pagsumpo.

# sysrc mysql_args="--bind-address=127.0.0.1"
# service mysql-server restart

Tinoa kung ang localhost binding malampuson nga gigamit pinaagi sa pagpadagan sa netstat command sama sa gipakita sa ubos nga pananglitan.

# netstat -an -p tcp

Kana lang! Malampuson nimong na-install ang Nginx web server, MariaDB relational database ug PHP server-side programming language sa FreeBSD. Mahimo ka na karon magsugod sa paghimo og dinamikong mga panid sa web aron mag-alagad sa sulud sa web sa imong mga bisita.