Linux (centOS) manual installation of Apache + MySQL + PHP + Memcached + Nginx

Source: Internet
Author: User
Tags openssl library php memcached nginx server fully qualified domain name
This document describes how to manually install Apache + MySQL + PHP + Memcached + Nginx on Linux (centOS ).

========================= Step 1: install Apache (updated to 2.2.x) ==============================

Step 1: install Apache (updated to 2.2.x)


Download Apache and go to the official website to find a new one.

There are many image sites in China, such as "everybody" and "163"

Please refer to httpd because there are many projects in apache, and httpd is the web server.

-------------------------------------------------#wget zxvf httpd-2.2.22.tar.gz#cd ./httpd-2.2.22#./configure --prefix=/usr/local/apache2/ --enable-proxy --enable-ssl --enable-cgi --enable-rewrite --enable-so --enable-module=so#make#make install

NOTE: If make fails, configure fails. For example, if you lack the ssl component, you can remove it.

-- Enable-ssl

If the error "No recognized SSL/TLS toolkit detected" appears

#yum -y install openssl openssl-devel


If Make: command not found

Check if make is not installed.

# Rpm-qa | grep make

If there is no information, make is not installed.

# Yum install make

Manual start

# /usr/local/apache2/bin/apachectl start


# /usr/local/apache2/bin/apachectl restart

If an error is reported:

Cocould not reliably determine the server's fully qualified domain name, using for ServerName

You need to modify/usr/local/apache2/conf/httpd. conf in


This location is changed:

serverName localhost:80


Start of YUM Automatic Installation

#service httpd restart

Enter this prompt to indicate that the httpd service is not identified.

For more information, see this blog.


This indicates that the installation is successful.

Input in your browser:


In this case, it indicates that the operation is successful!


Firewall Configuration

Note: If you want to access the Web page of the VM on a local machine, such as xp, if centos6 is used, you need to modify the firewall to start port 80.

/etc/sysconfig[root@centos6 ~]# vim /etc/sysconfig/iptables

Add the following line to open the firewall port 80:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

Restart Firewall

#/etc/init.d/iptables restart

View CentOS Firewall Information:/etc/init. d/iptables status

Disable CentOS Firewall Service:/etc/init. d/iptables stop


Enable Auto Start Service

Edit the rc. local file
# Vi/etc/rc. d/rc. local

Add the following startup command

/usr/sbin/apachectl start/etc/rc.d/init.d/mysqld start/etc/rc.d/init.d/smb start/usr/local/subversion/bin/svnserve -d

Note: My path is the path obtained by yum to install these services. You must enter the path according to your own installation path. If you cannot find the path, you can use the whereis command, it mainly finds the three startup files apachectl, mysqld, and smb.


Add permissions and file format conversion (otherwise, apache will report no permission to execute the file)
# Chmod + x file
# Dos2unix files


AB testing

#ab -n 1000  -c  10 http://localhost/


Uninstall apache

For source code Installation

# Rm-rf your apache installation path

Also act on other


Httpd enables Virtual Hosts

#Include conf/extra/httpd-vhosts.conf



Install YUM in the following way:

#yum remove httpd

Otherwise, use

#rpm -qa|grep httpd

Delete one by one

# rpm -e  ......

If the module is not correctly installed, the following error will be reported:In this case, open the comment module, especially the module of mod_slotmem_shm must be enabled.

[Sun May 06 19:02:44.486163 2012] [proxy_balancer:emerg] [pid 9141:tid 140629202720512] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??[Sun May 06 19:02:44.486651 2012] [:emerg] [pid 9141:tid 140629202720512] AH00020: Configuration Failed, exiting

Open the comment Module


Step 2 install Mysql


Method 1: Use yum for Automatic Installation

#yum -y install mysql-server


Type 2: Download the source code for compilation and Installation

