Configuring LNMP Environment under CentOS (source installation)

Source: Internet
Author: User
Tags fpm ftp install php mysql download openssl openssl download php download iptables


prepare to install dependent libraries


Check and install components
yum-y install gcc automake autoconf libtool make gcc-c++ glibc libxslt-devel libjpeg libjpeg-devel libpng l Ibpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2- Devel ncurses ncurses-devel Curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel OpenSSL Openssl-deve L pcre pcre-devel libmcrypt libmcrypt-devel cmake


A few notes:
Pcre, OpenSSL, zlib are required when installing Nginx
CMake is required when installing MySQL



First, view the Linux version of this


[Root@centos src]# cat/etc/issue
CentOS Release 6.7 (Final)
Kernel \ r on an \


second, compile and install Nginx



1. Download Nginx (Stable version)


WGET-P/tmp http://nginx.org/download/nginx-1.8.1.tar.gz


-P Specify download file directory
Or


[Root@centos/]# cd/usr/local/src
[root@centos src]# wget http://nginx.org/download/nginx-1.8.1.tar.gz


Default download to current directory



2, decompression Nginx


[Root@centos src]# Tar xf nginx-1.8.1.tar.gz 
[root@centos src]# cd nginx-1.8.1
[Root@centos nginx-1.8.1]#./ Configure--help (view parameters)


3. Compiling Nginx
If you specify users and user groups, you need to first create


Create user www and user group www
groupadd www
useradd-g www www
./configure--prefix=/usr/local/nginx--sbin-path=/usr/sbin/nginx--user=www--group=www--with-http_ssl_module-- With-http_gzip_static_module
    Make && make install//start Nginx * * First way to specify--sbin-path=/usr/sbin/nginx** nginx//Start Nginx-s St op//Stop nginx-s Reload//Reload * * The second method does not specify--sbin-path** cd/usr/local/nginx./sbin/nginx Restart nginx/u

    Sr/local/nginx/sbin/nginx-s Reload * * * Third Way * * Configure boot start first write a shell script, script name: Nginx Vi/etc/rc.d/init.d/nginx #! /bin/bash # chkconfig:35 # Description:nginx is an HTTP (s) server, HTTP (s) reverse set-e path=/ Usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin desc= "Nginx daemon" Name=nginx daemon=/usr/local/n Ginx/sbin/$NAME (This is the path specified by the Nginx installation--sbin-path) scriptname=/etc/init.d/$NAME test-x $DAEMON | | Exit 0 D_start () {$DAEMON | | echo-n "already Running"} d_stop () {$DAEMON-S Quit | | echo-
    N "not Running"} d_reload () {$DAEMON-S reload | | echo-n "COUNLD not Reload"} case "$" in Start) Echo-n "starting$DESC: $NAME "D_start echo". "
    ;;
    Stop) echo-n "stopping $DESC: $NAME" D_stop echo "."
    ;;
    Reload) echo-n "Reloading $DESC configuration ..." D_reload echo "Reloaded.";
    Restart) echo-n "Restarting $DESC: $NAME" D_stop sleep 2 d_start echo ".
    ;;
    *) echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2 exit 3;;  ESAC exit 0//Put the shell script into/etc/rc.d/init.d/and execute the following command chmod +x/etc/rc.d/init.d/nginx (set executable permissions) Chkconfig

    --add Nginx (Add system service) service Nginx start service nginx stop service nginx Restart service Nginx Reload Browser access: http://localhost If the Nginx page can appear to indicate success//view Nginx Process Ps-ef | grep nginx//View the number of processes minus the first Ps-ef | grep Nginx | Wc-l//View 80 Port NETSTAT-ANPT


4, install php
//php Download


