In the above three articles, we have installed and configured the system, configured the yum Package Manager, and installed several common tools. In this article, we will introduce how to build the lnmp environment. the lnmp environment here refers to setting up nginx + MySQL + PHP in Linux.
Nginx. nginx is a high-performance HTTP and reverse proxy server and an IMAP/POP3/SMTP proxy server. Nginx can be used not only as a Web server, but also as a server Load balancer. You can take a look at this article.
MySQL is an open-source and free data software. MySQL is a small relational database management system that features a small size, fast speed, and low total cost of ownership, especially open source code, many small and medium websites choose MYSQL as their website database to reduce their total cost of ownership.
PHP is short for Hypertext Preprocessor. PHP is an embedded HTML language. It is a scripting language that executes HTML documents embedded on the server. It is widely used in a style similar to C language.
Nginx current latest stable version is nginx-1.0.13
First, download nginx and run the following command in Linux:
CD/usr/src # general software source code in this directory wget http://nginx.org/download/nginx-1.0.13.tar.gz # download
Nginx has several dependency packages. First, install the dependencies. Do not report errors during installation:
yum -y install zlib-devel pcre-devel openssl-devel
Generally, the installation of source code is divided into four steps (some may skip the first step), decompress (tar command), pre-compile (execute configure under the source package), compile (make ), make install)
First, decompress the source package:
tar -zxvf nginx-1.0.13.tar.gz
In the following example, zrepresents gzip(.gz file after gzip(.gz file) X represents pressurization, V represents display details, and-F uses archive files or devices (required parameter)
Then we perform the pre-compilation. Generally, the pre-compilation includes some parameters that have achieved the desired effect. For example, to enable a function, disable a function:
Go to the source package directory for pre-Compilation:
CD nginx-1.0.13. /configure -- prefix =/usr/local/nginx \ # specify the installation directory as/usr/local/nginx -- With-OpenSSL =/usr/include/OpenSSL \ # enable SSL -- -PCRE \ # enable regular expressions -- with-http_stub_status_module # install programs that can view nginx status
./Configure indicates the configure file under the current directory.
After the pre-compilation is complete, we can compile and install:
Make # compile
After the make command is executed, a large amount of output will be generated. If no error is reported after the output is complete, you can install and execute the command:
Make install # Install
After the installation is complete, you can view the installation files in the corresponding directory:
ls /usr/local/nginx/conf html logs sbin
Now let's start nginx:
/usr/local/nginx/sbin/nginx
Run the following command to check whether nginx is successfully started and nginx occupies port 80 of TCP:
netstat -antlp ¦ grep 80tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5946/nginx
Check that port 80 is open.
Then open the browser and access http: // 192.168.3.120. We will see welcome to nginx (the previous version is it's work ):
After nginx installation, We will install MySQL, we use the MySQl-5.0.95 version first download:
wget http://dev.mysql.com/get/Downloads/MySQL-5.0/mysql-5.0.95.tar.gz/from/http://mysql.cdpa.nsysu.edu.tw/
Before installation, we should make some preparations,
Install dependency:
yum -y install ncurses-devel
Create a MySQL user:
Useradd-m-S/sbin/nologin MySQL #-M does not create the home directory, and-s specifies that shell is not logged on
Then install:
Tar-zxvf mysql-5.0.95.tar.gzcd mysql-5.0.95. /configure -- prefix =/usr/local/MySQL \ -- without-Debug \ # cancel debugging mode to improve performance -- With-extra-charsets = utf8, GBK \ # only specify the required default character set to Improve the Performance -- enable-javaser \ # Use the Assembly mode to Improve the Performance -- With-mysqld-ldflags =-all-static \ # increase the compilation speed in static mode performance -- With-client-ldflags =-all-static \ -- With-Unix-socket-Path =/tmp/MySQL. sock \ # Use UNIX socket to improve performance -- With-sslmakemake install
Copy the configuration file and startup script after installation:
CP support-files/my-medium.cnf/etc/My. CNF # copy the configuration file CP support-files/MySQL. server/etc/init. d/mysqld # copy the startup script chmod + x/etc/init. d/mysqld # grant the script execution permission
To facilitate a soft connection between all binary executable files and dynamic link library files:
Ln-S/usr/local/MySQL/bin/*/usr/local/bin/# perform soft connections for executable binary files ln-S/usr/local/MySQL/lib/ mySQL/lib */usr/lib/# make a soft connection for the Dynamic Link Library
Then we initialize the database:
Mysql_install_db -- user = MySQL # use a MySQL user to install a database
To make MySQL work normally, we need to change the owner and group of the MySQL installation directory and the MySQL database directory:
Chown-r root. mySQL/usr/local/MySQL/# change the root directory and Its group is mysqlchown-r MySQL. mySQL/usr/local/MySQL/var/# change the owner and group of the database directory to MySQL
Here, the-R parameter is used to apply to all subdirectories and files.
After the configuration is complete, start MYSQL:
service mysqld start
Now we can check whether MySQL is successfully started, MySQL occupies TCP port 3306, and check whether the port is occupied:
netstat -antlp ¦ grep 3306tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 32143/mysqld
Then we use the MySQL command to connect to MySQL:
mysql
The following content is displayed, indicating that MySQL has been successfully started and connected.
Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.0.95-log Source distributionCopyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
After installing MySQL, We will install PHP. install several source code packages before installing PHP:
Libmcrypt mhash mcrypt
First install several source code package dependencies:
Wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.bz2/downloadtar-jxvf libmcrypt-2.5.8.tar.bz2 # this package is bz2 using the-J parameter to unzip the CD libmcrypt-2.5.8. /configuremakemake install ##################################### ############## wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2/downloadtar-jxvf mhash-0.9.9.9.tar.bz2cd mhash-0.9.9.9. /configuremakemake install # after the two packages are installed, make a soft connection to the Dynamic Linked Library to/usr/lib, the next mcrypt depends on the two packages ln-S/usr/local/lib/libmcrypt */usr/libln-S/usr/local/lib/libmhash. */usr/lib/ln-S/usr/local/bin/libmcrypt-config/usr/bin/libmcrypt-config ############# ######################################## ###### wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/downloadtar-zxvf mcrypt-2.6.8.tar.gzcd mcrypt-2.6.8. /configuremakemake install
Then download PHP:
wget http://cn2.php.net/get/php-5.4.0.tar.bz2/from/this/mirror
Install dependency:
yum –y install libxml2-devel curl-devel libpng-devel openldap-devel
We use the FPM method when using nginx to call PHP, and added support for PHP-FPM in PHP 5.4, so we do not need to install patches. install PHP:
tar -jxvf php-5.4.0.tar.bz2cd php-5.4.0./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql/ --with-zlib --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --with-curl --with-curlwrappers --enable-fpm --enable-fastcgi --with-mcrypt --with-gd --with-openssl --with-mhash --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc -enable-zip --enable-soapmakemake install
The entire lnmp has been installed. Next we will configure PHP and nginx to run the PHP Website:
First, create a configuration file for PHP:
Cp php. ini-production/usr/local/PHP. INI # copy PHP for development. ini-developmentcp/usr/local/PHP/etc/php-fpm.conf.default/usr/local/PHP/etc/php-fpm.confln-S/usr/local/PHP/bin/PHP/usr/bin/
Configure PHP-FPM, edit php-fpm.conf
vi /usr/local/php/etc/php-fpm.conf
Find the line of listen and modify it to the following content:
Listen =/var/run/PHP-FPM/php-fpm.sock # Use UNIX socket
Start PHP-FPM
mkdir /var/run/php-fpm/usr/local/php/sbin/php-fpm
Configure nginx and edit the nginx configuration file.
vi /usr/local/nginx/conf/nginx.conf
Modify the nginx configuration file to support PHP:
Server {Listen 80; SERVER_NAME localhost; # charset koi8-r; # access_log logs/host. access. log main; Location/{root HTML; Index. PHP index.html index.htm; # add index. PHP homepage file} # Add the following content location ~ \. PHP $ {fastcgi_pass Unix:/var/run/PHP-FPM/php-fpm.sock; fastcgi_index index. PHP; fastcgi_param script_filename $ document_root/$ fastcgi_script_name; Include fastcgi_params; Include FastCGI. conf ;}
Save and restart nginx after modification:
pkill -9 nignx/usr/local/nginx/sbin/nginx
Then create index. php under/usr/local/nginx/html,
vi /usr/local/nginx/html/index.php
Add the following content:
<?phpphpinfo();?>
After saving and exiting, access http: // 192.168.3.120/index. php. The following page indicates that the configuration has been successfully installed:
Here, our lnmp environment is fully established. Run your website or learn your php.
Original article, reprinted Please note:Reprinted from cold night's Linux blog