# Wget zxvf mysql-5.1.57.tar.gz # cd mysql-5.1.57 #. /configure -- prefix =/usr/local/mysql -- with-mysqld-user = mysql -- with-charset = gbk -- bindir =/usr/local/bin -- sbindir =/usr/ local/sbin -- with-plugins = innobase -- with-partition # make install # cd/usr/local/mysql # mysql_install_db # chown-R mysql: mysql * # cp/usr/local/mysql/share/mysql. server/etc/init. d/mysql #/etc/rc. d/mysql start (add to/etc/rc. local to automatically start mysql after the system restarts)

Modify/etc/rc. local

Note: Please modify wait_timeout = 600 in the mysql configuration file my. cnf (some programs have a long execution time. If the mysql timeout time is too short, the mysql server has go away error may occur)


After the installation is complete, copy my. cnf to the ect directory and use one of the support-files directories as the template. There are four template files in this directory, which are customized based on the memory of different machines.

The mysql-5.1.57 version executes the following copy

# Cp support-files/my-medium.cnf/etc/my. cnf

Run the following copy for mysql-5.1.67 and later versions

# Cp/usr/local/mysql/share/mysql/my-medium.cnf/etc/my. cnf

The above script is used for installation. After installation is complete, a prompt is displayed when it is started.

[root@kspc mysql-5.1.31]# /etc/init.d/mysqld startStarting MySQL.Manager of pid-file quit without updating fi[FAILED]

The script has a creation
/Usr/local/mysql/bin/mysql_install_db -- user = mysql-------------------- Initialize the database and set the user

Run again

Restart mysql. Everything is OK.

[root@kspc mysql-5.1.31]# /etc/init.d/mysqld restartMySQL manager or server PID file could not be found!       [FAILED]Starting MySQL.                      [  OK  ]


Step 3 install PHP (updated to version 5.4.8)


Go to to find the latest version.

5.4 Installation

#wget zxvf  php-5.4.8.tar.gz#cd php-5.4.8
#./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql-dir=/usr/local/mysql

If an unknown configuration item -- with-mysql-dir =/usr/local/mysql is prompted, use the following

#./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql-dir=/usr/local/mysql

There may be various causes of failure.


If the prompt is: Cannot find MySQL header files under/date/mysql.

The installation package for mysql-devel is missing.
Yum-y install mysql-devel!

# Yum-y install mysql-devel


If the prompt is: xml2-config not found. Please check your libxml2 installation.

Libxml2 needs to be installed

# Yum install libxml2-devel


The following warning message may exist during installation:

Bytes ---------------------------------------------------------------------------------------------------------

##configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.    wget    tar -zxvf re2c-0.13.5.tar.gz    cd re2c-0.13.5    ./configure && make && make install

If the prompt is:

Try adding --with-zlib-dir=#./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql-dir=/usr/local/mysql --with-zlib-dir=/usr/local/zlib


Compilation and installation can only be completed successfully

# Make

# Make install


If the installation fails, the following error occurs: Cannot load/usr/local/apache2/modules/ into server:

Cause: There is a SELinux protection mode in Linux.

1. Method for disabling SELINUX:
Vi/etc/selinux/config change SELINUX = enforcing to SELINUX = disabled and restart
This method may cause risks to the server.

2. Do not disable SELINUX:

# setenforce 0# chcon -c -v -R -u system_u -r object_r -t textrel_shlib_t /usr/local/apache/modules/ service httpd restart# setenforce 1


Copy the php. ini configuration file

Note that the file name is changed in the new php version.

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

Modify the configuration file httpd. conf of apache


AddType application/x-httpd-php .php

Modify the default file address DocumentRoot of www

Note that the Directoty path must be modified in the same way as the preceding one.

Restart httpd

Test the PHP File

Phpinfo ()


Execute the PHP File

#/usr/local/php/bin/php    xx.php


Install eAccelerator in PHP
Two types of PHP installation extensions:

1. PHP installation extension mcrypt and related dependencies [PHP installation PECL Extension Method] 2. PHP installation and use VLD to view opcode [PHP third-party extension installation method]


Step 4 install Memcached


Libevent must be installed first