cd/usr/local/src///If the file being downloaded is mirror, unzip mirror directly wget Http://cn2.php.net/get/php-5.6.13.tar.gz/from/this/mirror Tar zxvf php-5.6.13.tar.gz cd php-5.6.13./configure--prefix=/usr/local/php--with-curl--with-bz2--with-zlib--with-m Hash--with-pcre-regex--with-mysqli=mysqlnd--with-mysql=mysqlnd--with-gd--with-jpeg-dir--with-png-dir-- With-openssl--with-pdo-mysql--with-libxml-dir--with-freetype-dir--with-iconv--enable-opcache--enable-bcmath-- Enable-ftp--enable-shmop--enable-fpm--enable-sockets--enable-sysvsem--enable-sysvshm--enable-pcntl-- Enable-mbregex--enable-calendar--enable-zip--enable-mbstring--enable-pdo make (recompile requires make clean clear) make install// Configure PHP-FPM cd/usr/local/php/etc CP php-fpm-default.conf php-fpm.conf//create user www and user group www groupadd www us
    Eradd-g www www//modify php-fpm.conf VI php-fpm.conf//change user = Nobody group = nobody to user = www Group = www Remove//Create php.ini cp/us with PID = Run/php-fpm.pid Front semicolonR/local/src/php-5.6.13/php.ini-production/usr/local/php/lib/php.ini//Set PHP-FPM boot//copy php-fpm script to/etc/init.d directory CP/USR/LOCAL/SRC/PHP-5.6.13/SAPI/FPM/INIT.D.PHP-FPM/ETC/INIT.D/PHP-FPM//Set permissions and start Php-fpm:chmod 755/etc/init.d/ PHP-FPM/ETC/INIT.D/PHP-FPM start chkconfig--add php-fpm///Finally, give php-fpm start, stop, and restart as a service: services PHP-FPM S Tart Service php-fpm stop service PHP-FPM Reload


5. Configuration support PHP


    cd/usr/local/nginx/conf
    vi  nginx.conf
    user   www  www;  #修改nginx运行账号为: www user
    index  index.php index.html index.htm;   #增加index. PHP location

     ~ \.php$ {
        root           html;
        Fastcgi_pass   127.0.0.1:9000;
        Fastcgi_index  index.php;
        Fastcgi_param  script_filename   $document _root$fastcgi_script_name;
        Include        fastcgi_params;
  }
     #取消FastCGI the comment for the server section location, and note the parameters of the Fastcgi_param line, change to $document_root$fastcgi_script_name, or use the absolute path

    // Verify Nginx configuration Syntax 
    cd/usr/local/nginx
    ./sbin/nginx-t

    //test
    cd/usr/local/nginx/html
    vim index.php 
    //contents are as follows
     <?php phpinfo ()?>
    //If the environment information of PHP can be displayed, the configuration is successful.


6. Virtual Host Settings
Here you use multiple. conf method Configurations
This is the time to put the project in the WWW directory, the corresponding domain name is onethinkcms.com
The downloaded items are placed under/www/onethinkcms
Open/usr/local/nginx/conf/nginx.conf
Add the following line at the end


# The configuration file containing all the virtual hosts is added in the Conf directory under the new vhosts directory to add the corresponding. conf profile

