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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Top 10 PHP CMS Platforms For Developers in 2024 Top 10 PHP CMS Platforms For Developers in 2024 Dec 05, 2024 am 10:29 AM

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? Apr 01, 2025 pm 03:03 PM

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

WordPress site file access is restricted: Why is my .txt file not accessible through domain name? WordPress site file access is restricted: Why is my .txt file not accessible through domain name? Apr 01, 2025 pm 03:00 PM

Wordpress site file access is restricted: troubleshooting the reason why .txt file cannot be accessed recently. Some users encountered a problem when configuring the mini program business domain name: �...

See all articles