#wget xzvf libevent-2.0.16-stable.tar.gz#cd libevent-2.0.16-stable#./configure --prefix=/usr#make#make install


Step 2 install memcached

# wget xzvf memcached-1.4.9.tar.gz#cd memcached-1.4.9#./configure --prefix=/usr/local/memcached --with-libevent=/usr#make#make install


#/usr/local/memcached/bin/memcached -d -m 100 -uroot -l -p 11211 -c 512 -P /usr/local/memcached/

View Details

#ps aux|grep mem

Output pid

#cat /usr/local/memcached/

View memory usage

#top -n 1 |grep Mem


There are two extensions for operating memcache in PHP: memcache and memcached.

We recommend that you use the memcached extension based on the libmemcahced library. Apsaradb for memcache supports the CAS operations provided by apsaradb for memcache, providing better stability and efficiency.

(Installed with memcached. so, you do not need to install memcache. so you can choose either of them based on the actual situation. here we need to note that memcached has both functions, but memcached does not have some functions. Therefore, we recommend that you install memcached. so)


Php extension memcache for installing memcache

#wget vxzf memcache-2.2.6.tgz#cd memcache-2.2.6#/usr/local/php/bin/phpize
#./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir


#. /Configure -- enable-memcache -- with-php-config =/usr/local/php/bin/php-config -- with-zlib-dir =/usr # make install # vi/usr/local/php/lib/php. iniphp. add extension = memcache to ini. so

Install memcached's php extension memcached

Preparations before installation:

Install libmemcached first. skip this step if it is already installed.

Directly configure prompts that the memcached binary file cannot be found. It has already been installed. Well, make it.

The installation method is as follows:


First install the libmemcached Library

tar zxvf libmemcached-0.42.tar.gzcd libmemcached-0.42./configuremake && make install

Install php memcached Extension

Tar zxvf memcached-1.0.2.tar.gzcd memcached-1.0.2/usr/local/php/bin/phpize. /configure -- with-php-config =/usr/local/php/bin/php-configmake & make installvi/usr/local/php/lib/php. ini with extension = memcached. so restart the service.



#/Usr/local/apache2/bin/apachectl restart


Test memcache

[Php] view plaincopy $ memcache = new Memcache; // create a memcache object $ memcache-> connect ('localhost', 11211) or die ("cocould not connect "); // connect to the Memcached server $ memcache-> set ('key', 'test'); // set a variable to the memory, name is key value is test $ get_value = $ memcache-> get ('key'); // retrieve the key value from memory echo $ get_value;

