Home Backend Development PHP Tutorial php+nginx+mysql installation and configuration under linux

php+nginx+mysql installation and configuration under linux

Aug 08, 2016 am 09:30 AM
mysql nbsp nginx php

我主要是用来安装php,以及nginx和php的交互。

一 安装插件


<span>可以选择YUM安装或者源码编译安装 </span><span>gcc</span><span>gcc</span>-c++<span> zlib
pcre
pcre-devel
libevent
libevent</span>-<span>devel
libxml2 
libxml2</span>-<span>devel
libmcrypt 
libmcrypt</span>-<span>devel
curl</span>-<span>devel
libpng</span>-<span>devel
libtool</span>-ltdl-<span>devel
gd</span>-<span>devel
openssl 
openssl</span>-<span>devel
ncurses</span>-<span>devel
cmake
mysql-devel </span>
Copy after login

 

 

 

二 安装mysql

<span>tar</span> -zxvf mysql-<span>5.5</span>.<span>25</span>.<span>tar</span><span>.gz
将mysql包解压 然后放入你想要mysql的安装位置 如本例中的/usr/local/webserver/mysql cmake命令需要这个路径</span><span>cmake \ </span>-DCMAKE_INSTALL_PREFIX=/usr/local/webserver/<span>mysql \ </span>-DMYSQL_DATADIR=/user/local/webserver/mysql/<span>data \ </span>-DSYSCONFDIR=/<span>etc \ </span>-DEXTRA_CHARSETS=<span>all \ </span>-DDEFAULT_CHARSET=<span>utf8 \ </span>-DDEFAULT_COLLATION=<span>utf8_general_ci \ </span>-DWITH_INNOBASE_STORAGE_ENGINE=<span>1</span><span> \ </span>-DWITH_ARCHIVE_STORAGE_ENGINE=<span>1</span><span> \ </span>-DWITH_BLACKHOLE_STORAGE_ENGINE=<span>1</span><span> \ </span>-DWITH_FEDERATED_STORAGE_ENGINE=<span>1</span><span> \ </span>-DWITH_PARTITION_STORAGE_ENGINE=<span>1</span><span> \ </span>-DWITH_PERFSCHEMA_STORAGE_ENGINE=<span>1</span><span> \ </span>-DMYSQL_UNIX_ADDR=/tmp/<span>mysqld.sock \ </span>-DMYSQL_TCP_PORT=<span>3306</span><span> \ </span>-DWITH_DEBUG=<span>0</span><span> \ </span>-DENABLED_LOCAL_INFILE=<span>1</span><span> 回车执行,执行完成后继续执行 </span><span>make</span> && <span>make</span><span>install</span><span>#设置Mysql
#在support</span>-<span>files目录中有五个配置信息文件(这里很重要,一定要根据自己的内存复制对应的cnf文件,否则mysql始终起不来):
#my</span>-small.cnf (内存<=<span>64M)
#my</span>-<span>medium.cnf (内存 128M)
#my</span>-<span>large.cnf (内存 512M)
#my</span>-huge.cnf (内存 1G-<span>2G)
#my</span>-innodb-heavy-<span>4G.cnf (内存 4GB)
cd </span>/usr/local/webserver/<span>mysql </span><span>cp</span> ./support-files/my-huge.cnf /etc/<span>my.cnf 
vi </span>/etc/<span>my.cnf
#在 [mysqld] 段增加
datadir </span>=  /usr/local/webserver/mysql/<span>data </span><span>wait</span>-timeout = <span>30</span><span> max_connections </span>= <span>512</span><span> default</span>-storage-engine =<span> MyISAM
#在 [mysqld] 段修改
max_allowed_packet </span>=<span> 16M </span><span>//</span><span>添加mysql运行的用户和用户组</span><span>groupadd mysql
useradd </span>-g mysql mysql -s /bin/<span>false</span> -d /home/mysql <span>//</span><span>没有shell,不可本机登陆(安全起见)</span><span> cd </span>/usr/local/webserver/<span>mysql </span><span>chown</span> -<span>R root . </span><span>chown</span> -<span>R mysql data </span><span>chgrp</span> -<span>R mysql . </span><span>//</span><span>生成新的mysql授权表 </span><span>//</span><span>进入mysql安装目录下的脚本目录</span> cd /usr/local/webserver/mysql/<span>scripts </span><span>//</span><span>利用mysql_install_db脚本生成新的mysql授权表</span> ./mysql_install_db --basedir=/usr/local/webserver/mysql --datadir=/usr/local/webserver/mysql/data --user=<span>mysql </span><span>//</span><span>mysql server在系统中的服务项设置 </span><span>//</span><span>复制服务文件并修改</span> cd /usr/local/webserver/mysql/support-<span>files </span><span>cp</span><span> mysql.server mysqld </span><span>//</span><span>修改mysqld</span> basedir=/usr/local/webserver/<span>mysql
datadir</span>=/usr/local/webserver/mysql/<span>data </span><span>mv</span> mysqld /etc/init.d/<span>mysqld </span><span>chmod</span><span>755</span> /etc/init.d/<span>mysqld
进行上述操作后 我们可以用 service mysqld start 来启动mysql服务了 </span><span>//</span><span>设置软连接使mysql,  mysqldump,  mysqladmin这三个bin命令能在shell中直接运行</span><span>sudo</span><span>ln</span> -s /usr/local/webserver/mysql/bin/mysql /usr/<span>bin </span><span>sudo</span><span>ln</span> -s /usr/local/webserver/mysql/bin/mysqldump /usr/<span>bin </span><span>sudo</span><span>ln</span> -s /usr/local/webserver/mysql/bin/mysqladmin /usr/<span>bin </span><span>rm</span> -rf /etc/mysql/my.cnf 因为已经把此文件复制到/etc/<span>my.cnf  如果不删除的话,mysql启动不起来。 </span>/etc/init.d/<span>mysqld start </span><span>//</span><span>设置root密码</span> mysqladmin -u root password <span>"</span><span>admin</span><span>"</span><span>//</span><span>mysql数据库中文乱码解决</span> vi /etc/<span>my.cnf </span><span>//</span><span>然后在[mysqld]配置选项下添加</span> character-set-server=<span>utf8 </span><span>//</span><span>然后进入mysql</span> cd /usr/local/webserver/mysql/<span>bin
mysql </span>-u root -<span>p
提示输入密码
mysql</span>> show variables like <span>'</span><span>%character%</span><span>'</span>;
Copy after login

 

