Build a LAMP environment in CentOS

Source: Internet
Author: User
Tags configuration php http authentication http file upload mcrypt

Build a LAMP environment in CentOS
Build a LAMP environment in CentOS

Preface:This section describes how to build a LAMP environment in CentOS (all use source code compilation and installation) for web server development.

LAMP:Linux + Apache + PHP + Mysql. System:CentOS 7, 64-bit. CentOS Installation

I have selected the 64-bit CentOS 7 Linux system. The installation process is also very simple. I will not describe it too much. I will just give a brief introduction to my partitions:

Partition name Size
/Boot 200 MB
/Swap 8 GB (not greater than the physical partition)
/ 20 GB
/Home Remaining Space
Apache compilation and Installation

I have selected httpd 2.2. For more information, see the official instructions.

Install dependency:

The gcc and g ++ compilers required for installing dependencies:

########gcc、g++sudo yum install gcc sudo yum install gcc-c++

The following two are apache optimization tools:

########aprwget http://labfile.oss.aliyuncs.com/apr-1.5.1.tar.gztar -zxvf apr-1.5.1.tar.gzcd apr-1.5.1/./configure --prefix=/opt/aprsudo make && make install
########apr-utilcd Downloads/wget http://labfile.oss.aliyuncs.com/apr-util-1.5.4.tar.gztar -zxvf apr-util-1.5.4.tar.gzcd apr-util-1.5.4./configure --prefix=/opt/apr-util --with-apr=/opt/aprsudo make && make install

Perl library, compatible with Regular Expression Library:

cd Downloads/wget http://labfile.oss.aliyuncs.com/pcre-8.36.tar.gztar -zxvf pcre-8.36.tar.gzcd pcre-8.36./configure --prefix=/opt/pcresudo make && make install
Install apache:
cd Downloads/wget http://labfile.oss.aliyuncs.com/httpd-2.2.29.tar.gztar -zxvf httpd-2.2.29.tar.gzcd httpd-2.2.29./configure --prefix=/opt/apache --with-apr=/opt/apr --with-apr-util=/opt/apr-util --with-pcre=/opt/pcresudo make && make installsudo sed -i 's/^#ServerName.*/ServerName localhost/' /opt/apache/conf/httpd.conf
Test:

Start:

sudo /opt/apache/bin/apachectl start

Obtain the Server Response (the server has been set as localhost In the last step of apache installation ):

curl -I 127.0.0.1

If the installation is successful, it is as follows:

Close:

sudo /opt/apache/bin/apachectl stop
Install and compile MySQL dependencies:

Note:MySQL and later versions use cmake for compilation.

Secret file rm-f CMakeCache.txt sudo yum-y install gcc-c ++ autoconf automake zlib * fiex * libxml * ncurses-devel libmcrypt * libtool-ltdl-devel * cmake-gui libaio-devel bison-devel make
Compilation and installation (Long Time ):
cd Downloads/wget http://labfile.oss.aliyuncs.com/mysql-5.6.24.tar.gztar -zxvf mysql-5.6.24.tar.gzcd mysql-5.6.24cmake -DCMAKE_INSTALL_PREFIX=/opt/mysql -DMYSQL_DATADIR=/opt/mysql/data -DWITH_INNOBASE_STORAGE_ENGINE=1sudo make sudo make install
Create a mysql user:
id mysql > /dev/null 2>&1 || sudo useradd -r -s /sbin/nologin -M -d /opt/mysql/data mysql
Initialize the data directory:
sudo chown -R mysql:mysql /opt/mysql/datasudo /opt/mysql/scripts/mysql_install_db --basedir=/opt/mysql --datadir=/opt/mysql/data --user=mysql --defaults-file=/opt/mysql/support-files/my-default.cnf
Modify the configuration file:
sudo sh -c 'cat >> /opt/mysql/my.cnf <<EOFbasedir = /opt/mysqldatadir = /opt/mysql/datalog-error = /opt/mysql/data/error.logpid-file = /opt/mysql/data/mysql.pidport = 3306EOF'
Clear residual files:
sudo sh -c 'for file in /etc/mysql/my.cnf /etc/my.cnf ; do [ -f $file ] && rm -f $file ; done'
Start:
sudo /opt/mysql/support-files/mysql.server start