--------------[php] view plaincopyclass MemCache {      var $mem = null;            private static $isLibLoad = false;      //put your code here      public function Com_MemCache(){          if(!self::$isLibLoad){              dl("");              self::$isLibLoad = true;          }                    $this->mem = new Memcache();          $this->mem->connect("", 11211);      }        public function set($key,$value,$flag = null,$expire = 5000){          $this->mem->set($key, $value, $flag, $expire);      }        public function get($key){          return $this->mem->get($key);      }        function __destruct(){          $this->mem->close();      }  }


[php] view plaincopy$cache = new MemCache();             $mm = $cache ->get("A");             if($mm==null){               $cache->set('A', date('Y-m-j').' '.date('G:i:s'),null,15);             }  echo $mm;

Test memcached and run the memcached daemon.
Running the memcached daemon is simple. You only need one command line and no configuration file needs to be modified (and no configuration file is modified for you ):
/Usr/local/memcached/bin/memcached-D-m256-URoot-L127.0.0.1-P11211-C256-P/Tmp/memcached. pid

Httpd parameters:
-D run memcached in daemon mode;
-M: Set the memory size available for memcached, in MB;
-L set the IP address of the listener. If it is a local machine, this parameter is usually not set;
-P: Set the listening port. The default value is 11211. Therefore, you can leave this parameter Unspecified. (Port 11212 is used during the test );
-U specifies the user. If the current user is root, you must use this parameter to specify the user.

-The "c" option is the maximum number of concurrent connections. The default value is 1024. I have set 256 here, which is based on the load of your server,

-P is the pid file for saving Memcache. Here I save it in/tmp/memcached. pid,

To end the Memcache process, run:

# Kill 'cat/tmp/memcached. Pi'

You can also start multiple daemon processes, but the ports cannot be repeated.

Of course, there are other parameters that can be used. man memcached can be seen at a moment.

$memcache = new Memcached();  $memcahce->connect('', 12121);  $memcache->set('Key', 'Value');  $memcache->get('Key');


Nginx Installation


#wget --prefix=/usr/local/nginx#make#make install

An error occurred while installing Nginx.

./configure: error: the HTTP rewrite module requires the PCRE library.

Install pcre-devel to solve the problem

yum -y install pcre-devel

Error message:

./configure: error: the HTTP cache module requires md5 functionsfrom OpenSSL library. You can either disable the module by using--without-http-cache option, or install the OpenSSL library into the system,or build the OpenSSL library statically from the source with nginx by using--with-http_ssl_module --with-openssl=


yum -y install openssl openssl-devel


yum -y install pcre-devel openssl openssl-devel./configure --prefix=/usr/local/nginxmakemake install

Start nginx

#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

Successful installation page

Stop operation

The stop operation is performed by sending a signal to the nginx process (for more information about the signal, see the linux chapter ).
Step 1: query the nginx master process number
Ps-ef | grep nginx
Find the master process in the process list. Its number is the master process number.
Step 2: send signals
Stop Nginx with ease:
Kill-QUIT master process number
Stop Nginx quickly:
Kill-TERM master process number
Force stop Nginx:
Pkill-9 nginx

In addition, if the pid file storage path is configured in nginx. conf, the file stores the Nginx main process number. If no pid file is specified, the file is placed in the nginx logs directory. With the pid file, we do not need to first query the main process Number of Nginx, but directly send a signal to Nginx. The command is as follows:
Kill-signal type '/usr/nginx/logs/nginx. pid'

Smooth restart
If you change the configuration, restart Nginx. Do you need to disable Nginx before enabling Nginx? No. You can send a signal to Nginx to restart Nginx smoothly.
Smooth restart command:
Kill-HUP enters the title or process number file path

Or use

/Usr/nginx/sbin/nginx-s reload

Note: After modifying the configuration file, check whether the modified configuration file is correct to avoid Nginx errors affecting the stable running of the server. The command to determine whether the Nginx configuration is correct is as follows:

nginx -t -c /usr/nginx/conf/nginx.conf


/usr/nginx/sbin/nginx -t

Smooth upgrade
If the Nginx server is running and the module needs to be upgraded, added, or deleted, we need to stop the server and modify it accordingly, so that the server will stop service for a period of time, nginx can be upgraded without stopping services, without affecting server operation.
Step 1:
If the Nginx program is upgraded, replace the old program file with the new program. If the new program is compiled and installed, it is directly compiled into the Nginx installation directory.
Step 2: Execute the command
Master process number or process File Name of the earlier version of kill-USR2
In this case, the old Nginx main process will change its process file to. oldbin, and then execute the new Nginx version. The old and new Nginx servers run in the same city to process requests.
In this case, stop the old Nginx version gradually and enter the following command:
Kill-master process Number of the old WINCH version
The old working process will exit as the task is completed, and the new Nginx working process will gradually replace the old working process.

At this time, we can decide whether to use the new version or restore to the old version.
Do not reload the configuration to start the new/old Working Process
Kill-HUP old/new version master process number
Calmly close old/new processes
Kill-QUIT old/new master process number
If an error is reported, the following command is used to shut down the old or new working process before closing the master process number:
Kill-TERM old/new worker process number

In this case, if you want to recover to the old version, you only need to operate the master process number of the new version in the previous steps, if you want to use the new version, perform the previous steps to operate the master process Number of the old version.

The above are some basic Nginx operations. I hope Nginx will have a better way to handle these operations in the future. It is best to use Nginx commands instead of sending system signals to the Nginx process.

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: 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.