三 安装Nginx 

#<span>tar</span> zxvf nginx-<span>0.8</span>.<span>24</span>.<span>tar</span><span>.gz
#cd nginx</span>-<span>0.8</span>.<span>24</span><span> #.</span>/configure --prefix=/usr/local/nginx <span>//</span><span>此处在本环节只需指定一个路径</span> #<span>make</span> && <span>make</span><span>install</span><span> #</span>/usr/local/nginx/sbin/nginx <span>//</span><span>启Nginx</span><span> 编写服务脚本(服务脚本请勿复制 请在linux下写入 不然回车换行符会引起异常)
vi </span>/etc/init.d/<span>nginx

把下列内容写入文件并保存 <span>#</span></span><span>!/bin/bash 
# nginx Startup script for the Nginx HTTP Server 
# this script create it by gcec at 2009.10.22. 
# it is v.0.0.1 version. 
# if you find any errors on this scripts,please contact gcec cyz. 
# and send mail to support at gcec dot cc. 
# 
# chkconfig: - 85 15 
# description: Nginx is a high-performance web and proxy server. 
# It has a lot of features, but it's not for everyone. 
# processname: nginx 
# pidfile: /var/run/nginx.pid 
# config: /usr/local/nginx/conf/nginx.conf 
nginxd=/usr/local/nginx/sbin/nginx  #这里设置为你安装nginx的执行文件位置
nginx_config=/usr/local/nginx/conf/nginx.conf  #这里设置为你nginx的配置文件位置
nginx_pid=/var/run/nginx.pid 
RETVAL=0 
prog="nginx" 
# Source function library. 
. /etc/rc.d/init.d/functions 
# Source networking configuration. 
. /etc/sysconfig/network 
# Check that networking is up. 
[ ${NETWORKING} = "no" ] && exit 0 
[ -x $nginxd ] || exit 0 
# Start nginx daemons functions. 
start() { 
if [ -e $nginx_pid ];then 
echo "nginx already running...." 
exit 1 
fi 
echo -n $"Starting $prog: " 
daemon $nginxd -c ${nginx_config} 
RETVAL=$? 
echo 
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx 
return $RETVAL 
} 
# Stop nginx daemons functions. 
stop() { 
echo -n $"Stopping $prog: " 
killproc $nginxd 
RETVAL=$? 
echo 
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid 
} 
# reload nginx service functions. 
reload() { 
echo -n $"Reloading $prog: " 
#kill -HUP `cat ${nginx_pid}` 
killproc $nginxd -HUP 
RETVAL=$? 
echo 
} 
# See how we were called. 
case "$1" in 
start) 
start 
;; 
stop) 
stop 
;; 
reload) 
reload 
;; 
restart) 
stop 
start 
;; 
status) 
status $prog 
RETVAL=$? 
;; 
*) 
echo $"Usage: $prog {start|stop|restart|reload|status|help}" 
exit 1 
esac</span><span><span>exit $RETVAL </span> 保存之后 赋予文件权限
chmod 755 /etc/init.d/nginx 