include vhosts/*.conf;


Create a new directory under/usr/local/nginx/conf


mkdir vhosts cd vhosts vim onethinkcms.conf//Add the following configuration server {Listen 80;

        server_name onethinkcms.com www.onethinkcms.com;

         Access_log/www/onethinkcms.log;

             Location/{root/www/onethinkcms;

        Index index.php index.html index.htm;

      } # Error_page 502 503 504/50x.html;

      # location =/50x.html {# root/usr/share/nginx/html; #} # Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~. php$ {# here
            The value corresponds to the $document_root root/www/onethinkcms;

            Fastcgi_pass 127.0.0.1:9000;

            Fastcgi_index index.php;

            #注意这里的路径 Fastcgi_param script_filename $document _root$fastcgi_script_name;

        Include Fastcgi_params; }
}


Configuring the Hosts File
Vi/etc/hosts Add 127.0.0.1 onethinkcms.com
Re-start Nginx
Visit onethinkcms.com to



7. Install MySQL



① Download MySQL
MySQL official website->downloads->community->mysql Community server-> drop-down box select source Code



CD/USR/LOCAL/SRC wget
Http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.30.tar.gz



② Installing the tools and libraries needed to compile the source code
If the CMake library is missing



Yum install CMake (can download source code from http://www.cmake.org and compile installation) (Installation package installation cmake)
wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz
TAR-XZVF cmake-2.8.10.2.tar.gz
./bootstrap Make Make
Install



③ setting up MySQL users and groups



New MySQL user group Groupadd MySQL
New MySQL user useradd-r-g MySQL MySQL



④ the directory needed to create a new MySQL



New MySQL installation directory mkdir-p/usr/local/mysql
New MySQL database data file directory Mkdir-p/data/mysqldb



⑤mysql Source Package Decompression



Cd/usr/local/src
Tar zxvf mysql-5.6.27.tar.gz
CD CD mysql-5.6.27



Cmake-dcmake_install_prefix=/usr/local/mysql
-dmysql_unix_addr=/usr/local/mysql/mysql.sock-ddefault_charset=utf8-ddefault_collation=utf8_general_ci-dwith_ Innobase_storage_engine=1-dwith_myisam_storage_engine=1-dwith_memory_storage_engine=1-dwith_partition_storage_ Engine=1-dmysql_datadir=/data/mysqldb-dmysql_tcp_port=3306-denable_downloads=1-dextra_charsets=all-denabled_ Local_infile=1-dwith_readline=1
(Note: Re-run the configuration, you need to delete the CMakeCache.txt file rm CMakeCache.txt) make make install



⑥ modifying MySQL directory Owners and Groups



Modify the MySQL installation directory Cd/usr/local/mysql chown-r Mysql:mysql.
Modify the MySQL database file directory cd/data/mysqldb chown-r mysql:mysql. (Note the following. refers to the current directory)



⑦ initializing MySQL Database



Cd/usr/local/mysql
./scripts/mysql_install_db–user=mysql–datadir=/data/mysqldb



⑧ Copy the MySQL service startup configuration file



Cp/usr/local/mysql/support-files/my-default.cnf/etc/my.cnf
(Note: Overwrite if the/etc/my.cnf file exists.) )



⑨ copy MySQL service startup script and join path path



CP Support-files/mysql.server/etc/init.d/mysqld



⑩ start the MySQL service and join the boot-up



Service mysqld Start
Chkconfig–level mysqld on



⑪ Check if the MySQL service is started



NETSTAT-TULNP | grep 3306
MySQL needs to be added to the environment variable Vim/etc/profile
At the end, add the following command
Mysql_home=/usr/local/mysql
Path= $MYSQL _home/bin: $PATH
Export PATH Mysql_home
Source/etc/profile
Mysql-u root-p (password is empty, if you can log on, the installation is successful)



⑫ Modifying the password of the MySQL user root



Method One: Mysql-u root-p
Use MySQL
Update user set Password=password (' 123456 ') where user= ' root '
Flush privileges (Refresh permissions)
Method Two:/usr/local/mysql/bin/mysql_secure_installation
(Modify the password for the MySQL user root, and disable root remote connection, remove test database and anonymous user.) )



⑬ errors that may occur
Error one: Starting MySQL. The server quit without updating PID file ([failed]/mysql/server03.mylinux.com.pid).
FIX: Modify/etc/my.cnf in DataDir, point to correct MySQL database file directory



Bug Two: Error 2002 (HY000): Cannot connect to local MySQL server through socket '/tmp/mysql.sock ' (2)
Workaround: Create a new link or add the-s parameter to MySQL to directly indicate the Mysql.sock location. Ln-s/usr/local/mysql/data/mysql.sock/tmp/mysql.sock
/usr/local/mysql/bin/mysql-u root-s/usr/local/mysql/data/mysql.sock



Error three:-bash:mysql:command not found (you can also add environment variables directly)
FIX: Because the path of the MySQL command is under/usr/local/mysql/bin, so when you use the MySQL command directly, the system checks the command under/usr/bin, so I can't find the
Ln-s/usr/local/mysql/bin/mysql/usr/bin make a link



summary of issues encountered during installation configuration
1. PDO connection Error sqlstate[hy000] [2002] No such file or directory caused by:pdoexception


    Vi/usr/local/php/lib/php.ini 
    mysql.default_socket =/usr/local/mysql/mysql.sock
    pdo_mysql.default_socket =/usr/local/mysql/mysql.sock
    Mysqli.default_socket =/usr/local/mysql/mysql.sock


Restart PHP-FPM Service PHP-FPM restart



2, port and firewall issues
Configure the firewall to turn on 80 ports, 3306 ports
Vi/etc/sysconfig/iptables
-A input-m state–state new-m tcp-p tcp–dport 80-j ACCEPT (allow 80 ports through the firewall)
-A input-m state–state new-m tcp-p tcp–dport 3306-j ACCEPT (Allow 3306 ports through the firewall)
Special Note: Many netizens add these two rules to the last line of the firewall configuration, causing the firewall to fail to start, the correct one should be added to the default 22 port under this rule
After adding the firewall rules as follows:


*filter
: INPUT accept [0:0]
: FORWARD accept [0:0]
: OUTPUT accept [0:0]
-A input-m State--state Establis  Hed,related-j Accept-
a input-p icmp-j accept
-a input-i lo-j ACCEPT
-a input-m state--state new-m TCP -P TCP--dport 22-j accept
-a input-m state--state new-m tcp-p TCP--dport 80-j accept
-a input-m state-- State new-m tcp-p TCP--dport 3306-j ACCEPT
-a input-j REJECT--reject-with icmp-host-prohibited
-a FORWARD- J REJECT--reject-with icmp-host-prohibited
COMMIT
/etc/init.d/iptables restart  #Finally restart the firewall to make the configuration effective


View firewall status
/etc/init.d/iptables status
Turn off SELinux


Vi/etc/selinux/config
#SELinux = forcing? Comment out
#Selinuxtype = targeted? Commented out
SELinux = disabled
: Wq  Save, close
Shutdown-r now? Restart the system


PHP command could not find a problem



① can be installed via yum install PHP-CLI (PHP-CLI version is lower)
Yum Remove Php-cli



②bash:php:command not found
Solve:
Export path= $PATH:/usr/local/php/bin
Then, Echo $PATH look, and see.
Usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/php/bin
At this time, you can directly in any directory of the system php-i can be executed



Installing PHP Extensions
① in the PHP source package to find the appropriate extension
Cd/usr/local/src/php-5.6.13/ext/pdo



② is executed under this directory
/usr/local/php/bin/phpize



③ execution
./configure–with-php-config=/usr/local/php/bin/php-config
Make & make Install



④ Open php.ini File
cd/usr/local/php/lib/
Join extension=pdo.so



⑤ Restart PHP-FPM
Service PHP-FPM Restart



If you encounter a problem such as the absence of ' Mysql_driver.lo ' is a valid Libtool object
Clear the compiled file make clean or remove the PHP installation package, re-unzip the new



Download link
Nginx Download
wget http://nginx.org/download/nginx-1.8.0.tar.gz
OpenSSL download
wget http://www.openssl.org/source/openssl-1.0.2d.tar.gz
Pcre Download
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz
PHP download
Wget http://cn2.php.net/get/php-5.6.13.tar.gz/from/this/mirror (if the file download file is mirror, directly decompression mirror can)
Libmcrypt Download
wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
MySQL Download
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.30.tar.gz



Installing the Libmcrypt Extension


    CD/USR/LOCAL/SRC
    tar zxvf libmcrypt-2.5.7.tar.gz
    cd libmcrypt-2.5.7
    ./configure--prefix=/usr/local/ Libmcrypt make
    && make install
    vi/etc/ld.so.conf.d/local.conf
    Add "/usr/local/libmcrypt/lib"
    ldconfig-v


Attached: my own test, feasible. Refer to the small partner step-by-step


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.