| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
9年前发布

(实用)拿到一个新的阿里云64位centOS系统后,手把手教你安装PHP生产环境(lnmp)

本次使用的是centOS6.7 64位系统

第一步:处理阿里云磁盘挂载问题

查看磁盘情况,本次机器系统盘为阿里云赠送的20G,数据盘为100G

其中/dev/xvda为系统盘,/dev/xvdb为数据盘,数据盘暂未做任何操作,要使用的话,先做挂载

输入fdisk  /dev/xvdb 对数据盘进行分区。根据提示,输入 n, p, 1, 回车,回车, wq

紧接着输入命令mkfs.ext3 /dev/xvdb1,格式化磁盘,耐心等待即可

接下来挂载此磁盘到目录,挂载目录因人而异,我习惯挂载到/data0下(因为假如以后又买了第二块数据盘,我可以挂载到/data1、/data2......等等以此类推)

输入mkdir /data0,然后输入mount /dev/xvdb1 /data0

然后写入分区表:echo '/dev/xvdb1  /data0 ext3    defaults    0  0' >> /etc/fstab

磁盘挂载大功告成,然后输入命令df -h查看,就能看到/dev/xvdb1已经挂在到/data0目录下了,以后cd /data0其实就是访问这块数据盘,而不是系统盘了,我们可以将网站和数据库以及日志等容易占用磁盘空间的文件放在这里

第二步:安装MySQL

先安装所依赖包,执行下面的命令:

yum -y install gcc gcc-c++ autoconf cmake libjpeg libjpeg-devel libpng \  libpng-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 krb5-devel libidn libidn-devel \  openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients \  openldap-servers gd gd2 gd-devel gd2-devel perl-CPAN

创建MySQL用户和组:

groupadd mysql  seradd -g mysql -s /usr/sbin/nologin mysql  mkdir -p /data0/mysql/data  mkdir /data0/mysql/logs  chown -R mysql:mysql /data0/mysql/

编译MySQL:

tar zxvf mysql-5.6.28.tar.gz  cd mysql-5.6.28/  cmake \  -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \  -DMYSQL_DATADIR=/data0/mysql/data \  -DSYSCONFDIR=/etc \  -DWITH_MYISAM_STORAGE_ENGINE=1 \  -DWITH_INNOBASE_STORAGE_ENGINE=1 \  -DWITH_MEMORY_STORAGE_ENGINE=1 \  -DWITH_READLINE=1 \  -DMYSQL_TCP_PORT=3306 \  -DENABLED_LOCAL_INFILE=1 \  -DWITH_PARTITION_STORAGE_ENGINE=1 \  -DEXTRA_CHARSETS=all \  -DDEFAULT_CHARSET=utf8 \  -DDEFAULT_COLLATION=utf8_general_ci  make && make install  cd ..

配置MySQL开机自启动

cp ./support-files/mysql.server /etc/init.d/mysqld  chmod +x /etc/init.d/mysqld  chkconfig --add mysqld  chkconfig mysqld on
添加MySQL的软链接以适应init脚本
ln -sv /usr/local/mysql/bin/mysql  /usr/sbin/mysql  ln -sv /usr/local/mysql/bin/mysqladmin  /usr/sbin/mysqladmin  ln -sv /usr/local/mysql/bin/mysqldump  /usr/sbin/mysqldump
修改配置文件
cp ./support-files/my-medium.cnf /etc/my.cnf  vi /etc/my.cnf  # 输入以下内容(可以先清空默认内容):  [mysqld]  datadir=/data0/mysql/data  socket=/tmp/mysql.sock  user=mysql  symbolic-links=0  [mysqld_safe]  log-error=/data0/mysql/logs/mysqld.log  pid-file=/data0/mysql/mysqld.pid  user=mysql  tmpdir=/tmp
初始化数据库
/usr/local/mysql/scripts/mysql_install_db  --user=mysql  --basedir=/usr/local/mysql  --datadir=/data0/mysql/data
启动MySQL
service mysqld start  # 或 /etc/init.d/mysqld start
进入MySQL(输入mysql或者/usr/local/mysql/bin/mysql直接回车即可进入)
/usr/local/mysql/bin/mysql #回车直接进入mysql
(下面这步操作非必须,这只是我个人习惯)输入以下SQL语句,创建一个具有root权限的用户(admin)和密码(12345678):
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY '12345678';  GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY '12345678';  GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '12345678'; #这句可以创建一个允许远程访问的账户  DELETE FROM user WHERE User!='admin'; #删除原先默认的用户,仅保留新建的admin用户(这招狠吧,连root用户都删了)

第三步:安装Tengine(nginx)

添加www用户和组、创建网站虚拟目录

groupadd www  useradd -g www -s /usr/sbin/nologin www  mkdir -p /data0/htdocs/www  chmod +w /data0/htdocs/www  chown -R www:www /data0/htdocs/

 安装Nginx所需的pcre库