我们就可以通过service nginx start 来启动服务了 </span>
Copy after login

 

四 安装php

create user and group <span>for</span><span> fpm(fastcgi process manager)
groupadd fpm
useradd </span>--shell /sbin/nologin -<span>g fpm fpm

download, configure and </span><span>install</span> php5.<span>3.3</span><span>wget</span> http:<span>//</span><span>www.php.net/distributions/php-5.3.3.tar.gz</span><span>tar</span> zxvf php-<span>5.3</span>.<span>3</span>.<span>tar</span><span>.gz
cd php</span>-<span>5.3</span>.<span>3 </span>
Copy after login
<span>[直接复制]
./configure  --prefix=/usr/local/php  --enable-fpm  --with-fpm-user=fpm  --with-fpm-group=fpm --with-config-file-path=/usr/local/php/lib --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --without-pdo-sqlite --without-sqlite3 --without-sqlite --with-curl --enable-mbstring --with-mhash --with-mcrypt --with-openssl --with-gd --enable-sockets --with-gettext --with-zlib --enable-zip --enable-soap --with-xmlrpc --with-freetype-dir=/usr/local/freetype --enable-gd-native-ttf </span> 
<pre class="brush:php;toolbar:false"><span>--disable-fileinfo</span>
Copy after login
<span>中途错误需要yum install几个依赖包 </span><span> [手敲版]
.</span>/configure --prefix=/usr/local/<span>php \ </span>--enable-<span>fpm \ </span>--with-fpm-user=<span>fpm \ </span>--with-fpm-group=<span>fpm \ <span>--with-config-file-path=/usr/local/php/lib     #这里是配置放php.ini的存放位置</span></span>--with-mysql=<span>mysqlnd \ </span>--with-mysqli=<span>mysqlnd \ </span>--with-pdo-mysql=<span>mysqlnd \ </span>--without-pdo-<span>sqlite \ </span>--without-<span>sqlite3 \ </span>--without-<span>sqlite \ </span>--with-mysql-sock=/tmp/<span>mysql.sock \ </span>--with-<span>curl \ </span>--enable-<span>mbstring \ </span>--with-<span>mhash \ </span>--with-<span>mcrypt \ </span>--with-<span>openssl \ </span>--with-<span>gd \ </span>--enable-<span>sockets \ </span>--with-<span>gettext \ </span>--with-<span>zlib \ </span>--enable-<span>zip</span><span> \ </span>--enable-<span>soap \ </span>--with-<span>xmlrpc \
--with-freetype-dir=/usr/local/freetype \
--enable-gd-native-ttf \ <span>--with-jpeg-dir=/usr/lib64       #64位系统lib64 32位系统lib32 </span></span><span>make</span> && <span>make</span><span>install
 make出现错误</span>virtual memory exhausted: Cannot allocate memory<span>,在configure上加上–disable-fileinfo</span><span></span> 
<pre class="brush:php;toolbar:false">如果出现mysql_config not found的错误
解决办法: vi /etc/profile 在最后加入一行 export PATH="$PATH:/usr/local/mysql/bin" 这个是你的mysql安装到的目录
Copy after login


 

 五 配置php

<span>cp</span> /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
复制源码包里的php.ini-development到/usr/local/php/lib <span> vi php-fpm.conf
找到"listen="  修改为 listen = /dev/shm/php-fpm.sock (要求php版本5.3以上 该方式为使用sock文件监听)

cp</span> /backup/php-5.3.3/php.ini-development /usr/local/php/lib/php.ini
启动php
/usr/local/php/sbin/php-fpm <span>如果设置路径正确,php.ini文件也存在,还无法加载php.ini的话 修改启动命令 /usr/local/php/sbin/php-fpm -c /etc/php.ini</span>编写服务脚本(服务脚本请勿复制 请在linux下写入 不然回车换行符会引起异常)
touch /etc/init.d/phpfpm
vim /etc/init.d/phpfpm

内容如下:
Copy after login

#!/bin/bash

start() {  

     /usr/local/php/sbin/php-fpm  

     /bin/echo 'starting php listener ---[ok]'

}

stop() {  

     /usr/bin/pkill php-fpm  

     /bin/echo 'stopping php listener ---[ok]'

}

case "$1" in

start)   

    start   

    ;;

stop)   

    stop   

    ;;

restart)   

    stop   

    start   

    ;;