If the operation is successful, the following information is displayed:

Test:
/opt/mysql/bin/mysqladmin ping

If the operation is successful, the following information is displayed:

View the database:
/opt/mysql/bin/mysql -u root -e 'show databases;'
Close:
sudo /opt/mysql/support-files/mysql.server stop

If the operation is successful, the following information is displayed:

PHP compilation, installation, and installation dependencies:

The last three files can be downloaded from here.

Cd Downloads/sudo yum install zlib1g-dev libbz2-dev libcurl4-gnutls-dev libjpeg-dev libpng12-dev libfreetype6-dev libmcrypt-dev libxml2-devsudo yum install bzip2-devel.x86_64-ysudo yum-y install curl-develsudo yum install openssl. x86_64 openssl-devel.x86_64-ysudo yum install libjpeg. x86_64 libpng. x86_64 freetype. x86_64 libjpeg-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64-ysudo yum install libjpeg-devel libpng-devel ####### install Libmcrypttar-zxvf libmcrypt-2.5.8.tar.gzcd libmcrypt-2.5.8. /configuresudo makesudo make install Description: libmdefaults installed in/usr/local ######## install mhashcd ..tar-zxvf mhash-0.9.9.9.tar.gzcd. /configuresudo makesudo make install ######## install mcryptcd .tar-zxvf mcrypt-2.6.8.tar.gzcd mcrypt-2.6.8LD_LIBRARY_PATH =/usr/local/lib. /configuresudo makesudo make install
Compile and install:
cd Downloads/wget http://labfile.oss.aliyuncs.com/php-5.5.21.tar.gztar -zxvf php-5.5.21.tar.gzcd php-5.5.21./configure --prefix=/opt/php --with-bz2 --with-curl --with-jpeg-dir --with-gd --enable-shared --enable-mbstring --with-mcrypt --with-mysql=/opt/mysql --with-pdo-mysql=/opt/mysql --with-mysqli=/opt/mysql/bin/mysql_config --enable-fpm --enable-phar --enable-bcmath --with-zlib --enable-zip --enable-ftp --with-gettext --enable-sockets --with-freetype-dir  --with-config-file-path=/opt/php/etc/php.ini --with-config-file-scan-dir=/opt/php/etc/conf.d --with-apxs2=/opt/apache/bin/apxssudo makesudo make installsudo cp php.ini-production /opt/php/etc/php.ini
Check whether php supports mysql:
/opt/php/bin/php -m | grep mysql

If yes, the following information is displayed:

Modify the apache configuration file and add the supported php configuration:
sudo sh -c 'cat >> /opt/apache/conf/httpd.conf <<EOF<IfModule mime_module>AddType application/x-httpd-php .php</IfModule>EOF'
If it is disabled, enable apache; otherwise, restart apache:
sudo /opt/apache/bin/apachectl -k restart
Test:
sudo sh -c 'echo "<?php echo phpinfo(); ?>" > /opt/apache/htdocs/test.php'

Enter in the browser:

http://localhost/test.php

If the configuration is successful, the php information is displayed:

Add a third-party module and use apxs to manually compile the Apache module:

For more information about apxs and its parameters, see here.
The metadata module is used as an example:

Cd ~ /Downloads/httpd-2.2.29/modules/metadatasudo/opt/apache/bin/apxs-I-a-c mod_headers.c ################# ######################################## #-c: this option indicates that the compilation operation is required. It will first compile the C source program (. c) files is the target code file (. o), then, connect the target code and other target code files in files (. o and. a) To generate a dynamic shared object dsofile. If the-o option is not specified, the output file name is inferred from the first file name in files, so it is typically a mod_name.so-i: This option indicates the installation is required, to install one or more dynamic shared objects to the modules directory of the server. -A: This option automatically adds a LoadModule row to the httpd. conf file to activate the Module # title #. Or, if this row already exists, it is enabled. ######################################## ###################
Use phpize to manually compile the PHP module:
cd ~/Downloads/php-5.5.21/ext/ftp/opt/php/bin/phpize./configure --with-php-config=/opt/php/bin/php-configsudo makesudo make install