tar zxvf pcre-8.33.tar.gz  cd tar zxvf pcre-8.33  ./configure  make && make install  ln -s /usr/local/lib/libpcre.so.1 /usr/lib64/libpcre.so.1  cd ..
安装Nginx
tar zxvf tengine-2.1.2.tar.gz  cd tar zxvf tengine-2.1.2  ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_concat_module  make && make install  cd ..
创建Nginx日志目录
mkdir -p /data0/logs  chmod +w /data0/logs  chown -R www:www /data0/logs
创建Nginx配置文件,在/usr/local/nginx/conf/目录中创建nginx.conf文件:
rm -f /usr/local/nginx/conf/nginx.conf  vi /usr/local/nginx/conf/nginx.conf
输入以下内容:
user www www;  worker_processes 4;  error_log /data0/logs/nginx_error.log  crit;  #pid /usr/local/nginx/nginx.pid;    #Specifies the value for maximum file descriptors that can be opened by this process.  worker_rlimit_nofile 65535;    events  {      use epoll;      worker_connections 65535;  }    http  {      include       mime.types;      default_type  application/octet-stream;        #charset  gb2312;        server_names_hash_bucket_size 128;      client_header_buffer_size 32k;      large_client_header_buffers 4 32k;      client_max_body_size 8m;        sendfile on;      tcp_nopush     on;        keepalive_timeout 60;        tcp_nodelay on;        fastcgi_connect_timeout 300;      fastcgi_send_timeout 300;      fastcgi_read_timeout 300;      fastcgi_buffer_size 64k;      fastcgi_buffers 4 64k;      fastcgi_busy_buffers_size 128k;      fastcgi_temp_file_write_size 128k;        gzip on;      gzip_min_length  1k;      gzip_buffers     4 16k;      gzip_http_version 1.0;      gzip_comp_level 2;      gzip_types       text/plain application/x-javascript text/css application/xml;      gzip_vary on;        #limit_zone  crawler  $binary_remote_addr  10m;        include /usr/local/nginx/conf/sites-enabled/*;  }
新建sites-available目录专门存放虚拟网站配置,创建sites-enabled目录专门存放正在使用的网站配置
mkdir /usr/local/nginx/conf/sites-available  mkdir /usr/local/nginx/conf/sites-enabled
新建www网站配置文件,vi /usr/local/nginx/conf/sites-available/www ,输入以下内容:
server  {      listen       80;      server_name  localhost;      index index.html index.htm index.php;      root  /data0/htdocs/www;      access_log  /data0/logs/www_access.log  access;      #limit_conn   crawler  20;        location ~ .*\.(php|php5)?$      {          #fastcgi_pass  unix:/tmp/php-cgi.sock;          fastcgi_pass  127.0.0.1:9000;          fastcgi_index index.php;          include /usr/local/nginx/conf/fcgi.conf;      }        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$      {          expires      30d;      }        location ~ .*\.(js|css)?$      {          expires      1h;      }  }

vi fcgi.conf,并输入下面内容:

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;  fastcgi_param  SERVER_SOFTWARE    nginx;     fastcgi_param  QUERY_STRING       $query_string;  fastcgi_param  REQUEST_METHOD     $request_method;  fastcgi_param  CONTENT_TYPE       $content_type;  fastcgi_param  CONTENT_LENGTH     $content_length;     fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;  fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;  fastcgi_param  REQUEST_URI        $request_uri;  fastcgi_param  DOCUMENT_URI       $document_uri;  fastcgi_param  DOCUMENT_ROOT      $document_root;  fastcgi_param  SERVER_PROTOCOL    $server_protocol;     fastcgi_param  REMOTE_ADDR        $remote_addr;  fastcgi_param  REMOTE_PORT        $remote_port;  fastcgi_param  SERVER_ADDR        $server_addr;  fastcgi_param  SERVER_PORT        $server_port;  fastcgi_param  SERVER_NAME        $server_name;     # PHP only, required if PHP was built with --enable-force-cgi-redirect  fastcgi_param  REDIRECT_STATUS    200;
发布sites-available里的www网站:
ln -s /usr/local/nginx/conf/sites-available/www /usr/local/nginx/conf/sites-enabled/www
启动nginx:
ulimit -SHn 65535  /usr/local/nginx/sbin/nginx

第四步:安装PHP

安装PHP所需依赖包:

tar zxvf libiconv-1.14.tar.gz  cd libiconv-1.14  ./configure --prefix=/usr/local  make && make install  cd ..    tar zxvf libmcrypt-2.5.8.tar.gz  cd libmcrypt-2.5.8/  ./configure  make  make install  /sbin/ldconfig  cd libltdl/  ./configure --enable-ltdl-install  make  make install  cd ../../    tar zxvf mhash-0.9.9.9.tar.gz  cd mhash-0.9.9.9/  ./configure  make  make install  cd ../    # 对共享库做符号链接  ln -s /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la  ln -s /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so  ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4  ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8  ln -s /usr/local/lib/libmhash.a /usr/lib64/libmhash.a  ln -s /usr/local/lib/libmhash.la /usr/lib64/libmhash.la  ln -s /usr/local/lib/libmhash.so /usr/lib64/libmhash.so  ln -s /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2  ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1  ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config  ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18    tar zxvf mcrypt-2.6.8.tar.gz  cd mcrypt-2.6.8/  /sbin/ldconfig  ./configure  make  make install  cd ../
安装PHP:
tar zxvf php-5.5.31.tar.gz  cd tar zxvf php-5.5.31  ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc \  --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config \  --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr \  --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization \  --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf \  --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap \  --enable-opcache --without-pear --disable-fileinfo  #注:如果内存较大 可以去掉--disable-fileinfo  make ZEND_EXTRA_LIBS='-liconv'  make install  cp php.ini-development /usr/local/php/etc/php.ini  cd ../  cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf  vi /usr/local/php/etc/php-fpm.conf
修改
user = nobody  group = nobody

user = www  group = www
# 将;pid = run/php-fpm.pid前的;去掉并修改为  pid = /usr/local/php/var/run/php-fpm.pid
启动php-fpm
/usr/local/php/sbin/php-fpm
将Nginx与fpm加入自启动
vi /etc/rc.local  # 输入  ulimit -SHn 65535  /usr/local/php/sbin/php-fpm  /usr/local/nginx/sbin/nginx
编译PHP扩展模块memcache、redis 、pdo_mysql、imagick
tar zxvf memcache-3.0.8.tgz  cd memcache-3.0.8  /usr/local/php/bin/phpize  ./configure --with-php-config=/usr/local/php/bin/php-config  make  make install  cd ../    tar zxvf redis-2.2.7.tgz  cd redis-2.2.7  /usr/local/php/bin/phpize  ./configure --with-php-config=/usr/local/php/bin/php-config  make  make install  cd ../     tar zxvf PDO_MYSQL-1.0.2.tgz  cd PDO_MYSQL-1.0.2/  /usr/local/php/bin/phpize  ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql  ln -s /usr/local/mysql/include/* /usr/local/include/  make  make install  cd ../     tar zxvf ImageMagick.tar.gz  cd ImageMagick-6.5.1-2/  ./configure  make  make install  cd ../     tar zxvf imagick-3.2.0RC1.tgz  cd imagick-3.2.0RC1  /usr/local/php/bin/phpize  export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig  ./configure --with-php-config=/usr/local/php/bin/php-config  make  make install  cd ../

修改php.ini配置文件

vi /usr/local/php/etc/php.ini  #在最后一行加入以下  extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/"  extension=memcache.so  extension=redis.so  extension=pdo_mysql.so  extension=imagick.so    zend_extension=opcache.so    [opcache]    opcache.memory_consumption=128    opcache.interned_strings_buffer=8    opcache.max_accelerated_files=4000    opcache.revalidate_freq=60    opcache.fast_shutdown=1    opcache.enable=1    opcache.enable_cli=1

执行下面的命令使配置文件立即生效:

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

此时已经大功告成,在/data0/htdocs/www目录下建一个index.php写入phpinfo()看看扩展是都都正常加载

其他(可选):

优化linux内核参数

vi /etc/sysctl.conf
在末尾增加以下内容:
# Add  net.ipv4.tcp_max_syn_backlog = 65536  net.core.netdev_max_backlog =  32768  net.core.somaxconn = 32768     net.core.wmem_default = 8388608  net.core.rmem_default = 8388608  net.core.rmem_max = 16777216  net.core.wmem_max = 16777216     net.ipv4.tcp_timestamps = 0  net.ipv4.tcp_synack_retries = 2  net.ipv4.tcp_syn_retries = 2     net.ipv4.tcp_tw_recycle = 1  #net.ipv4.tcp_tw_len = 1  net.ipv4.tcp_tw_reuse = 1     net.ipv4.tcp_mem = 94500000 915000000 927000000  net.ipv4.tcp_max_orphans = 3276800     #net.ipv4.tcp_fin_timeout = 30  #net.ipv4.tcp_keepalive_time = 120  net.ipv4.ip_local_port_range = 1024  65535
使配置立即生效:
/sbin/sysctl -p

修改php.ini时区:

vi /usr/local/php/etc/php.ini  #找到;date.timezone =  去掉前面的分号并改为date.timezone = Asia/Chongqing

常用命令:

#修改完php.ini后执行:  kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`  #修改完nginx.conf后执行  /usr/local/nginx/sbin/nginx -s reload  #重启mysql服务执行:  service mysqld (start|stop|restart)


有兴趣研究的留邮箱,打包发送所有安装所需文件:

来自: http://my.oschina.net/ruiorz/blog/604232

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1453115235886.html
Linux