*)  

    echo 'usage:start/stop/restart'  

    exit 0  

    ;;

esac

 

保存退出

然后 就能通过 service phpfpm start/stop/restart 来启动监听

 六 配置Nginx

cat /usr/local/php/etc/php-fpm.conf
查看端口 为 127.0.0.1:9000
修改nginx配置文件 /usr/local/nginx/conf/<span>nginx.conf </span><span># location / {    //一定要注掉这部分,否则会不解析PHP文件,而会下载 了 
#    root   html;  
#    index  index.html index.htm;  
#} </span><span></span>
Copy after login
<span><span>location </span></span><span>~ \.php {
 
  root              www;          #这是你网站的根目录
 
  fastcgi_pass  127.0.0.1:9000;            #这里指定了fastcgi进程侦听的端口,nginx就是通过这里与php交互的
       #</span>fastcgi_pass      unix:/dev/shm/php-fpm.sock;
Copy after login
<span>  fastcgi_index  index.php;   fastcgi_param SCRIPT_FILENAME <span>$document_root</span>/</span><span><span>$fastcgi_script_name; 
   #因为SCRIPT_FILENAME在配置中是写死的并没有随着$doucument_root变化而变化,我们可以修改SCRIPT_FILENAME配置如下 </span></span> #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;<span><span>   include           fastcgi_params;        
 
}</span> 重启nginx服务
/usr/local/nginx/sbin/nginx -s reload
在/usr/local/nginx下创建www目录
mkdir www
新建一个index.php文件
cd www
vim index.php
写入<?php echo phpinfo();?>

访问服务器 如果起作用就说明配置成功</span>
Copy after login

 

七 设置php nginx mysql 自启动

<span>我想在centos里不启用图形界面
那么选择系统运行级别为2或者3的 推荐3 </span>在配置之前 我们先检查下 /etc/init.d中有没有我们mysql,php,nginx的服务脚本 如果没有的话 先配置再做下列操作

如以上 mysqld , nginx, phpfpm 这3个脚本都编写好 并且放入/etc/init.d下的话 我们来配置一下自启动
我想通过一个服务来启动这3个服务那么再写一个脚本就可以了

Copy after login

注意:system类型的服务都可以用service来启动,用chkconfig来add 和del

但是有些自己配置的服务在用chkconfig来配置到时候会提示: “service XX does not support chkconfig”

这一般都是script不符合格式造成的,解决如下,

在script开始加入两行内容即可:

# chkconfig: - 85 15

# description: this is a World Wide Web server.

<span>mv /etc/init.d/mysqld /etc/init.d/webapp-mysqld</span><span>mv /etc/init.d/nginx /etc/init.d/webapp-nginx</span><span>mv /etc/init.d/phpfpm /etc/init.d/webapp-phpfpm</span><span>touch /etc/init.d/webapp</span><span>vim /etc/init.d/webapp</span>写入以下脚本
Copy after login

#!/bin/bash

# chkconfig: - 85 15
# description: this is a World Wide Web server.

ACTION=$1

if [ "$ACTION" = "" ] || [ "$ACTION" = "start" ];then    

  #start php listeners  

  /sbin/service webapp-phpfpm start

  #start nginx service  

  /sbin/service webapp-nginx start

  #start mysql service  

  /sbin/service webapp-mysqld start

  echo "web applications[mysql,nginx,php] is running now !"

elif [ "$ACTION" = "stop" ];then    

   /sbin/service webapp-phpfpm stop  

   /sbin/service webapp-nginx stop  

   /sbin/service webapp-mysqld stop    

   echo 'web application stopped' 

else

echo "use start or stop or none after your service command"

fi

Add system service to start at boot

chkconfig --add webapp (note that the service script under /etc/init.d must add #chkconfig and #description to support the chkconfig command here, as mentioned above)

chkconfig --level 3 webapp on

In this way, our lamp architecture is configured successfully.

illustrate:

The syntax is:

chkconfig --list [name] is used to list services

chkconfig --add name is used to add services

chkconfig --del name is used to delete services

chkconfig [--level levels] name Change startup information and check the startup status of specific services.

on and off respectively refer to the starting and stopping of the service when changing the run level. reset refers to initializing service information.

For the on and off switches, the system default is only effective for run levels 3, 4, and 5, but reset can be effective for all run levels.


The above introduces the installation configuration of php+nginx+mysql under Linux, including the installation of mysql. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

NGINX and Apache: Understanding the Key Differences NGINX and Apache: Understanding the Key Differences Apr 26, 2025 am 12:01 AM

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

How to uninstall MySQL and clean residual files How to uninstall MySQL and clean residual files Apr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

See all articles