Load the module in php. ini:

sudo sh -c 'echo "extension=ftp.so" >> /opt/php/etc/php.ini'
LAMP configuration php configuration:

Configuration File/opt/php/etc/php. ini:

### General configuration engine = On short_open_tag = Off # tags recognition asp_tags = Off # asp tags recognition precision = 14 # effective digits output_buffering = 4096 # output cache allowed you can send the header implicit_flush = Off after the output body content # Tell the output layer to automatically refresh its own data after each output block disable_functions = # disable the specific function display_errors = Off # display the error message log_errors = On # record the error request_order = "GP" # get post register_argc_argv = On # whether to declare argv and argc variables post_max_size = 8 M # maximum POST size file_uploads = On # whether to allow HTTP file Upload upload_tmp_dir = # temporary directory of the file used for HTTP upload upload_max_filesize = 2 M # maximum size of the uploaded file max_file_uploads = 20 # maximum number of uploaded files each time allow_url_fopen = On # whether to allow URLs as http :.. or treat the file as ftp :... default_socket_timeout = 60 # default socker timeout # MySQL related mysql. allow_persistent = On # Allow persistent connection to mysql. cache_size = 2000 # cache size mysql. max_persistent =-1 # maximum number of persistent connections-1 does not limit mysql. max_links =-1 # maximum link to mysql. connect_timeout = 60 # connection timeout # session related session. save_handler = files # session used to save/retrieve data. use_cookies = 1 # use cookies session. name = PHPSESSID # Set the cookie name session. use_only_cookies = 1 # force session id to pass session with COOKIE. auto_start = 0 # initialize the session when the request starts. cookie_lifetime = 0 # cookie retention time # session garbage collection parameter session. gc_probability = 1 # Sort sessions by percentage. gc_divisor = 1000 # Each request has a 1/1000 chance to start the "Garbage Collection" process # The Session ID transmits the parameter session. use_trans_sid = 0 # Use URL to pass session id session. hash_function = 0 # generate the SID MD5 Algorithm session. hash_bits_per_character = 5 # specify how many bits are saved in each character of the SID string # common configuration parameter max_execution_time 30 # How many CPU seconds a script can use max_input_time 60 # When a script waits for input data how long (seconds) memory_limit 32 M # Memory (in bytes) output_buffering 4096 before a script is canceled # How much data (in bytes) needs to be cached before data is sent to the client
Apache configuration:

Apache main configuration file/opt/apache/conf/httpd. conf:

# Apache service root directory ServerRoot "/opt/apache" # service administrator ServerAdmin you@example.com # listening port Listen 80 # default homepage DirectoryIndex index. php # Load php module LoadModule php5_module modules/libphp5.so # parse phpAddType application/x-httpd-php. php
# Directory permission parameter DocumentRoot "/opt/apache/htdocs" # WEB file root Directory <Directory "/opt/apache/htdocs"> # official document link http://httpd.apache.org/docs/2.2/mod/core.html Options Indexes FollowSymLinks AllowOverride None Order allow, deny Allow from all # Allow all requests </Directory>

AllowOverride parameters:

None does not allow. htaccess to use any commands. AuthConfig allows all permission commands, including AuthDBMGroupFile AuthDBMUserFile AuthGroupFile AuthName AuthTypeAuthUserFile and Require. FileInfo allows commands of the file control type. Including AddEncoding AddLanguage AddType DEfaultType ErrorDocument extends agepriority. Indexes allows commands of the Directory control type. Including AddDescription AddIcon AddIconByEncoding AddIconByType DefaultIcon DirectoryIndex FancyIndexing HeaderName IndexIgnore IndexOptions ReadmeName. Limit allows you to use permission control commands. Including Allow Deny and Order. Options allows commands that control directory features, including Options and XBitHack.

Options parameters:

All indicates All features except MultiViews. This is also the default setting of the Options command. None indicates that no server features are enabled. The FollowSymLinks server allows symbolic connections in this directory. If this configuration option is in the configuration segment, it will be ignored. Indexes if the entered URL corresponds to a file directory on the server, and there is no DirectoryIndex command MultiViews in this directory, the mod_negotiation Module is allowed to provide "multi-view" for content negotiation ". If the path requested by the client may correspond to multiple types of files, the server automatically selects the file that best matches the requirements of the client according to the specific request of the client. The SymLinksIfOwnerMatch server is used only when the symbolic connection has the same user ID as the owner of the target file or directory. The symbolic connection is used only when the owner of the target file or directory to which the symbolic connection points is the same user. ExecCGI allows the use of the mod_cgi module to execute CGI scripts. Includes allows the mod_include module to provide the server-side inclusion function IncludesNOEXEC allows the server-side inclusion, but disables "# exec cmd" and "# exec cgi ". However, you can still use the "# include virtual" virtual CGI script from the ScriptAlias directory.
# Log Configuration Parameter ErrorLog "logs/error_log" LogLevel warn # error log record level <IfModule log_config_module> LogFormat "% h % l % u % t \" % r \ "%> s % B \ "% {Referer} I \" \ "% {User-Agent} I \" "combined LogFormat" % h % l % u % t \ "% r \" %> s % B "common # custom log name: common <IfModule logio_module> LogFormat" % h % l % u % t \ "% r \" %> s % B \" % {Referer} I \ "\" % {User-Agent} I \ "% I % O" combinedio </IfModule> CustomLog "logs/access_log" common # The Log Path is named common custom format </IfModule>

LogLevel Log Level:

Error. Warn warning. Info. Debug error level information.

Log format:
1.% h-Client IP address or host name.
2.% l-The ID of RFC 1413 determined by The client identd. The "-" symbol in The output indicates that The information here is invalid.
3.% u-name of the customer accessing the webpage obtained by the HTTP Authentication System. Valid only when authentication is available. The "-" symbol in the output indicates that the information here is invalid.
4.% t-time when the server completes processing the request.
5. "% r"-the quotation marks indicate the request content sent by the customer that contains many useful information.
6. %> s-the status code returned by the server to the client.
7.% B-the last value is the number of bytes that are returned to the client, excluding the response header.
8. "% {Referer} I"-This item indicates the webpage from which the request was submitted.
9. "% {User-Agent} I"-This item is the browser identification information provided by the customer's browser.

# Actual access log 192.168.1.166--[04/Feb/2015: 12: 52: 35 + 0800] "GET/HTTP/1.1" 200 25
# Domain name-based VM <VirtualHost *: 80> DocumentRoot/web1 ServerName web1.com ErrorLog/app/apache/logs/error_log CustomLog/app/apache/logs/access_log combined </VirtualHost> <VirtualHost *: 80> DocumentRoot/web2 ServerName web2.com </VirtualHost> # IP-based virtual host <VirtualHost 210.20.30.40> DocumentRoot/web1 ServerName web1.com </VirtualHost> <VirtualHost 210.20.30.50> DocumentRoot web2 ServerName web2.com </ virtualHost> # port-based virtual host Listen 172.20.30.40: 80 Listen 172.20.30.40: 8080 <VirtualHost 172.20.30.40: 80> DocumentRoot/web1 ServerName web1.com </VirtualHost> <VirtualHost 172.20.30.40: 8080> DocumentRoot/web2 ServerName web2.com </VirtualHost>
# Running k working mode parameter StartServers 50 # pre-create 50 processes MinSpareServers 15 # Keep the minimum idle process MaxSpareServers 30 # Keep the maximum idle process MaxClients 225 # maximum number of processes MaxRequestsPerChild 4000 # used to control each process how many requests are automatically destroyed after processing

Note:UsePs auxCheck the number of memory occupied by the process to adjust the parameters in the actual environment to prevent consumption of all resources.

Feedback and suggestions Weibo: @ AnSwEr is not the AnSwEr github: AnSwErYWJ blog: AnSwEr is not the AnSwEr column Reference List: https://www.shiyanlou.com/courses/running/600https://www.shiyanlou.com/courses/running/601

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.